本文實(shí)例講述了Android編程雙重單選對話框布局實(shí)現(xiàn)與事件監(jiān)聽方法。分享給大家供大家參考,具體如下:
首先是自定義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" android:padding="@dimen/dialog" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/choice1" android:textColor="@color/green" android:textSize="@dimen/text"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radiogroup1"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/kind" android:id="@+id/radio1" android:checked="true" /> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/attribute" android:id="@+id/radio2"/> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/choice2" android:textColor="@color/green" android:textSize="@dimen/text"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radiogroup2"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/area" android:id="@+id/radio3" android:checked="true"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/count" android:id="@+id/radio4"/> </RadioGroup></LinearLayout>
效果圖如下

引用布局的對話框和監(jiān)聽如下:
LayoutInflater layoutInflater = LayoutInflater.from(MainPlan.this);View self = layoutInflater.inflate(R.layout.multichoicedialog, null);//引入對話框布局final RadioGroup radioGroup1 = (RadioGroup) self.findViewById(R.id.radiogroup1);final RadioGroup radioGroup2 = (RadioGroup) self.findViewById(R.id.radiogroup2);new AlertDialog.Builder(MainPlan.this)//MainPlan是當(dāng)前activity .setView(self) .setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); } }) .setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (radioGroup1.getCheckedRadioButtonId() == R.id.radio1) { if (radioGroup2.getCheckedRadioButtonId() == R.id.radio3) { } else {//處理各種事件 } } else { if (radioGroup2.getCheckedRadioButtonId() == R.id.radio3) { } else { } } } }) .show();運(yùn)行之后的圖如下所示

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選