简体   繁体   中英

Edit Text inside Alert Dialog Builder

I have an Alert Dialog inside a Fragment:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),
                             com.yanay.yanay.apps.odds.R.style.AppCompatAlertDialogStyle);

                LayoutInflater inflater = getActivity().getLayoutInflater();
                builder.setView(inflater.inflate(R.layout.dialog_numberpicker, null));
                View view =  inflater.inflate(R.layout.dialog_numberpicker, null);
                final EditText editText = (EditText) view.findViewById(R.id.numberpicked);
                if (editText.getText().toString() != null){
                    numberpicked = editText.getText().toString();
                    Log.d("Text Picked", numberpicked);}
                Boolean reverse = true;
                String title = "";
                String dialogMessage = "";
                String buttonTitle = "";
                if (!didStart && replies == 0) {
                    buttonTitle = "Choose Odds";
                    dialogMessage = ping.getBody() + " Challenged You! Odds you: " + ping.getNum() + ". Choose out of:";
                    title = "New Challenge";
                    message = numberpicked;
                }

                builder.setTitle(title);
                builder.setMessage(dialogMessage);
                builder.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                dialog.dismiss();
                            }
                        });


                Log.d("DidStart", didStart.toString());
                Log.d("Replies", replies.toString());




                if (replies < 3) {
                    builder.setPositiveButton(buttonTitle,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    //Pinger pinger = mPingerAdapter.getItem(position);
                                    // pingSomeone(pinger);
                                    final Context context = getActivity();
                                    EditText choosenNumber = (EditText) getView().findViewById(R.id.numberpicked);


                                    Bundle data = new Bundle();
                                    data.putString(PingerKeys.ACTION, GcmAction.PING_CLIENT);
                                    String from = ping.getFrom();
                                    data.putString(PingerKeys.TO, ping.getFrom());
                                    //PingerKeys.NUM = "3333";

                                    data.putString(PingerKeys.NUM, message);
                                    Log.d("LOOK HERE ####", message);
                                    data.putString(PingerKeys.SENDER,
                                            mDefaultSharedPreferences.getString(RegistrationConstants.TOKEN, "Test"));
                                    try {
                                        GoogleCloudMessaging.getInstance(context)
                                                .send(FriendlyPingUtil.getServerUrl(getActivity()),
                                                        String.valueOf("6969"), data);
                                        AnalyticsHelper.send(context, TrackingEvent.PING_SENT);
                                    } catch (IOException e) {
                                        Log.w(TAG, "Could not ping client.", e);
                                    }

                                }
                            });}

                    builder.show();
                replies = replies + 1;
                }

Number Picked is always null. How can I get the value of the text entered into the edit text. The dialog layout is just a linear layout with an edit text inside.

Here is the way to get the EditText value from Dialog

        LayoutInflater layoutInflater = LayoutInflater.from(YourActivity.this);
        View rootView = layoutInflater.inflate(R.layout.input_dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);
        alertDialogBuilder.setView(rootView);

        final EditText yourEditText= (EditText) rootView .findViewById(R.id.edittext);

        alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                         // get the `EditText` value 
                         Toast.makeText(YourActivity.this,"EditText value" + yourEditText.getText(),Toast.LENG_SHORT).show();
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        AlertDialog alert = alertDialogBuilder.create();
        alert.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM