用户单击时,警报对话框消失了 [英] Alert Dialog was disappearing when user clicks out side
问题描述
大家好,我有一个带有两个文本框的警报框,这里的问题是,当用户单击该弹出窗口之外时,警报对话框消失了,或者当用户单击确定"按钮时,警报对话框也消失了.
Hi everyone i have a alert box with two text box's, and here the problem is the alert dialog was disappearing when user clicks outside of that pop up or the Alert dialog is disappearing when user clicks Ok button too.
所以请在这方面帮助我 预先感谢...
So please help me in this regards Thanks in advance...
final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Sign In Failed");
final EditText input1=new EditText(MainActivity.this);
final EditText input2=new EditText(MainActivity.this);
input1.setHint("eNTER name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
builder.setMessage("Invalid username or password");
linearLayout.addView(input1);
linearLayout.addView(input2);
builder.setView(linearLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
推荐答案
因为默认情况下它是可取消的
because by default it is Cancelable
在builder.setView(linearLayout)之后添加它-
Add this after builder.setView(linearLayout) -
builder.setCancelable(false);
更新
按照下面的代码段-
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
单击肯定按钮(确定")时,您正在设置dialog.cancel()
请勿执行此操作,而是应根据需要对肯定按钮单击进行一些操作.
on positive button ("OK") click, you are setting dialog.cancel()
Don't do this, you should set some action as you required on positive button click.
查看此内容:
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); // close the current dialog
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Perform any Intent Action or perform validation as you want
}
});
更新2
只需复制&粘贴下面的代码-完美地工作
Just copy & paste below code - working perfectly
final EditText input1 = new EditText(MainActivity.this);
final EditText input2 = new EditText(MainActivity.this);
input1.setHint("Enter name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(input1);
linearLayout.addView(input2);
final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Sign In Failed")
.setCancelable(false)
.setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
builder.show();
builder.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (input1.length() <= 0) {
Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
builder.dismiss();
}
}
});
这篇关于用户单击时,警报对话框消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!