2010-10-25 79 views
25

主题有点说这一切..我要求从用户的PIN码,如果他们进入它,点击确定正面按钮和PIN不正确我想显示一个敬酒,但保持对话框打开。此刻它会自动关闭..当然,这是非常微不足道的事情,但无法找到答案。如何在点击按钮onclick后保持alertdialog打开?

谢谢..

+0

我目前只是回顾我的功能来创建并显示一个对话框,但它感觉像是浪费资源,当我只需要通知对话框不要自行消除... – Dave 2010-10-25 16:11:26

+2

重复http://stackoverflow.com/questions/2620444 /如何防止对话框从关闭时点击按钮/ 9523257 – ccpizza 2012-12-29 13:43:36

回答

12

构建与属性Android上的EditText一个自定义对话框:密码=“真”按钮,然后手动设置的onClick监听器按钮,并明确选择它做什么。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:minWidth="180dip" 
     android:digits="1234567890" 
     android:maxLength="4" 
     android:password="true"/> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/Accept" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Accept"/> 

    </LinearLayout> 
</LinearLayout> 

然后,当你希望它弹出:

final Dialog dialog = new Dialog(RealizarPago.this); 
dialog.setContentView(R.layout.custom_dialog); 
dialog.setTitle("PIN number:"); 
dialog.setCancelable(true); 

Button button = (Button) dialog.findViewById(R.id.Accept); 
button.setOnClickListener(new OnClickListener() { 
@Override 
    public void onClick(View v) { 
     if(password_wrong){ 
      // showToast 
     } else{ 
      dialog.dismiss(); 
      // other stuff to do 
     } 
    } 
}); 

dialog.show(); 
+0

非常感谢,所以对话框除非指定了自定义布局,否则会自动关闭onclick? – Dave 2010-10-25 17:20:28

+0

我确实相信,我可能会误解,但是我发现自定义对话更容易,而不是尝试让另一个人工作,并且完全控制它的外观和行为。 Plz将答案标记为接受,如果它适合你。 – blindstuff 2010-10-25 17:26:27

1

只要继续使用你已经拥有的对话框,只是把一个if子句中的onClick()说

if(pin_check_method){ //pin_check_method should be a boolean returned method 
    //close the Dialog, then continue 
    } 
    else{ 
    //dont put the dialog.dismiss() in here, put instead 
    Toast.makeText(getApplicationContext(),"Invalid pin, please try again",Toast.LENGTH_LONG).show(); 
} 

现在,要使用此代码,只需调用text.setText(“”);并把文本,你想在这里 常见的错误是,当你键入:

TextView text = (TextView) findViewById(R.id.dialog); 

你错过,它需要实际上是

dialog.findViewById 

,这是不管的名字是什么在我的例子中,对话框恰好是同名的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout_root" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 

    <TextView android:id="@+id/text" 
       android:layout_height="wrap_content" 
       android:textColor="#FFF" 
       android:layout_centerHorizontal="true" 
       android:layout_width="wrap_content"/> 



    <Button android:text="Continue" 
      android:id="@+id/Button01" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:layout_below="@+id/text"> 
      </Button> 

</RelativeLayout> 
+0

这就是我正在做的事情,但是即使没有dismiss()调用,对话框也会自动关闭。 – Dave 2010-10-25 17:19:51

+0

好吧,看着它后,它看起来像自定义对话框将是你最好的选择..我会编辑我的答案,你会发现有用的。我实际上是在我的应用程序中使用它。不要忘记标记为接受,如果你使用我的答案:) – Samuel 2010-10-26 03:52:22

4

您可以设置一个OnClickListener如下,以保持对话框打开:

public class MyDialog extends AlertDialog { 
    public MyDialog(Context context) { 
     super(context); 
     setMessage("Hello"); 
     setButton(AlertDialog.BUTTON_POSITIVE, "Ok", (new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // this will never be called 
      } 
     }); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (ok) { 
        // do something 
        dismiss(); 
       } else { 
        Toast.makeText(getContext(), "when you see this message, the dialog should stay open", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 
} 
36

你并不需要创建一个自定义类。您可以为AlertDialog注册一个View.OnClickListener。这个监听器不会关闭AlertDialog。这里的诀窍是你需要在显示对话框后注册监听器,但是它可以在OnShowListener中完成。您可以使用附件布尔变量来检查,如果这已经这样做了,它只会做一次:这个解决方案的

/* 
    * Prepare the alert with a Builder. 
    */ 
    AlertDialog.Builder b = new AlertDialog.Builder(this); 

    b.setNegativeButton("Button", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) {} 
    }); 
    this.alert = b.create(); 

    /* 
    * Add an OnShowListener to change the OnClickListener on the 
    * first time the alert is shown. Calling getButton() before 
    * the alert is shown will return null. Then use a regular 
    * View.OnClickListener for the button, which will not 
    * dismiss the AlertDialog after it has been called. 
    */ 

    this.alertReady = false; 
    alert.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 
      if (alertReady == false) { 
       Button button = alert.getButton(DialogInterface.BUTTON_NEGATIVE); 
       button.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         //do something 
        } 
       }); 
       alertReady = true; 
      } 
     } 
    }); 

部分由http://groups.google.com/group/android-developers/browse_thread/thread/fb56c8721b850124#

+0

这个解决方案为我工作,谢谢 – 2011-10-26 15:43:07

+1

不幸的是,setOnShowListener只适用于API级别8和更高版本。为了支持API 7,我使用链接页面中提供的方法:将一个无操作监听器传递给setNegativeButton,调用show(),然后获取对该按钮的引用,并使用完整监听器调用setOnClickListener()。 – Travis 2012-02-05 04:57:07

+0

对于好的和有帮助的答案+1,这个解决方案适用于我。 – rajpara 2012-10-03 06:19:01

0

同样的问题,我在FragmentDialog提供。这是我的犯罪/优雅的解决方案: 删除对话框中的所有按钮(正面,负面,中性)。从xml.eg与添加您的按钮:

<LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/button_cancel" 
      style="@style/Widget.AppCompat.Button.Borderless.Colored" 
      android:text="@android:string/cancel" 
      android:layout_gravity="left" 
      /> 
     <Button 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:id="@+id/button_ok" 
      style="@style/Widget.AppCompat.Button.Borderless.Colored" 
      android:text="@android:string/ok" 
      android:layout_gravity="right" 
      /> 
    </LinearLayout> 

然后在你的代码处理:

view.findViewById(R.id.button_ok).setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view2) { 
        if (wannaClose) 
         dismiss(); 
        else 
         //do stuff without closing! 
       } 
      }); 

,其中的观点是分配给对话框的看法!

0

试试这个:

final AlertDialog alertDialog = new AlertDialog.Builder(context) 
     .setView(v) 
     .setTitle(R.string.my_title) 
     .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick 
     .setNegativeButton(android.R.string.cancel, null) 
     .create(); 

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { 

    @Override 
    public void onShow(DialogInterface dialog) { 

     Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // TODO Do something 

      } 
     }); 
    } 
}); 
alertDialog.show(); 

来源:Prevent Alertdialog from closing after button click


希望这有助于!祝你好运!

相关问题