Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何读取单选按钮状态_Android_Radio Button_Android Alertdialog - Fatal编程技术网

Android 如何读取单选按钮状态

Android 如何读取单选按钮状态,android,radio-button,android-alertdialog,Android,Radio Button,Android Alertdialog,我有一个名为options.xml的xml文件 我用它来创建我的AlertDialog。我还为AlertDialog添加了一个Done按钮。 按下“完成”按钮时,将调用下面的onClick方法。但是,无论在单击“完成”之前选中了哪个单选按钮,我总是将第一个单选按钮视为选中,而将其他单选按钮视为未选中。 我假设我下面所做的只是读取xml值,而不是实际的运行时值?那么如何读取实时运行时值 public void onClick(DialogInterface arg0, int arg1)

我有一个名为options.xml的xml文件 我用它来创建我的AlertDialog。我还为AlertDialog添加了一个Done按钮。 按下“完成”按钮时,将调用下面的onClick方法。但是,无论在单击“完成”之前选中了哪个单选按钮,我总是将第一个单选按钮视为选中,而将其他单选按钮视为未选中。 我假设我下面所做的只是读取xml值,而不是实际的运行时值?那么如何读取实时运行时值

    public void onClick(DialogInterface arg0, int arg1) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
            View layout = inflater.inflate(R.layout.options, null);
            RadioButton a1 = (RadioButton)layout.findViewById(R.id.radio0);
            RadioButton a2= (RadioButton)layout.findViewById(R.id.radio1);
            RadioButton a3 = (RadioButton)layout.findViewById(R.id.radio2);
            RadioButton a4 = (RadioButton)layout.findViewById(R.id.radio3);
            boolean b1 = a1.isChecked();
            boolean b2 = a2.isChecked();
            boolean b3 = a3.isChecked();
            boolean b4 = a4.isChecked();

    }

编辑:我使用的解决方案是“谢谢,我没有检查第二个选项,但我发现AlertDialog有findViewById,创建对话框后只需调用它即可。(不需要在onCreate或任何其他特殊函数中完成)。您只需记住在调用show()后调用findViewById即可)对于对话框,否则它将无法工作“

您是对的,您正在读取它们的xml值,因为您试图在直接从xml膨胀的布局上查找DViewById

如果mContext是活动,则可以使用mContext.findViewById(..)。如果做不到这一点,请在onCreateDialog(..)中的某个位置而不是在onClick(..)中定义单选按钮,然后使用这些按钮确定isChecked()

这是一个类似的示例,我必须从onClick(..)方法内部获取EditText的值。如果您需要
final
修饰符,Eclipse应该告诉您。这都在onCreateDialog(..)方法中


谢谢,我没有检查第二个选项,但我发现AlertDialog已经找到了findViewById,创建对话框后只需调用它。(不需要在onCreate或任何其他特殊函数中完成)。您只需要记住在为对话框调用show()之后调用findViewById,否则它将无法工作。
case FIRST_TRANSLATE:
        View view = mHost.getLayoutInflater().inflate(R.layout.first_translate, null);
        //Define your radiobuttons here
        final EditText mLanguage = (EditText) view.findViewById(R.id.editText1);
        b.setView(view)
        .setTitle(R.string.dialog_first_translate_title)
        .setPositiveButton(sOkay, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                //Retrieve their values here
                mHost.getSharedPreferences(States.INTERNAL_PREFS, 0).edit().putString("deflang", mLanguage.getText().toString()).commit();
                dialog.dismiss();
            }

        }).create();