我尝试实现RadioGroup视图并将其放在DialogFragment中,但出了点问题。我已经按照本教程
了http://developer.android.com/guide/topics/ui/controls/radiobutton.html
而是Activity,view在DialogFragment中初始化:
public class MenuDialog extends DialogFragment {
public MenuDialog(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout, container);
this.getDialog().setTitle("title");
return view;
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio1:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio2:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio3:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
case R.id.radio4:
if(checked) {
Log.i("MENU", ((RadioButton)view).getText());
}
break;
}
this.getDialog().dismiss();
}
}
的xml:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/radio_group" >
<RadioButton
android:id="@+id/radio1"
android:text="20" />
<RadioButton
android:id="@+id/radio2"
android:text="40" />
<RadioButton
android:id="@+id/radio3"
android:text="60" />
<RadioButton
android:id="@+id/radio4"
android:text="80" />
</RadioGroup>
onRadioButtonClicked监听器它以与教程相同的方式实现。现在,当启动对话框时,如果我点击radiobutton,我会收到此错误:
09-27 09:41:08.018: E/AndroidRuntime(7447): FATAL EXCEPTION: main
09-27 09:41:08.018: E/AndroidRuntime(7447): java.lang.IllegalStateException: Could not
find a method onRadioButtonClicked(View) in the activity class
android.view.ContextThemeWrapper for onClick handler on view class
android.widget.RadioButton with id 'radio1'
怎么了?
答案 0 :(得分:3)
我宁愿在片段内部实现click处理程序并动态添加侦听器(通过java而不是XML)。 所以我会做这样的事情:
public class MenuDialog extends DialogFragment implements OnClickListener{
[...]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout, container);
view.findViewById(R.id.radio1).setOnClickListener(this);
view.findViewById(R.id.radio2).setOnClickListener(this);
//and so on [...]
this.getDialog().setTitle("title");
return view;
}
@Override
public void onClick(View v){
[...]
}
答案 1 :(得分:1)
问题是如果你为xml文件中的任何View
指定了onClick
回调属性,它应该位于Activity
代码内,而不是代码片段
答案 2 :(得分:1)
您需要将侦听器与无线电组链接,您可以这样做
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout, container);
this.getDialog().setTitle("title");
view.findViewById(R.id.radio_group).setOnCheckedChangeListener(...);
return view;
}
答案 3 :(得分:0)
为您的片段实现RadioButton.OnCheckedChangeListener。根据你的单选按钮setOnCheckedChangeListener。
public class MenuDialog extends DialogFragment implements RadioButton.OnCheckedChangeListener{
//..
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout, container);
this.getDialog().setTitle("title");
((RadioButton)view.findViewById(R.id.radio1)).setOnCheckedChangeListener(this);
((RadioButton)view.findViewById(R.id.radio2)).setOnCheckedChangeListener(this);
// and so on
return view;
}
@Override
public void onCheckedChanged(CompoundButton v, boolean checked) {
switch(v.getId()) {
case R.id.radio1:
// todo
break;
case R.id.radio2:
// todo
break;
// and so on..
}
this.getDialog().dismiss();
}
BTW删除片段中的onRadioButtonClicked()和&#34; android:onClick =&#34; onRadioButtonClicked&#34;&#34;按照http://developer.android.com/guide/topics/ui/controls/radiobutton.html
中的说明操作xml中的RadioButton答案 4 :(得分:0)
也许我有点晚了,但发现了这个解决方案。适用于 API 23
YourDialogFragment.kt
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return requireActivity().let {
AlertDialog.Builder(it)
.setTitle("Your title here")
.setSingleChoiceItems(
R.array.answersStringAray, // resource in res/values/arrays.xml
1, // value of choosed item in array above
clickListener // Implements DialogInterface.OnClickListener
)
.setNegativeButton(R.string.cancel, clickListener)
.create()
}
}
YourViewModel.kt
class YourViewModel : ViewModel(), DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
when (which) {
DialogInterface.BUTTON_POSITIVE -> {}
DialogInterface.BUTTON_NEGATIVE -> {}
DialogInterface.BUTTON_NEUTRAL -> {}
0 -> {} // all your answers would be non-negative
1 -> {}
}
}
}