在自定义对话框

时间:2018-03-28 21:22:38

标签: android

我有一个按钮,按下后会打开一个自定义对话框。该对话框显示EditText视图。当用户按下提交按钮时,我想获取他们输入的值并将其发送到我的服务器。

FloatingActionButton fab = (FloatingActionButton) item.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        class CustomOnClickListener implements DialogInterface.OnClickListener {
            int mClientNumber;
            int mRecordNumber;
            String mNotes;
            public CustomOnClickListener( int clientNumber, int recordNumber, String notes ) {
                mClientNumber = clientNumber;
                mRecordNumber = recordNumber;
                mNotes = notes;
            }

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // This is where I need the notes field
                Log.i("notes", mNotes);
            }
        }

        @Override
        public void onClick(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View entryView = inflater.inflate(R.layout.entry_form_layout,null);
            EditText mNotes = (EditText) entryView.findViewById(R.id.dialog_note);
            // ---------------------- Show Dialog Form ----------------------
            builder.setTitle(R.string.entry_form_title);
            builder.setView(entryView)
                .setPositiveButton("Submit", new CustomOnClickListener(mItem.clientNumber, medicine.getRecordNumber(), "im stuck" ))
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                    }
                });
            builder.create();
            builder.show();
        }
    });

我可以获得对notes字段的引用,但它不会反映对它所做的更改。我试图实现一个更改侦听器,但遇到了无法获取响应并将其传回对话框setPositiveButton调用的问题。

mNotes.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable s) {
        Log.i("This works", s.toString());
        // how do I get this value to the dialog positive button?
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    public void onTextChanged(CharSequence s, int start, int before, int count) {}
});

提前谢谢!

1 个答案:

答案 0 :(得分:1)

在类中初始化一个String变量,即String note =“”;

然后,在肯定按钮上设置onClickListener并从EditText中获取文本。我不知道你为什么使用自定义onClickListener

.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        note = mNotes.getText();
                      }
});

这应该可以解决问题。

相关问题