Android if语句中的AlertDialog不';t show()
我有以下代码:Android if语句中的AlertDialog不';t show(),android,show,android-alertdialog,Android,Show,Android Alertdialog,我有以下代码: public void button_login(View view) { // Instantiate an AlertDialog.Builder with its constructor AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickLi
public void button_login(View view) {
// Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { /* User clicked OK button */ }
});
// Preserve EditText values.
EditText ET_username = (EditText) findViewById(R.id.username);
EditText ET_password = (EditText) findViewById(R.id.password);
String str_username = ET_username.toString();
String str_password = ET_password.toString();
// Intercept missing username and password.
if(str_username.length() == 0) {
builder.setMessage(R.string.hint_username_empty);
AlertDialog dialog = builder.create();
dialog.show();
}
}
我有一个活动,包括两个EditText视图和一个按钮。当我单击按钮时,将调用显示的方法
我的问题是:
AlertDialog没有显示
当我将“创建和显示”放在开头时,如下所示:
// Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { /* User clicked OK button */ }
});
builder.setMessage(R.string.hint_username_empty);
AlertDialog dialog = builder.create();
dialog.show();
// Preserve EditText values.
EditText ET_username = (EditText) findViewById(R.id.username);
EditText ET_password = (EditText) findViewById(R.id.password);
String str_username = ET_username.toString();
String str_password = ET_password.toString();
// Intercept missing username and password.
if(str_username.length() == 0) {
}
}
然后出现对话框
知道为什么对话框没有首先显示吗?这是因为
EditText.toString()
不返回文本。改用EditText.getText().toString()
。您还应该在if语句之前和中添加一些日志语句,以便更好地了解正在发生的事情。这是因为EditText.toString()
不返回文本。改用EditText.getText().toString()
。您还应该在if语句之前和中添加一些日志语句,以便更好地了解正在发生的事情。这是因为EditText.toString()
不返回文本。改用EditText.getText().toString()
。您还应该在if语句之前和中添加一些日志语句,以便更好地了解正在发生的事情。这是因为EditText.toString()
不返回文本。改用EditText.getText().toString()
。您还应该在if语句之前和中添加一些日志语句,以便更好地了解正在发生的事情。问题在于以下几行:
String str_username = ET_username.toString();//is never empty
String str_password = ET_password.toString();//is never empty
试试下面的代码,应该可以
String str_username = ET_username.getText().toString();
String str_password = ET_password.getText().toString();
问题在于以下几行:
String str_username = ET_username.toString();//is never empty
String str_password = ET_password.toString();//is never empty
试试下面的代码,应该可以
String str_username = ET_username.getText().toString();
String str_password = ET_password.getText().toString();
问题在于以下几行:
String str_username = ET_username.toString();//is never empty
String str_password = ET_password.toString();//is never empty
试试下面的代码,应该可以
String str_username = ET_username.getText().toString();
String str_password = ET_password.getText().toString();
问题在于以下几行:
String str_username = ET_username.toString();//is never empty
String str_password = ET_password.toString();//is never empty
试试下面的代码,应该可以
String str_username = ET_username.getText().toString();
String str_password = ET_password.getText().toString();
非常感谢您的解决方案和建议。我将查看日志库:)非常感谢您提供的解决方案和建议。我将查看日志库:)非常感谢您提供的解决方案和建议。我将查看日志库:)非常感谢您提供的解决方案和建议。我将查看日志库:)