Android 在自定义对话框中获取EditText视图的值
我有一个按钮,按下时会打开一个自定义对话框。该对话框显示编辑文本视图。当用户按下提交按钮时,我希望获取他们输入的值并将其发送到我的服务器Android 在自定义对话框中获取EditText视图的值,android,Android,我有一个按钮,按下时会打开一个自定义对话框。该对话框显示编辑文本视图。当用户按下提交按钮时,我希望获取他们输入的值并将其发送到我的服务器 FloatingActionButton fab = (FloatingActionButton) item.findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { class CustomOnClickListener implements
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) {}
});
提前谢谢你 初始化类中的字符串变量,即String note=“” 然后,在肯定按钮上设置onClickListener,并从EditText获取文本。我不知道你为什么要使用自定义的OnClick侦听器
.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
note = mNotes.getText();
}
});
这应该能奏效