我正在制作一个 Android 应用程序,用户需要在其中添加信息。我希望如果用户留下任何空白字段并单击按钮,则应启动填充其他字段的相同 Activity 。
我为此使用了 Intent 并能够再次开始 Activity ,但所有字段都被清空了。 我怎样才能有意地做到这一点。
谢谢
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Register=(ImageButton)findViewById(R.id.register);
Reset=(ImageButton)findViewById(R.id.reset);
firstName=(EditText)findViewById(R.id.firstName);
surName=(EditText)findViewById(R.id.surname);
email=(EditText)findViewById(R.id.eMAil);
password=(EditText)findViewById(R.id.passWord);
retypePassword=(EditText)findViewById(R.id.retypePassword);
town=(EditText)findViewById(R.id.town);
cellno=(EditText)findViewById(R.id.cellno);
dateOfBirth=(Button)findViewById(R.id.datepickerbutton);
Register.setOnClickListener(clickRegisterListener);
private OnClickListener clickRegisterListener = new OnClickListener() {
public void onClick(View v)
{
try{
FirstName=firstName.getText().toString();
SurName=surName.getText().toString();
sex.setOnItemSelectedListener(new MyOnItemSelectedListener());
Email=email.getText().toString();
Password=password.getText().toString();
RetypePassword=retypePassword.getText().toString();
Town=town.getText().toString();
Cellno=cellno.getText().toString();
if(FirstName.equals(""))
{
AlertDialog.Builder alertbox = new AlertDialog.Builder(Register.this);
alertbox.setMessage("First Name field can not be left empty!");
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
Intent intent = new Intent(Register.this, Register.class);
// onSaveInstanceState(bundle);
startActivity(intent);
}
});
alertbox.show();
}
最佳答案
您需要做的是将您想要显示的所有信息传递给具有 Intent 的 Activity 。当您创建 Intent 时,您会在 Intent 中“添加额外”信息。
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("variableName", "variable value");
然后在 Activity 中您可以检索信息:
Bundle extras = this.getIntent().getExtras();
String var = extras.getString("variableName");
编辑:
如果你想在暂停时保存 Activity 状态,你需要在 onPause() 方法中保存感兴趣的变量,然后在 onResume() 方法中恢复它们
关于android - 单击按钮恢复相同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401489/