现在,在我的点击事件中,我注意到有时候我的调试器上有多个对话框可能同时出现轻微的onClick。
我们如何解决它,所以有办法让它只显示1 AlertDialog?
代码:很标准。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(class1.this, class2.class);
startActivity(i);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create()
答案 0 :(得分:1)
使用isShowing()
方法,您可以查看是否显示警告对话框。
和onClick事件每次单击时都会创建新对话框,因此检查对话框是否为null然后创建对话框(如果不为null)然后选中.isShowing()
所以,
AlertDialog alert=null;//declare as a global ..mind that not in your onClick method
if(null=alert){
alert = new AlertDialog.Builder(this).create();
}
if(!alert.isShowing()){
//do stuff here is dialog is showing here...
}
答案 1 :(得分:0)
public void onClick(View v) {
send();
alertdialog();
alertdialog1();
}
//first
void alertdialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
/*.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});*/
AlertDialog alert = builder.create();
alert.show();
}
//////sexcond
void alertdialog1(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
/*.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});*/
AlertDialog alert = builder.create();
alert.show();
}
答案 2 :(得分:0)
private AlertDialog mDialog;
public void fillDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(class1.this, class2.class);
startActivity(i);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
mDialog = builder.create()
}
public void showDialog(){
if (mDialog == null) createDialog();
if (mDialog.isShowing()) return;
mDialog.show();
}
答案 3 :(得分:0)
如果您快速单击该按钮,则会对所有点击进行排队并逐个处理。但你想要的是在第一次点击后忽略其余的点击。
boolean isClickable = true;
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (isClickable)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Go to the next screen?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener()
{
public void onClick(
DialogInterface dialog, int id)
{
Intent i = new Intent(class1.this,
class2.class);
startActivity(i);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener()
{
public void onClick(
DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
}
isClickable = false;
}
});