2

我尝试实现 RadioGroup 视图并将其放在 DialogFragment 中,但出现了问题。我已经按照这个教程

http://developer.android.com/guide/topics/ui/controls/radiobutton.html

而是在 DialogFragment 中初始化 Activity,视图:

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 listener 它的实现方式与教程相同。现在,当启动对话框时,如果我点击一个单选按钮,我会收到此错误:

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'

怎么了?

4

5 回答 5

3

我宁愿在片段内实现点击处理程序并动态添加侦听器(通过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){
    [...]
}
于 2013-09-27T08:16:13.480 回答
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;
}
于 2013-09-27T08:16:23.703 回答
1

问题是,如果您为 xml 文件onClick中的任何内容指定回调属性,View它应该位于Activity代码内而不是片段代码中

于 2013-09-27T08:14:10.600 回答
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();
}

顺便说一句,按照http://developer.android.com/guide/topics/ui/controls/radiobutton.html中的说明,删除片段中的 onRadioButtonClicked() 和 xml 中 RadioButton 的“android:onClick="onRadioButtonClicked"”

于 2015-10-21T09:33:38.620 回答
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()
    }
}

你的视图模型.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 -> {}
        }
    }
}
于 2021-04-03T15:01:26.763 回答