我创建了一个由 2 EditText(用户名、密码)和一个按钮表示的登录应用程序。当我单击登录时,它当然会检查信息,如果错误,它会打印一个带有重试按钮的对话框。当我回到登录活动时,旧的用户名和密码文本仍然存在,我想重置它。我尝试在 onPause() 方法上执行此操作,因为当错误对话框弹出时会生成 onPause() 方法但我失败了。
任何的想法?谢谢。
编辑:根据您的建议,我决定重新安排我的代码并按应有的方式声明 EditText。结果还是一样=\
public class MainActivity extends Activity
{
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.username = (EditText)findViewById(R.id.username);
this.password = (EditText)findViewById(R.id.password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onPause()
{
this.username.setText("");
this.password.setText("");
super.onPause();
}
public void showMsg(View view)
{
String user = this.username.getText().toString();
String pass = this.password.getText().toString();
if(user.equals("a") && pass.equals("a"))
{
Intent i1 = new Intent(this,DisplayMessageActivity.class);
i1.putExtra(EXTRA_MESSAGE, user);
startActivity(i1);
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Error!");
builder.setMessage(R.string.failure);
builder.setPositiveButton("Try again", null);
builder.show();
}
}
}