0

我有一个包含两个 RadioButtons 的 RadioGroup 的自定义对话框。选择的 RadioButton 确定要设置的字符串。当我做

 Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG)
                            .show();

无论选择哪个 RadioButton,它都会将 checkedId 显示为 false,并且只有第二种情况将 String 设置为正确的值。以下是我目前的代码:

AlertDialog.Builder customDialog = new AlertDialog.Builder(NewFile.this);
        LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.jquery_dialog, null);
        final EditText idTxt = (EditText) view.findViewById(R.id.idName);
        final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk);
        final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk);
        final RadioGroup group = (RadioGroup) view
                .findViewById(R.id.jqmNavigation);
        customDialog.setView(idTxt);
        customDialog.setView(headerChk);
        customDialog.setView(footerChk);
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                case R.id.ol: // First RadioButton
                    Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG)
                            .show();
                    jqMobNavbar = "<div data-role=\"navbar\">\n" + "        <ol>\n"
                            + "         <li><a href=\"#\">Page 1</a></li>\n"
                            + "         <li><a href=\"#\">Page 2</a></li>\n"
                            + "         <li><a href=\"#\">Page 3</a></li>\n"
                            + "     <ol>\n" + " </div>\n";
                    break;
                case R.id.ul: // Second RadioButton
                    Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG)
                            .show();
                    jqMobNavbar = "<div data-role=\"navbar\">\n" + "        <ul>\n"
                            + "         <li><a href=\"#\">Page 1</a></li>\n"
                            + "         <li><a href=\"#\">Page 2</a></li>\n"
                            + "         <li><a href=\"#\">Page 3</a></li>\n"
                            + "     <ul>\n" + " </div>\n";
                    break;

                }

            }
        });

和我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.androidwebtoolkit.TransparentPanel
        android:id="@+id/transparentPanel1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="0"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/imageView1"
            android:text="jBuilder"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="11dp"
            android:src="@drawable/jquerymobileicon" />

    </com.androidwebtoolkit.TransparentPanel>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0"
        android:text="ID:"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/idName"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <CheckBox
        android:id="@+id/headerChk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0"
        android:text="Header" />

    <CheckBox
        android:id="@+id/footerChk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0"
        android:text="Footer" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingLeft="10dp"
        android:text="Navigation"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RadioGroup
        android:id="@+id/jqmNavigation"
        android:layout_width="312dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/ol"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ordered List" />

        <RadioButton
            android:id="@+id/ul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unordered List" />

    </RadioGroup>

</LinearLayout>

我也试过

group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonID = group.getCheckedRadioButtonId();
                View radioButton = group.findViewById(radioButtonID);
                int idx = group.indexOfChild(radioButton);
                switch (idx) {
                case 1:
                    Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG)
                            .show();
                    jqMobNavbar = "<div data-role=\"navbar\">\n" + "        <ol>\n"
                            + "         <li><a href=\"#\">Page 1</a></li>\n"
                            + "         <li><a href=\"#\">Page 2</a></li>\n"
                            + "         <li><a href=\"#\">Page 3</a></li>\n"
                            + "     <ol>\n" + " </div>\n";
                    break;
                case 2:
                    Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG)
                            .show();
                    jqMobNavbar = "<div data-role=\"navbar\">\n" + "        <ul>\n"
                            + "         <li><a href=\"#\">Page 1</a></li>\n"
                            + "         <li><a href=\"#\">Page 2</a></li>\n"
                            + "         <li><a href=\"#\">Page 3</a></li>\n"
                            + "     <ul>\n" + " </div>\n";
                    break;

                }

            }
        });

检查所选 RadioButton 值的正确方法是什么?

4

2 回答 2

1

你是什​​么意思checkedId是假的?这是一个整数,不能为假。如果您的任何一个 case 语句都有效,这意味着 checkedId 是它应该是的 id 值。

如果您将第一个 RadioButton 设置为在 XML 中选中,则在按下另一个按钮之前不会调用您的任何案例。或者,您也可以 在每个按钮上设置:http: //developer.android.com/reference/android/widget/CompoundButton.OnCheckedChangeListener.html 。

于 2013-04-24T21:10:30.167 回答
0

它返回false是因为它是一个Integer. 尝试使用:

Toast.makeText(getApplicationContext(), Integer.toString(checkedId), Toast.LENGTH_SHORT).show();
于 2013-07-14T17:56:18.520 回答