创建一个带有单选按钮列表的自定义对话框 [英] Create a custom dialog with radio buttons list

查看:33
本文介绍了创建一个带有单选按钮列表的自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法,其中有一个值列表:

I've got a method in which i have a list of values:

     /**
     * ISO
     * */
    public void getISO(View view) {
        // Open dialog with radio buttons
        List<String> supported_isos = preview.getSupportedISOs();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        String current_iso = sharedPreferences.getString(MainActivity.getISOPreferenceKey(), "auto");

    }

此方法被注入到 ImageButton 的 onClick() 上:

This method is enjected on onClick() of a ImageButton:

android:onClick="getISO"

但我需要在带有单选按钮的对话框中显示此列表.可能已经在对话框中选择了首选项值.这可能吗?

But i need to rapresent this list in a dialog with radio buttons. Possibly the preference values should be already selected in the dialog.. Is it possible?

推荐答案

从按钮调用 showRadioButtonDialog().

这只是一个例子:

private void showRadioButtonDialog() {

 // custom dialog

  final Dialog dialog = new Dialog(this);
  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  dialog.setContentView(R.layout.dialog_layout);
  List<String> stringList=new ArrayList<>();  // here is list
      for(int i=0;i<2;i++) {
          if (i==0){
              stringList.add("Number Mode");
          }else {
              stringList.add("Character Mode");
          }

      }

      RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radioGroup);

      for(int i=0;i<stringList.size();i++){
            RadioButton rb=new RadioButton(this); // dynamically creating RadioButton and adding to RadioGroup.
            rb.setText(stringList.get(i));
            rg.addView(rb);
      }
}

您的布局视图可能是:radiobutton_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
      <RadioGroup
          android:id="@+id/radio_group"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="center_vertical"
          android:orientation="vertical">
    </RadioGroup>
 </LinearLayout>

注意:您可以自定义对话框视图(如设置标题、消息等)

Note: you can customize your dialog view (like setting title, message etc.)

要检索所选 RadioButton 的值,您必须为您的 RadioGroup 实现 setOnCheckedChangeListener 侦听器:

To retrieving value of the selected RadioButton you have to implement setOnCheckedChangeListener listener for your RadioGroup as :

 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                 int childCount = group.getChildCount();
                 for (int x = 0; x < childCount; x++) {
                    RadioButton btn = (RadioButton) group.getChildAt(x);
                    if (btn.getId() == checkedId) {
                         Log.e("selected RadioButton->",btn.getText().toString());

                    }
                 }
            }
        });

这篇关于创建一个带有单选按钮列表的自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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