如果edittext为空,如何禁用按钮alertdialog?

时间:2018-03-10 10:55:38

标签: java android

我有alertdialog输入文本,我想如果edittext是空的,请获取toast" plz fill field"如果用户单击肯定按钮并且edittext不为空,请继续打开活动

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();

}

2 个答案:

答案 0 :(得分:1)

您可以按如下方式检查文本的长度

if(name.getText().toString().trim().length() == 0 ){
  //empty
}else{
  //non-empty
}

答案 1 :(得分:0)

呃......这不是pepole为你编写代码的地方,但是......

    .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();
           }
        }
    })