如何在dialogFragment中初始化edittext

时间:2015-09-14 20:19:12

标签: android android-dialogfragment

我在这里做错了什么?在扩充Layout之后,我试图在DialogFragment onCreateDialog中实例化EditText。注意:EditText已经是在setView中膨胀的LinearLayout的一部分。

public class UserEntryDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    builder.setView(inflater.inflate(R.layout.entry_dialog, null));

//        EditText userEntryEditText;
//        userEntryEditText = userEntryEditText.findViewById(R.id.entryDialogEditText); - may not have been initalized
//        final EditText userEntryEditText = (EditText) userEntryEditText.findViewById(R.id.entryDialogEditText); - variable may not have been initiized
//        final EditText = new EditText(); - not a valid constructor
//        final EditText = new (EditText) userEntryEditText.findViewById(R.id.entryDialogEditText); - cannot resolve symbol userEntryText
//        EditText userEntryEditText = new userEntryEditText.findViewWithTag("edittexttag"); - findViewWithTag not a method
//        inflater.inflate(R.id.entryDialogEditText, null); - requires a layout argument

    userEntryEditText.requestFocus();

其他代码......

}

同样,我想要一个EditText,然后我可以将其用于getText。我希望你能帮忙。

谢谢你们。

1 个答案:

答案 0 :(得分:0)

由于您在创建对话时没有做任何特别的事情,我建议您删除覆盖代码,然后使用此代码剪切。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.entry_dialog, container, false);
    EditText ev = (EditText) v.findViewById(R.id.editTextId);
    return v;
}

所以要么删除上面的代码,要么将其替换为:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return super.onCreateDialog(savedInstanceState);
}