【问题标题】:Custom dialog with with a Contentview that has a layout with 2 buttons - Listener?带有 2 个按钮布局的 Contentview 的自定义对话框 - 侦听器?
【发布时间】:2012-09-05 03:49:44
【问题描述】:

我正在从列表视图中打开一个警报对话框(3 个选项和 2 个按钮)

我有这个:

                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);// ass
            inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.custom_dialog,null);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            builder.setView(layout);
            builder.setTitle("Select");
            builder.setCancelable(true);
            builder.setSingleChoiceItems(tonos, -1,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Log.d(TAG, "Option: " + tonos[item]);
                    tone=tonos[item];


                    Button botonOK = (Button) findViewById(R.id.botOK);

                    try {
                        botonOK.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {
                                Log.d(TAG,"button ok");
                                }
                            });

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Log.d(TAG, "Exception " + e);
                    }



                }
            });


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




    }

第二个 onClick 的参数是否正确? 我正在疯狂测试,但我找不到解决方案。

【问题讨论】:

  • 代码中没有自定义对话框布局,这里
  • 你在哪一行得到 NullPointer?
  • @Carnal 在这里 --> botonOK.setOnClickListener(new OnClickListener()

标签: android button dialog listener


【解决方案1】:
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_dialog,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        Button botonOK = (Button) layout.findViewById(R.id.botOK);
        botonOK.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.d(TAG,"button ok");
            }
        });

        builder.setView(layout);
        builder.setTitle("Select");
        builder.setCancelable(true);
        builder.setSingleChoiceItems(tonos, -1,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Log.d(TAG, "Option: " + tonos[item]);
                tone=tonos[item];
            }
        });

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

【讨论】:

  • getCurrentFocus() 有什么用?如果要进入 AlertDialog ,您不能使用 null 吗?
  • @Carnal 不起作用.. PhoneLayoutInflater(LayoutInflater).inflate(int, ViewGroup, boolean) line: 322) 我把监听器放在这里:builder.setSingleChoiceItems(tones, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Log.d(TAG, "Option" + tonos[item]); HERE} });没关系?????谢谢!
  • @Héctor Ortiz - 什么不起作用?似乎您没有按应有的方式膨胀,您得到了什么例外?
  • @Carnal NullPointerException :(
  • @Héctor Ortiz - 编辑您的问题以显示您使用的代码以及您在代码中获得 NullPointer 的位置,这样更容易查看发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2012-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多