我有一个警报对话框,其中包含5个按钮的Radiogroup。按下确定后,应用程序崩溃我是否选择了单选按钮。为Radiogroup尝试了不同的方法,但都失败了。如果有人可以帮助我,我会很感激。
PS。我有一个调用此警报对话框活动的开关案例。 colorpickdialog();
这是我的警报对话框XML:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radiosizeg"
android:layout_below="@+id/bgadddialogsize"
android:layoutDirection="rtl"
android:gravity="start"
android:layout_gravity="start"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:text="@string/sizesmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sizesmall"/>
<RadioButton
android:text="@string/sizemed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sizemed" />
<RadioButton
android:text="@string/sizelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sizelar" />
<RadioButton
android:text="@string/sizefill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sizefill" />
<RadioButton
android:text="@string/sizecustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sizecustom" />
</RadioGroup>
这是我的MainActivity.class:
public void colorpickdialog() {
// get prompts.xml view
final RadioGroup radiosizeg = (RadioGroup) findViewById(R.id.radiosizeg);
LayoutInflater layoutInflater2 = LayoutInflater.from(MainActivity.this);
View promptView2 = layoutInflater2.inflate(R.layout.colorpickdialog, null);
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder2.setView(promptView2);
// setup a dialog window
alertDialogBuilder2.setCancelable(false)
.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
radiosizeg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int id = radiosizeg.getCheckedRadioButtonId();
switch (id) {
case R.id.sizesmall:
Toast.makeText(getApplicationContext(),"A", Toast.LENGTH_SHORT).show();
break;
case R.id.sizemed:
Toast.makeText(getApplicationContext(),"B", Toast.LENGTH_SHORT).show();
break;
case R.id.sizelar:
Toast.makeText(getApplicationContext(),"C", Toast.LENGTH_SHORT).show();
break;
case R.id.sizefill:
Toast.makeText(getApplicationContext(),"D", Toast.LENGTH_SHORT).show();
break;
case R.id.sizecustom:
Toast.makeText(getApplicationContext(),"E", Toast.LENGTH_SHORT).show();
break;
default:
onCheckedChanged(radioGroup, i);
break;
}
}
});
}
})
.setNegativeButton(getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
错误堆栈跟踪
10-27 20:56:53.753 4312-4327/? E/ReportTools: This is not beta user build
10-27 20:56:53.959 4312-7462/? E/HsmCoreServiceImpl: onTransact in code is: 102
10-27 20:56:54.025 13521-13539/? E/HwLauncher: SettingsEx , no such field.
10-27 20:56:54.088 8152-8242/? E/PackageLogInfoManager: checkPackageLogState, cr: android.app.ContextImpl$ApplicationContentResolver@4d74931, packageNames: null
答案 0 :(得分:1)
好吧,我有一个简单的方法来处理 AlertDialog 框中的 Radiogroup: 问题出现在每次创建对话框时,孩子都会获得额外的 ID:
Xml 代码:Just Radio 组部分:
<RadioGroup
android:id="@+id/rg_Payment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_25sdp"
android:layout_marginHorizontal="@dimen/_25sdp"
android:orientation="horizontal"
android:weightSum="3">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Paid"
android:textColor="@color/green"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Trail"
android:textColor="@color/orange"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blocked"
android:textColor="@color/colorRed"/>
</RadioGroup>
和代码:
public void updateStatus() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
LayoutInflater inflater = LayoutInflater.from(mActivity);
View dialogView = inflater.inflate(R.layout.status_popup, null);
builder.setView(dialogView);
RelativeLayout rrSubmit = dialogView.findViewById(R.id.rr_submit);
RelativeLayout rr_lg_cancel = dialogView.findViewById(R.id.rr_lg_cancel);
RadioGroup rg_Payment = dialogView.findViewById(R.id.rg_Payment);
final AlertDialog alert = builder.create();
paymentType = "";
rg_Payment.setOnCheckedChangeListener((radioGroup, id) -> {
int position = radioGroup.getCheckedRadioButtonId() % radioGroup.getChildCount();
setLog("MyRadioChoice", "Choice" + position);
switch (position) {
case 0:
paymentType = "Blocked";
break;
case 1:
paymentType = "Paid";
break;
case 2:
paymentType = "Trail";
break;
default:
paymentType = "";
}
});
rrSubmit.setOnClickListener(v -> {
//do something
});
rr_lg_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});
alert.show();
}
这里使用下面的方法来解决动态创建单选按钮的问题
int position = radioGroup.getCheckedRadioButtonId()% radioGroup.getChildCount();
答案 1 :(得分:0)
我发现了闷热。
问题是无线电组在实际视图中定义:
final RadioGroup radiosizeg = (RadioGroup) findViewById(R.id.radiosizeg);
然而,我把观点夸大了:
LayoutInflater layoutInflater2 = LayoutInflater.from(MainActivity.this);
View promptView2 = layoutInflater2.inflate(R.layout.colorpickdialog, null);
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder2.setView(promptView2);
因此,解决方案是在promptView2中定义视图:
final RadioGroup radiosizeg = (RadioGroup) promptView2.findViewById(R.id.radiosizeg)
大();