单击“确定”时 EditTexts 始终为空
EditTexts are always empty when clicking OK
当我单击确定时,字符串 returned 总是空字符串 ""
。他们不是,问题可能是对话框创建了两次,所以他们指的不是同一个 EditTexts
?
package com.example.gaetano.notebook;
public class AddNoteFragment extends DialogFragment`enter code here`{
public EditText title;
public EditText note;
public OnNoteAdded noteAdded;
public interface OnNoteAdded {
void onNoteAdded(Note note);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
noteAdded = (OnNoteAdded) activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_add_note, null);
title = (EditText) rootView.findViewById(R.id.edTitle);
note = (EditText) rootView.findViewById(R.id.edNote);
builder.setTitle("Add a new note");
builder.setView(inflater.inflate(R.layout.fragment_add_note, null))
.setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String t = title.getText().toString();
String n = note.getText().toString();
Note note = new Note(t, n , false);
noteAdded.onNoteAdded(note);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddNoteFragment.this.getDialog().cancel();
}
});
return builder.create();
}
单击“确定”时,EditTexts 始终为空“”。
View rootView = inflater.inflate(R.layout.fragment_add_note, null);
title = (EditText) rootView.findViewById(R.id.edTitle);
note = (EditText) rootView.findViewById(R.id.edNote);
然后在我的 setPositiveButton, OnClick
方法中我得到了这些文本的字符串。通过这样做 String t = title.getText().toString();
String n = note.getText().toString();
他们总是return""
在 MainActivity 中,我然后像这样实现接口 @Override
public void onNoteAdded(Note note) {
noteAdapter.add(note);
}
根据 Layout Inflater API,Inflater 的 inflate()
方法 returns 一个 View
对象。您可以将此 View
对象的 reference
保存在一个变量中,它将充当 Dialog 的 screen.Using 此 root view
的 root view
,您可以找到所有此 root view's tree
中包含的子视图或简称为使用 findViewById()
的此根视图的子视图。
这是 onCreateDialog()
的片段:-
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView=inflater.inflate(R.layout.fragment_add_note, null)
Button mydialogbutton=(Button) rootView.findViewById(R.id.my_dialog_button);
//Button is exemplary but this is how you can access any child component of the root view
builder.setTitle("Add a new note");
builder.setView(rootView)
.setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Note newNote = new Note(
"hey",
"notehey",
false);
noteAdded.onNoteAdded(newNote);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddNoteFragment.this.getDialog().cancel();
}
});
return builder.create();
视图的创建和引用过程与onCreateView()相同。正如在 onCreateDialog()
和 onCreateView()
中一样,您正在使用 Layout Inflater 来获取对 View
对象的引用。
当我单击确定时,字符串 returned 总是空字符串 ""
。他们不是,问题可能是对话框创建了两次,所以他们指的不是同一个 EditTexts
?
package com.example.gaetano.notebook;
public class AddNoteFragment extends DialogFragment`enter code here`{
public EditText title;
public EditText note;
public OnNoteAdded noteAdded;
public interface OnNoteAdded {
void onNoteAdded(Note note);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
noteAdded = (OnNoteAdded) activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_add_note, null);
title = (EditText) rootView.findViewById(R.id.edTitle);
note = (EditText) rootView.findViewById(R.id.edNote);
builder.setTitle("Add a new note");
builder.setView(inflater.inflate(R.layout.fragment_add_note, null))
.setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String t = title.getText().toString();
String n = note.getText().toString();
Note note = new Note(t, n , false);
noteAdded.onNoteAdded(note);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddNoteFragment.this.getDialog().cancel();
}
});
return builder.create();
}
单击“确定”时,EditTexts 始终为空“”。
View rootView = inflater.inflate(R.layout.fragment_add_note, null);
title = (EditText) rootView.findViewById(R.id.edTitle);
note = (EditText) rootView.findViewById(R.id.edNote);
然后在我的 setPositiveButton, OnClick
方法中我得到了这些文本的字符串。通过这样做 String t = title.getText().toString();
String n = note.getText().toString();
他们总是return""
在 MainActivity 中,我然后像这样实现接口 @Override
public void onNoteAdded(Note note) {
noteAdapter.add(note);
}
根据 Layout Inflater API,Inflater 的 inflate()
方法 returns 一个 View
对象。您可以将此 View
对象的 reference
保存在一个变量中,它将充当 Dialog 的 screen.Using 此 root view
的 root view
,您可以找到所有此 root view's tree
中包含的子视图或简称为使用 findViewById()
的此根视图的子视图。
这是 onCreateDialog()
的片段:-
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView=inflater.inflate(R.layout.fragment_add_note, null)
Button mydialogbutton=(Button) rootView.findViewById(R.id.my_dialog_button);
//Button is exemplary but this is how you can access any child component of the root view
builder.setTitle("Add a new note");
builder.setView(rootView)
.setPositiveButton(R.string.add_note, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Note newNote = new Note(
"hey",
"notehey",
false);
noteAdded.onNoteAdded(newNote);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
AddNoteFragment.this.getDialog().cancel();
}
});
return builder.create();
视图的创建和引用过程与onCreateView()相同。正如在 onCreateDialog()
和 onCreateView()
中一样,您正在使用 Layout Inflater 来获取对 View
对象的引用。