【问题标题】:Android read state of radiobuttons in custom dialog [update -> How to read from a custom dialog?]Android在自定义对话框中读取单选按钮的状态[更新->如何从自定义对话框中读取?]
【发布时间】:2011-07-19 02:05:36
【问题描述】:

-- 更新问题--

我无法从我的自定义对话框中读取任何数据,起初我以为它只是单选按钮,所以我认为让我们继续项目,因此我为测试目的提供了一个默认值。

即使在我尝试从 EditText 中读取数据后,我还是会收到 nullpointerexceptions。

知道我在这里做错了什么吗? (下面是 XML + 代码,在下面的 cmets 中我展示了我对 EditText 所做的事情)

-- 老问题--

所以,我使用了多个来源来组合我现在使用的布局/检查功能。

当我试图查看选择了哪个单选按钮时,我不断收到空指针错误,所以我在这里搜索并添加了一些测试,看看我是否可以通过查看通过 radiogroup 设置的内容来获得结果(而不是单个按钮)。

让我向您展示我到目前为止所做/尝试过的事情。

public class MainActivity extends Activity {

    private RadioGroup rbs; //added for test
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;

...

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.connectpopup);
        dialog.setTitle("Quick Connect");
        dialog.setCancelable(true);
        Button button = (Button) dialog.findViewById(R.id.connect);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                rbs = (RadioGroup) findViewById(R.id.widget33); //added for test
                rb1 = (RadioButton) findViewById(R.id.nmdc);
                rb2 = (RadioButton) findViewById(R.id.adc);
                rb3 = (RadioButton) findViewById(R.id.adcs);
                //RadioGroup.getCheckedRadioButtonId()
                Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
                if (rb1.isChecked() == true) { // these check for checked makes the app stop.
                    Toast.makeText(getApplicationContext(), rb1.getText(), 5).show();
                }
                if (rb2.isChecked() == true) {
                    Toast.makeText(getApplicationContext(), rb2.getText(), 5).show();
                }
                if (rb3.isChecked() == true) {
                    Toast.makeText(getApplicationContext(), rb3.getText(), 5).show();
                }
                dialog.dismiss();
            }
        });
        dialog.show();

以及我从中调用的 xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/quickconnectpopup" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Please enter the address.\r\nSelect the protocol.\r\nEnter your nickname." > </TextView> <EditText android:id="@+id/hubaddress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="some.address.com:411" android:textSize="18sp" > </EditText> <RadioGroup android:id="@+id/widget33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/nmdc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="protocol1" > </RadioButton> <RadioButton android:id="@+id/adc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="prototocol2" > </RadioButton> <RadioButton android:id="@+id/adcs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="protocol3" > </RadioButton> </RadioGroup> <EditText android:id="@+id/nickname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Nick" android:textSize="18sp" > </EditText> <Button android:id="@+id/connect" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Connect" > </Button> </LinearLayout>

当我尝试读取(一两行)此行时出现错误(由于我稍后添加了 log.v,因此显示了同一行)

10:26:44.814    1124    ERROR   AndroidRuntime  Uncaught handler: thread main exiting due to uncaught exception
10:26:44.845    1124    ERROR   AndroidRuntime  java.lang.NullPointerException
10:26:44.845    1124    ERROR   AndroidRuntime      at nl.my.project.MainActivity$1.onClick(MainActivity.java:89)

Log.v("test", ""+rbs.getCheckedRadioButtonId()); //this doesn't give results either same nullpointer error
if (rb1.isChecked() == true) { // these check for checked makes the app stop.

所以再次陈述确切的问题,我怎样才能检索选定的单选按钮。

在示例中显示在 Log.v("selected button:", variable); = Verbose Androidblabla radio1

【问题讨论】:

  • 即使只是一个想法也可能奏效,我现在一无所知。如果您没有立即的解决方案,但认为您已经看到了一些东西,我可以再次开始使用谷歌搜索,用我描述中的 3 个词,我再也无法在谷歌上找到任何有用的结果。
  • YaHubAddress = (EditText) findViewById(R.id.hubaddress); Log.v("NMDC", YaHubAddress.getText().toString());这给出了完全相同的错误:S 似乎我根本无法从对话框中读取任何数据:S
  • 答案仅用于此目的,答案。将来,只需编辑您的问题以提供说明。 Stack Overflow 不是论坛 :)

标签: android dialog radio


【解决方案1】:

Johan... 我可以建议您首先使用建议的架构来创建和显示对话框。因此,您需要提供 onCreateDialog(int id) 方法并在此方法中返回对话框,如下所示:

protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case MANAGE_PASSWORD:
            dialog= getInstancePasswordDialog(); //<== CREATE DIALOG HERE
            break;
        case DIALOG_ABOUT:
            // do the work to define the About Dialog
            dialog= getInstanceAlertDialog(); // called "the first time"
            break;
        default:
            dialog = null;
            break;
        }
        return dialog;
    }

密码对话框的创建位置如下:

private Dialog getInstancePasswordDialog() {
        final Dialog d= new Dialog(this);
        d.setContentView(R.layout.password_dialog);
        d.setTitle("Key Manager");
        final EditText editTextPasswordFirst= (EditText)d.findViewById(R.id.EditTextPasswordFirst);
        final EditText editTextPasswordSecond= (EditText)d.findViewById(R.id.EditTextPasswordSecond);
        // CANCEL BUTTON HANDLER
        final Button buttonCancel= (Button)d.findViewById(R.id.ButtonPasswordCancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                d.dismiss();
            }
        });
        // UPDATE BUTTON HANDLER
        final Button buttonUpdate= (Button)d.findViewById(R.id.ButtonPasswordUpdate);
        buttonUpdate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String entryFirst= editTextPasswordFirst.getText().toString();
                String entrySecond= editTextPasswordSecond.getText().toString();       
                if (entryFirst.equals(entrySecond)) { // do NOT use == with string values
                    if (entryFirst.length() != lengthPassword) { // invalid key length
                        editTextPasswordFirst.setText("Invalid Key Length of "+
                                new Integer(entryFirst.length()).toString());                       
                        editTextPasswordSecond.setText("");
                        editTextPasswordFirst.selectAll();
                        editTextPasswordFirst.requestFocus();
                    }
                    else {
                        editTextPassword.setText(entryFirst);
                        // *** Clear Key Fields ***
                        editTextPasswordFirst.setText("");                  
                        editTextPasswordSecond.setText("");
                        d.dismiss();
                    }
                }
                else { // entries do not match
                    editTextPasswordFirst.setText("Entries did not match!");                  
                    editTextPasswordSecond.setText("");
                    editTextPasswordFirst.selectAll();
                    editTextPasswordFirst.requestFocus();
                }
            }
        });
        return d;
    }

然后显示对话框如下:

this.showDialog(MANAGE_PASSWORD);

地点:

private static final int MANAGE_PASSWORD= 0;

如果您使用建议的架构,Android 操作系统将自动处理方向更改,因此您不想在每次调用 onCreate 时启动新对话框。

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 2014-07-29
    • 2019-06-18
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多