【问题标题】:Using Radio Buttons within a Dialog in Android app在 Android 应用程序的对话框中使用单选按钮
【发布时间】:2012-02-24 20:11:51
【问题描述】:

我有一个 Android 应用程序,我想为用户提供使用不同字段订购表格的选择。我使用了一个带有三个单选按钮的对话框来提供每种排序类型的选择。

下面是对话框代码中声明单选按钮的代码

        LayoutInflater layoutInflater = (LayoutInflater)
        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View layout = layoutInflater.inflate(R.layout.lectindex_dialog, (ViewGroup) findViewById(R.id.lect_index));

        builder.setView(layout);

        // Now configure the AlertDialog
        builder.setTitle(R.string.exindex_title);                       

        final RadioButton radio_date = (RadioButton) findViewById(R.id.RBdate);
        final RadioButton radio_loctn = (RadioButton) findViewById(R.id.RBloctn);
        final RadioButton radio_stream = (RadioButton) findViewById(R.id.RBstream);        
        radio_date.setOnClickListener(radio_listener);
        radio_loctn.setOnClickListener(radio_listener);
        radio_stream.setOnClickListener(radio_listener);

        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // We forcefully dismiss and remove the Dialog, so it cannot be used again (no cached info)
                LecturesActivity.this.removeDialog(SELINDEX_DIALOG_ID);
            }
        });

radio_listener 的代码是这样单独声明的

        private OnClickListener radio_listener = new OnClickListener() {
          public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            Toast.makeText(LecturesActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
          }
        }; 

似乎一切正常,但 radio_listener 的代码从未被使用过,为什么?

【问题讨论】:

    标签: android eclipse radio-button onclicklistener


    【解决方案1】:

    您是否尝试过使用 RadioGroup 而不是单个单选按钮?您将更好地控制他们的行为,减少 IMO 的头痛

    【讨论】:

      【解决方案2】:

      错误和 radio_listener 从未被调用的原因是它在尝试使用 radio_date 时抛出错误,因为它没有正确声明。当我将声明更改为最终 RadioButton radio_date = (RadioButton) layout.findViewById(R.id.RBdate); layout 是对话框的 id,代码运行良好。

      【讨论】:

        猜你喜欢
        • 2012-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多