使用单选按钮列表创建自定义对话框

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

     /**
     * 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");

    }

此方法入到 onClick() 上:ImageButton

android:onClick="getISO"

但是我需要在带有单选按钮的对话框中删除此列表。可能首选项值应该已经在对话框中选择了。可能吗?


答案 1

最好和简单的方法...

void dialog(){

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
        //alt_bld.setIcon(R.drawable.icon);
        alt_bld.setTitle("Select a Group Name");
        alt_bld.setSingleChoiceItems(grpname, -1, new DialogInterface
                .OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(),
                        "Group Name = "+grpname[item], Toast.LENGTH_SHORT).show();
                dialog.dismiss();// dismiss the alertbox after chose option

            }
        });
        AlertDialog alert = alt_bld.create();
        alert.show();


///// grpname is a array where data is stored... 


    }

答案 2

从按钮呼叫。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>

enter image description here

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

编辑:要检索所选值,您必须为您的实现监听器作为:RadioButtonsetOnCheckedChangeListenerRadioGroup

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

                    }
                 }
            }
        });

推荐