我的應用程序中有一些按鈕,當點擊它們時會打開一個對話框。 在這些對話框中,用戶可以用edittext填寫他的作業。 無論如何,我想保存該用戶輸入,以便如果我關閉對話框或應用程序,我能夠回憶這些數據,並且因爲我是新的eclipse和編程,我不知道該怎麼做。保存對話框輸入
這是我的MainActivity
public class MainActivity extends ActionBarActivity {
protected static final String TAG = null;
private Button bthomeWork;
private Button bthomeWork1;
private Button bthomeWork2;
private Button bthomeWork3;
private Button bthomeWork4;
private AlertDialog.Builder dialogbuilder;
private String strName="";
private void homeworkdialog(){
dialogbuilder = new AlertDialog.Builder(this);
final EditText txtinput = new EditText(this);
dialogbuilder.setTitle("Homework");
dialogbuilder.setMessage("Was sind deine Hausaufgaben?");
dialogbuilder.setView(txtinput);
dialogbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@SuppressLint("ShowToast") @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
strName+= txtinput;
Toast.makeText(getApplicationContext(), "Eingetragen", Toast.LENGTH_SHORT);
}
});
dialogbuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@SuppressLint("ShowToast") @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Kein Eintrag", Toast.LENGTH_SHORT);
}
});
dialogbuilder.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
bthomeWork = (Button)findViewById(R.id.bthomeWork);
bthomeWork1= (Button)findViewById(R.id.bthomeWork1);
bthomeWork2= (Button)findViewById(R.id.bthomeWork2);
bthomeWork3= (Button)findViewById(R.id.bthomeWork3);
bthomeWork4= (Button)findViewById(R.id.bthomeWork4);
bthomeWork.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
homeworkdialog();
}
});
bthomeWork1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
homeworkdialog();
}
});
希望你能幫助我:)
代替'strName + = txtinput;'可以通過變量'strName'訪問你的文本... – 2014-09-11 11:28:57
如果你想將編輯框的文本設置爲上次輸入的內容,只需執行'txtinput.setText(strName);' – 2014-09-11 11:30:20