이름, 주소, 전화 번호 등의 텍스트 상자가있는 간단한 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);
}
}