我有一个TextViewer Activity,它有一个按钮,当我单击它时,我想弹出一个带有列表的AlertDialog。我遵循了这个link,但是它不起作用(没有弹出窗口)。我认为背景是错误的。
我使用以下代码:

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

最佳答案

您尚未调用show()方法。做这个:

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

关于android - 自定义AlertDialog不显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3160231/

10-12 06:10