2014-10-19 65 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

您可以使用共享首选项来达到此目的。只要将用户需要的信息(如本地保存的用户名和密码)存储到任何登录表单中,将值分配到共享首选项和加载。