2014-10-19 55 views
0

我試圖創建一個簡單的Android程序,其中包含名稱,地址,電話號碼等的文本框。當用戶將此信息放入並點擊保存它會清除文本框,當它們點擊加載按鈕時,它將檢索信息。我知道如何用一個EditText框做到這一點,但我無法找出多個。我可以在一個try/catch語句中做這個,還是我需要多個?這就是我現在所擁有的:如何使用保存和加載按鈕將信息保存在多個文本框中

public class MainActivity extends ActionBarActivity { 
    private EditText textBoxName; 
    private EditText textBoxAddress; 
    private EditText textBoxCity; 
    private EditText textBoxPhone; 
    private EditText textBoxEmail; 
    private static final int READ_BLOCK_SIZE = 100; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     textBoxName = (EditText) findViewById(R.id.txtName); 
     textBoxAddress = (EditText) findViewById(R.id.txtAddress); 
     textBoxCity = (EditText) findViewById(R.id.txtCity); 
     textBoxPhone = (EditText) findViewById(R.id.txtPhone); 
     textBoxEmail = (EditText) findViewById(R.id.txtEmail); 
     Button saveBtn = (Button) findViewById(R.id.btnSave); 
     Button loadBtn = (Button) findViewById(R.id.btnLoad); 

     saveBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       String strName = textBoxName.getText().toString(); 
       String strAddress = textBoxAddress.getText().toString(); 
       String strCity = textBoxCity.getText().toString(); 
       String strPhone = textBoxPhone.getText().toString(); 
       String strEmail = textBoxEmail.getText().toString(); 
       try { 
        FileOutputStream fOut = openFileOutput("textfile.txt", MODE_WORLD_READABLE); 

        OutputStreamWriter osw = new OutputStreamWriter(fOut); 

        //write the string to the file 

        osw.write(strName); 

        osw.flush(); 

        osw.close(); 

        //display file saved messages 
        Toast.makeText(getBaseContext(), "File saved successfully!", 
          Toast.LENGTH_SHORT).show(); 

        //clears the EditText 
        textBoxName.setText(""); 
        textBoxAddress.setText(""); 
        textBoxCity.setText(""); 
        textBoxPhone.setText(""); 
        textBoxEmail.setText(""); 
       } 
       catch (IOException ioe) 
       { 
        ioe.printStackTrace(); 
       } 


      } 
     }); 

     loadBtn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       try 
       { 
        FileInputStream fIn = openFileInput("textfile.txt"); 
        InputStreamReader isr = new InputStreamReader(fIn); 

        char[] inputBuffer = new char[READ_BLOCK_SIZE]; 
        String s = ""; 

        int charRead; 
        while ((charRead = isr.read(inputBuffer))>0) 
        { 
         //convert the chars to a String 
         String readString = String.copyValueOf(inputBuffer, 0, charRead); 
         s += readString; 

         inputBuffer = new char[READ_BLOCK_SIZE]; 
        } 
        //set the EditText to the text that has been read 
        textBoxName.setText(s); 
        textBoxAddress.setText(s); 
        textBoxCity.setText(s); 
        textBoxPhone.setText(s); 
        textBoxEmail.setText(s); 

        Toast.makeText(getBaseContext(), "File loaded successfully!", 
          Toast.LENGTH_SHORT).show(); 
       } 
       catch (IOException ioe) 
       { 
        ioe.printStackTrace(); 
       } 
      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

回答

0

,可以使用共享首選項來存儲和Android中檢索信息。

0

您可以使用共享首選項來達到此目的。只要將用戶需要的信息(如本地保存的用戶名和密碼)存儲到任何登錄表單中,將值分配到共享首選項和加載。

相關問題