Android在对话框中获取EditText值[返回空字符串]

Android在对话框中获取EditText值[返回空字符串],android,android-edittext,android-dialog,Android,Android Edittext,Android Dialog,我有一个包含EditText的对话框片段,我想从中检索值,但当我尝试时,得到一个空字符串。 这是我的对话代码 public class PersonEditDialog extends DoubleActionDialog{ public PersonEditDialog() { } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder =

我有一个包含EditText的对话框片段,我想从中检索值,但当我尝试时,得到一个空字符串。 这是我的对话代码

public class PersonEditDialog extends DoubleActionDialog{



public PersonEditDialog() {

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.activity_personne_edit, null))
    .setPositiveButton(R.string.s_button_save, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            doAction();
        }
    })
    .setNegativeButton(R.string.s_button_cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            PersonEditDialog.this.getDialog().cancel();
        }
    });   
    builder.setTitle(getTitleId());
    return builder.create();
}

@Override
public void doUpdate() {
    // mise à jour de la personne
    View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
    EditText nom = (EditText)  view.findViewById(R.id.nom);
    EditText prenom = (EditText) view.findViewById(R.id.prenom);
    Toast.makeText(getActivity(), nom.getText() + " " + prenom.getText() + " mis à jour", Toast.LENGTH_SHORT).show();
}

@Override
public void doCreate() {
    // ajout de la personne
    View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
    EditText nom = (EditText)  view.findViewById(R.id.nom);
    EditText prenom = (EditText) view.findViewById(R.id.prenom);
    Toast.makeText(getActivity(), nom.getText().toString() + " " + prenom.getText().toString() + " créé", Toast.LENGTH_SHORT).show();
}

@Override
protected int getUpdateLabelId() {
    return R.string.s_title_edit_person;
}

@Override
protected int getCreateLabelId() {
    return R.string.s_title_add_person;
}
}

doAction()方法调用两个方法之一:doUpdate()或docCreate(),具体取决于参数的值


doCreate()或doUpdate()正在尝试读取EditText的内容。

您正在扩展一个新布局,并在这个新布局中获取EditText的内容。由于编辑文本所在的布局是对话框布局,因此可以在
DialogFragment
中使用
getDialog()
来获取对对话框的引用,并在对话框上调用
findViewById()

例如,更换

View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
有点像

Dialog dlg = getDialog();

然后调用
dlg.findViewById(…)

您正在扩展一个新布局,并在这个新布局中获取编辑文本的内容。由于编辑文本所在的布局是对话框布局,因此可以在
DialogFragment
中使用
getDialog()
来获取对对话框的引用,并在对话框上调用
findViewById()

例如,更换

View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
有点像

Dialog dlg = getDialog();

然后调用
dlg.findViewById(…)

您正在扩展一个新布局,并在这个新布局中获取编辑文本的内容。由于编辑文本所在的布局是对话框布局,因此可以在
DialogFragment
中使用
getDialog()
来获取对对话框的引用,并在对话框上调用
findViewById()

例如,更换

View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
有点像

Dialog dlg = getDialog();

然后调用
dlg.findViewById(…)

您正在扩展一个新布局,并在这个新布局中获取编辑文本的内容。由于编辑文本所在的布局是对话框布局,因此可以在
DialogFragment
中使用
getDialog()
来获取对对话框的引用,并在对话框上调用
findViewById()

例如,更换

View view = getActivity().getLayoutInflater().inflate(R.layout.activity_personne_edit, null);
有点像

Dialog dlg = getDialog();

然后调用
dlg.findViewById(…)
您将得到一个空的
字符串,因为您试图从另一个
编辑文本中获取它。在
doUpdate
doCreate
方法中,您正在使用布局
activity\u personne\u edit
创建新的
View
s

相反,在
onCreateDialog
中,您应该将布局膨胀为全局
视图
对象

layoutView = inflater.inflate(R.layout.activity_personne_edit, null);
builder.setView(layoutView)
然后在
doUpdate
doCreate
方法中进行更改

EditText nom = (EditText)  layoutView.findViewById(R.id.nom);
EditText prenom = (EditText) layoutView.findViewById(R.id.prenom);


然后无需创建
视图
对象。

您将获得一个空的
字符串
,因为您试图从不同的
编辑文本
获取它。在
doUpdate
doCreate
方法中,您正在使用布局
activity\u personne\u edit
创建新的
View
s

相反,在
onCreateDialog
中,您应该将布局膨胀为全局
视图
对象

layoutView = inflater.inflate(R.layout.activity_personne_edit, null);
builder.setView(layoutView)
然后在
doUpdate
doCreate
方法中进行更改

EditText nom = (EditText)  layoutView.findViewById(R.id.nom);
EditText prenom = (EditText) layoutView.findViewById(R.id.prenom);


然后无需创建
视图
对象。

您将获得一个空的
字符串
,因为您试图从不同的
编辑文本
获取它。在
doUpdate
doCreate
方法中,您正在使用布局
activity\u personne\u edit
创建新的
View
s

相反,在
onCreateDialog
中,您应该将布局膨胀为全局
视图
对象

layoutView = inflater.inflate(R.layout.activity_personne_edit, null);
builder.setView(layoutView)
然后在
doUpdate
doCreate
方法中进行更改

EditText nom = (EditText)  layoutView.findViewById(R.id.nom);
EditText prenom = (EditText) layoutView.findViewById(R.id.prenom);


然后无需创建
视图
对象。

您将获得一个空的
字符串
,因为您试图从不同的
编辑文本
获取它。在
doUpdate
doCreate
方法中,您正在使用布局
activity\u personne\u edit
创建新的
View
s

相反,在
onCreateDialog
中,您应该将布局膨胀为全局
视图
对象

layoutView = inflater.inflate(R.layout.activity_personne_edit, null);
builder.setView(layoutView)
然后在
doUpdate
doCreate
方法中进行更改

EditText nom = (EditText)  layoutView.findViewById(R.id.nom);
EditText prenom = (EditText) layoutView.findViewById(R.id.prenom);


然后无需创建
视图
对象。

我这样做了,但返回的视图为空:/我按照您的建议使用了getDialog(),它成功了。谢谢还有一个问题,我没有重写的onCreateView()应该返回什么?它在常规片段中提供片段视图层次结构。你有一个对话片段。我首先想到的是常规片段,然后才读到足够的代码,可能是dialogfragment。我已经搜索了几个小时空字符串返回的原因,最后发现这就是实际原因。谢谢。我这样做了,但是返回的视图是空的:/I我按照您的建议使用了getDialog(),它成功了。谢谢还有一个问题,我没有重写的onCreateView()应该返回什么?它在常规片段中提供片段视图层次结构。你有一个对话片段。我首先想到的是常规片段,然后才读到足够的代码,可能是dialogfragment。我已经搜索了几个小时空字符串返回的原因,最后发现这就是实际原因。谢谢。我这样做了,但是返回的视图是空的:/I我按照您的建议使用了getDialog(),它成功了。谢谢还有一个问题,我没有重写的onCreateView()应该返回什么?它在常规片段中提供片段视图层次结构。你有一个对话片段。我首先想到的是常规片段,然后读了足够多的代码,看到的可能是dialogfragment