Java 如果edittext为空,如何禁用按钮alertdialog?
我有alertdialog输入文本,我想如果edittext为空,如果用户单击肯定按钮,则获取toast“plz填充字段”,如果edittext不为空,则继续打开活动Java 如果edittext为空,如何禁用按钮alertdialog?,java,android,Java,Android,我有alertdialog输入文本,我想如果edittext为空,如果用户单击肯定按钮,则获取toast“plz填充字段”,如果edittext不为空,则继续打开活动 LayoutInflater layoutInflaterAndroid = LayoutInflater.from(newapp.this); View view = layoutInflaterAndroid.inflate(R.layout.input_dialog, null); AlertDialog.Builder a
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(newapp.this);
View view = layoutInflaterAndroid.inflate(R.layout.input_dialog, null);
AlertDialog.Builder adb = new AlertDialog.Builder(newapp.this);
adb.setView(view);
name = (EditText) view.findViewById(R.id.name);
password = (EditText) view.findViewById(R.id.password);
adb
.setCancelable(false)
.setTitle(getResources().getString(R.string.info))
.setIcon(R.mipmap.ic_launcher)
.setPositiveButton(getResources().getString(R.string.start), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
Intent cc = new Intent(StartActivity.this, news.class);
startActivity(cc);
}
})
.setNegativeButton(getResources().getString(R.string.close),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
dialogBox.cancel();
}
});
AlertDialog alertDialogAndroid = adb.create();
alertDialogAndroid.show();
}
您可以按如下方式检查文本的长度
if(name.getText().toString().trim().length() == 0 ){
//empty
}else{
//non-empty
}
嗯。。。这不是人们为你写代码的地方,但是
.setPositiveButton(getResources().getString(R.string.start), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
if(!TextUtils.isEmpty(name.getText().toString())
Intent cc = new Intent(StartActivity.this, news.class);
startActivity(cc);
} else {
Toast.makeText(context, "Message", Toast.LENGTH_LONG).show();
}
}
})
尝试以下操作::
TextUtils.isEmpty(值)代码>我尝试过这个,但是如果edittext为空,用户可以单击肯定按钮并打开Activity,这是一个错误的答案。按下对话框上的任何按钮都将关闭窗口。这是一个错误的答案。按下对话框上的任何按钮都将关闭窗口。但是,用户希望在EditText中写入某些内容之前一直停留在对话框上。