无法在自定义对话框中检索EditText

时间:2013-05-24 06:16:45

标签: android dialog

我似乎无法绕过自定义对话框。我尝试了很多变化,但到目前为止还没有运气。

这是我当前对话框的xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/edit_name"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:layout_gravity="center" 
              android:orientation="vertical"  >

    <TextView
            android:id="@+id/Word"
            android:text="Word?"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"/>

    <EditText
            android:layout_height="100dp"
            android:layout_width="match_parent"
            android:id="@+id/lookupWordEdit"
            android:inputType="text"/>
</RelativeLayout>

以下是代码:

public class LookupWordDialogFragment extends DialogFragment {
    private EditText mEditText;

    public LookupWordDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    public interface LookupWordDialogListener {
        void onFinishEditDialog(String inputText);
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final EditText lookupWordEdit = (EditText)getActivity().findViewById(R.id.lookupWordEdit);

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.dialog_lookupword, null))
                // Add action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                // Return input text to activity

                LayoutInflater inflater = getActivity().getLayoutInflater();
                final EditText lookupWordEdit = (EditText)getActivity().findViewById(R.id.lookupWordEdit);

                LookupWordDialogListener activity = (LookupWordDialogListener) getActivity();
                activity.onFinishEditDialog(lookupWordEdit.getText().toString());
            }
        })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        LookupWordDialogFragment.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }
}

代码当前在此行上出现空指针异常崩溃:

activity.onFinishEditDialog(lookupWordEdit.getText().toString());

我怀疑我没有正确抓取lookupWordEdit。对话框非常简单。当用户点击“确定”按钮时,我该如何获取输入的文本?

谢谢!

2 个答案:

答案 0 :(得分:2)

您需要更改此

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
 View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_lookupword, null);
 builder.setView(view);

现在,您很少使用AlertDialog

中的EditText
EditText lookupWordEdit = (EditText)view.findViewById(R.id.lookupWordEdit);

答案 1 :(得分:0)

lookupWordEdit EditText位于AlertDialog布局内,而不是DialogFragment布局,因此您需要使用setView AlertDialog中传递的相同View实例来访问EditText AlertDialog布局执行:

LayoutInflater inflater = LayoutInflater.from(getActivity());
View dialogview = inflater.inflate(R.layout.dialog_lookupword, null);
// now pass dialogview to setView of Dialog
builder.setView(dialogview);
.....

现在使用dialogview将EditText从dialog_lookupword布局初始化为:

EditText lookupWordEdit = (EditText)dialogview.findViewById(R.id.lookupWordEdit);