Android - 从alertbuilder对话框中检索文本输入

发布于 2024-12-07 17:09:22 字数 2434 浏览 3 评论 0原文

我在 xml 文件中定义了一个视图。它包含两个 Edittext 字段(以及文本等其他内容),

我使用 AlertBuilder 触发一个对话框,用户在其中输入文本(例如用户名和密码)到两个 edittext 字段中。当我尝试检索字符串并将它们发送到 Login() 时,两个字符串都为空。到底是怎么回事?

似乎字符串数据没有保存?

这是我在应用程序中显示对话框的时间:

SignInDialog.show(ScreenMain.this, 
                                "Login", 
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        View layout = inflater.inflate(R.layout.screen_dialog_login, null);
                                        LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
                                                        ((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());

                                    }
                                }, 
                                "Cancel", 
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                });

这是我用来实例化对话框的类:

/* login dialog*/
static class SignInDialog {

    public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
        AlertDialog.Builder builder;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_dialog_login, null);

        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        if(positive != null && positiveText != null){
            builder.setPositiveButton(positiveText, positive);
        }
        if(negative != null && negativeText != null){
            builder.setNegativeButton(negativeText, negative);
        }

        builder.create().show();

    }
}

I've got a View defined in an xml file. It contains two Edittext fields (amongt other things like text)

I use an AlertBuilder to trigger a dialog where a user enters text(such as username and pass) into both edittext fields. When I try to retrieve the strings and send them to Login(), both strings are just null. What is going on?

It seems like somehow the string data isn't saved?

Here's when I show the Dialog in my app:

SignInDialog.show(ScreenMain.this, 
                                "Login", 
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        View layout = inflater.inflate(R.layout.screen_dialog_login, null);
                                        LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
                                                        ((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());

                                    }
                                }, 
                                "Cancel", 
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();
                                    }
                                });

Here's a class I use to instantiate a Dialog:

/* login dialog*/
static class SignInDialog {

    public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
        AlertDialog.Builder builder;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_dialog_login, null);

        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        if(positive != null && positiveText != null){
            builder.setPositiveButton(positiveText, positive);
        }
        if(negative != null && negativeText != null){
            builder.setNegativeButton(negativeText, negative);
        }

        builder.create().show();

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装不在乎 2024-12-14 17:09:23

膨胀布局就是创建它的一个新实例。 (您没有收到对现有实例的引用。)因此,在您的 onClick 中,您正在创建布局的新副本,并且您的字段不包含任何文本,因为它们不相同您的用户刚刚输入文本的。

To inflate a layout is to create a new instance of it. (You're not receiving a reference to an existing instance.) So, in your onClick you are creating a new copy of the layout and your fields don't contain any text because they are not the same ones your user just entered text in.

飞烟轻若梦 2024-12-14 17:09:23

为什么不完全子类化 AlertDialog.Builder 并添加一个方法来检索 EditText 值?

Why not just completely subclass AlertDialog.Builder and add a method to retrieve the EditText values?

回忆那么伤 2024-12-14 17:09:23

做类似的事情:

View layout = inflater.inflate(R.layout.screen_dialog_login, null);

layout.findViewById(R.id.*yourwidget*);

我尝试过,它有帮助

Do something like:

View layout = inflater.inflate(R.layout.screen_dialog_login, null);

layout.findViewById(R.id.*yourwidget*);

i tried it and it helped

柠檬心 2024-12-14 17:09:23

这是我正在使用的方法:

private void showPopUp3() {          

                 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(AlarmReceiverActivity.this);
                 helpBuilder.setTitle("hi");
                // helpBuilder.setMessage("This is a Simple Pop Up");
                 final EditText input = new EditText(this);
                 input.setHeight(20);
                 input.setText("");
                 LayoutInflater inflater = getLayoutInflater();
                 final View checkboxLayout = inflater.inflate(R.layout.alarm, null);


                 checkboxLayout.findViewById(R.id.Yes).setOnClickListener(new OnClickListener(){
                        public void onClick(View arg0) {
                            // setTitle("button2");
                            checkboxLayout.findViewById(R.id.note).setVisibility(View.VISIBLE);
                        }
                    });
                 checkboxLayout.findViewById(R.id.No).setOnClickListener(new OnClickListener(){
                        public void onClick(View arg0) {
                            // setTitle("button2");
                            checkboxLayout.findViewById(R.id.note).setVisibility(View.INVISIBLE);
                        }
                    });
                 helpBuilder.setView(checkboxLayout);

                 helpBuilder.setPositiveButton("No",
                   new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                     // Do nothing but close the dialog
                         mMediaPlayer.stop();
                         finish();
                    }
                   });
                 helpBuilder.setNegativeButton("Yes",
                           new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                             // Do nothing but close the dialog
                     mMediaPlayer.stop();

                        //showSimplePopUp();

                            }
                           });
                 // Remember, create doesn't show the dialog
                 AlertDialog helpDialog = helpBuilder.create();
                 helpDialog.show();
            }

Here is the method I am using:

private void showPopUp3() {          

                 AlertDialog.Builder helpBuilder = new AlertDialog.Builder(AlarmReceiverActivity.this);
                 helpBuilder.setTitle("hi");
                // helpBuilder.setMessage("This is a Simple Pop Up");
                 final EditText input = new EditText(this);
                 input.setHeight(20);
                 input.setText("");
                 LayoutInflater inflater = getLayoutInflater();
                 final View checkboxLayout = inflater.inflate(R.layout.alarm, null);


                 checkboxLayout.findViewById(R.id.Yes).setOnClickListener(new OnClickListener(){
                        public void onClick(View arg0) {
                            // setTitle("button2");
                            checkboxLayout.findViewById(R.id.note).setVisibility(View.VISIBLE);
                        }
                    });
                 checkboxLayout.findViewById(R.id.No).setOnClickListener(new OnClickListener(){
                        public void onClick(View arg0) {
                            // setTitle("button2");
                            checkboxLayout.findViewById(R.id.note).setVisibility(View.INVISIBLE);
                        }
                    });
                 helpBuilder.setView(checkboxLayout);

                 helpBuilder.setPositiveButton("No",
                   new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                     // Do nothing but close the dialog
                         mMediaPlayer.stop();
                         finish();
                    }
                   });
                 helpBuilder.setNegativeButton("Yes",
                           new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                             // Do nothing but close the dialog
                     mMediaPlayer.stop();

                        //showSimplePopUp();

                            }
                           });
                 // Remember, create doesn't show the dialog
                 AlertDialog helpDialog = helpBuilder.create();
                 helpDialog.show();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文