我有一个包含两个 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 值的正确方法是什么?