我有一个带有两个RadioButtons的RadioGroup的自定义Dialog。选择的RadioButton确定要设置的String。当我做的时候
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值的正确方法是什么?
答案 0 :(得分:1)
你是什么意思checkId是假的?这是一个int,不能是假的。如果你的任何一个case语句都有效,那就意味着checkedId是它应该是的id值。
如果您将第一个RadioButton设置为XML格式,则在按下另一个按钮之前,不会调用任何情况。或者,您也可以设置:http://developer.android.com/reference/android/widget/CompoundButton.OnCheckedChangeListener.html 在每个按钮上。
答案 1 :(得分:0)
它返回false
,因为它是Integer
。尝试使用:
Toast.makeText(getApplicationContext(), Integer.toString(checkedId), Toast.LENGTH_SHORT).show();