开发者

Android - retrieving text input from an alertbuilder dialog

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();

    }
}


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.


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


Do something like:

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

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

i tried it and it helped


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();
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜