自定义AlertDialog不显示 [英] Custom AlertDialog not showing

查看:1213
本文介绍了自定义AlertDialog不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextViewer活动,即有​​一个按钮,当我点击它,我想和列表弹出一个AlertDialog。我跟着这个链接,但它不工作(不弹出)。我相信情况下是错误的。
我用下面的code:

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.resources);
    的ImageButton btnlist =(的ImageButton)findViewById(R.id.list);
    btnlist.setOnClickListener(新View.OnClickListener(){
             公共无效的onClick(视图v){              如果(Vars.bookchapter→1){
               最终的CharSequence [] =项目{红,绿,蓝};
               上下文mContext = getBaseContext();
               AlertDialog.Builder建设者=新AlertDialog.Builder(mContext);
               builder.setTitle(选择颜色);
               builder.setItems(项目,新DialogInterface.OnClickListener(){
                   公共无效的onClick(DialogInterface对话,诠释项){
                       Toast.makeText(getApplicationContext(),项目[项目],Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog警报= builder.create();
              }其他{
               //没有
              }
             }});
     }
        }


解决方案

您已经不叫<一个href=\"http://developer.android.com/reference/android/app/Dialog.html#show%28%29\"><$c$c>show()方法。做到这一点:

  AlertDialog警报= builder.create();
alert.show();

I have a TextViewer Activity , that has a button, and when I click on it I want to popup an AlertDialog with list. I followed this link but it does not work (no popup). I believe the context is wrong. I used the following code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources);
    ImageButton btnlist = (ImageButton)findViewById(R.id.list);
    btnlist.setOnClickListener(new View.OnClickListener() {
             public void onClick (View v){                  

              if (Vars.bookchapter>1){
               final CharSequence[] items = {"Red", "Green", "Blue"};
               Context mContext = getBaseContext();
               AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
               builder.setTitle("Pick a color");
               builder.setItems(items, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int item) {
                       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog alert = builder.create();
              }else{
               //Nothing
              }
             }});         
     }
        }

解决方案

You haven't called the show() method. Do this:

AlertDialog alert = builder.create();
alert.show();

这篇关于自定义AlertDialog不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆