在Alert Dialog Builder中编辑文本

时间:2016-05-06 22:41:04

标签: java android android-fragments alertdialog

我在片段中有一个警告对话框:

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始终为null。如何获取输入到编辑文本中的文本的值。对话框布局只是一个线性布局,里面有编辑文本。

1 个答案:

答案 0 :(得分:1)

以下是从EditText

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