我想在用戶回到我的第一個活動時恢復編輯文本的值? 請幫我一把。如何在我移動到下一個活動並再次返回時恢復活動的值?
在此先感謝
這是在編輯文本獲取用戶價值我的第一個活動代碼
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
這是我的第二個活動代碼
public class Second_activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
是否要從Second_activity向IntentActivity發送測試,還是要確保在活動恢復時恢復IntentActivity中的文本框中輸入的文本? – 2012-03-27 09:42:30
是的,我希望當我回到活動時輸入的文本被恢復。 – Shri 2012-03-27 09:45:28