如何使用保存和加载按钮在多个文本框中保存信息

时间:2014-10-19 16:34:24

标签: java android textbox persistence

我正在尝试创建一个简单的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);
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用“共享首选项”在Android中存储和检索信息。

答案 1 :(得分:0)

您可以将共享偏好设置用于此目的。只需将值置于共享首选项中,并在用户需要将本地保存的用户名和密码等信息保存到任何登录表单时加载。