java Android - 无法从自定义对话框中的 EditText 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14568172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:43:05  来源:igfitidea点击:

Android - Can't get value from EditText inside Custom Dialog

javaandroidxmldialogandroid-edittext

提问by Alec Moore

I have been unsuccessful in getting the input from my EditText object inside my custom dialog.

我没有成功从我的自定义对话框中的 EditText 对象获取输入。

 public class SetCityDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater factory = LayoutInflater.from(MainActivity.this);
        final View view = factory.inflate(R.layout.city_dialog, null);


        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.city_dialog, null))
                // Add action buttons
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                //This is the input I can't get text from
                                EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
                                //query is of the String type
                                query = inputTemp.getText().toString();
                                newQuery();
                                getJSON newData = new getJSON();
                                newData.execute("Test");
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                SetCityDialog.this.getDialog().cancel();
                            }
                        });
        return builder.create();
    }
}

I don't get any exceptions, but the variable query is set to an empty string. Any help would be fantastic.

我没有得到任何异常,但变量查询设置为空字符串。任何帮助都会很棒。

回答by jerome

I was trying to do the same thing and i get the same error. I don't no why. I already use AlertDialog.Builder in the past and get no trouble. But in your case change this code:

我试图做同样的事情,但我得到了同样的错误。我不知道为什么。我过去已经使用过 AlertDialog.Builder 并且没有遇到任何麻烦。但在您的情况下更改此代码:

public void onClick(DialogInterface dialog,
                                int id) {

                            //This is the input I can't get text from
                            EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
                            //query is of the String type
                            query = inputTemp.getText().toString();
                            newQuery();
                            getJSON newData = new getJSON();
                            newData.execute("Test");
                        }

By this one:

通过这个:

public void onClick(DialogInterface dialog,
                                int id) {
                            Dialog f = (Dialog) dialog;
                            //This is the input I can't get text from
                            EditText inputTemp = (EditText) f.findViewById(R.id.search_input_text);
                            query = inputTemp.getText().toString();
                           ...
                        }

This solution works for me and it seems to be the same for you. Found on stackoverflow

这个解决方案对我有用,对你来说似乎是一样的。在stackoverflow上找到

回答by h4rd4r7c0r3

Use this instead :

改用这个:

View myLayout = nflater.inflate(R.layout.city_dialog, null);
EditText myEditText = (EditText) myLayout.findViewById(R.id.myEditText);
String valueOfEditText = myEditText.getText().toString();

回答by Android Freaks

No need to do that much coding. just change

不需要做那么多编码。只是改变

    builder.setView(inflater.inflate(R.layout.city_dialog, null))

to

    builder.setView(view)

and access text of EditText using **view.findView.....

并使用 **view.findView 访问 EditText 的文本.....

    EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
    String xyz = inputTemp.getText().toString();

回答by Vinod Joshi

This is worked for me: 

// if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                try {

                    Dialog inDialog = (Dialog) dialog;                    

                    EditText emailAddress = (EditText) inDialog.findViewById(R.id.emailAddress);
                    email = emailAddress.getText().toString();

                    if(email.length() == 0) {

                        objPublicDelegate.showToast("Please fill Email Address.");

                    }else{

                        objLoadingDialog.show("Please wait...");

                        // Call a network thread Async task 
                        mNetworkMaster.runForgetAsync(email);

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });