安卓AlertDialog使用自定义视图:获取输入数据

12

我有一个应用程序,其中包含一个AlertDialog,显示一个单独的EditText。我按照Android开发者指南进行操作,但我找不到如何获取用户输入的数据。

自定义布局只有一个EditText:

<EditText
    android:id="@+id/license_value"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/license" />

我正在使用DialogFragment创建对话框。为了在用户点击“确定”按钮时获取数据,我正在使用接口。

public class EditLicenseDialogFragment extends DialogFragment {

    public interface EditLicenseDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog, String value);
    }

    private EditLicenseDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (EditLicenseDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
             throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.new_license);

        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.edit_license, null))

        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                        // GET VALUE. HOW TO DO IT?
                EditText valueView = (EditText) getActivity().findViewById(R.id.license_value);
                if(valueView == null) Log.d("AA", "NULL");
                else{
                    String value = valueView.getText().toString();
                    mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value);
                }
            })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                EditLicenseDialogFragment.this.getDialog().cancel();
            }
        }); 

        return builder.create();

    }
}

在我的活动中,我会执行以下操作:

public class LicenseListActivity extends FragmentActivity 
    implements EditLicenseDialogFragment.EditLicenseDialogListener{

    ...

    findViewById(R.id.buttonAddLicense).setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    DialogFragment dialog = new EditLicenseDialogFragment(true);
                    dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");
                }
            });

    @Override
    public void onDialogPositiveClick(DialogFragment dialog, String value) {
       Log.d("TAG", value);
    }

我尝试从DialogFragment中检索的EditText始终为NULL。如何获取EditText的值?

谢谢!

1个回答

25

我认为这是因为你尝试查找的视图并不正确。

应该像这样:

LayoutInflater inflater = getActivity().getLayoutInflater();

View dialogView = inflater.inflate(R.layout.edit_license, null);
builder.setView(dialogView)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

            EditText valueView = (EditText) dialogView.findViewById(R.id.license_value); //here
            if(valueView == null) Log.d("AA", "NULL");
            else{
                String value = valueView.getText().toString();
                mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value);
            }
        })

我尝试了这个,但在初始化valueView的那一行上,编译器给了我一个错误:“无法引用在不同方法中定义的内部类中的非最终变量diag_view”。如果我将dialogView设置为final变量,它将不会被更改,也无法从中检索任何内容。 - jerryh91
因此,请将“View dialogView”声明在方法之外,作为类字段。 - Michał Z.
1
你需要将dialogView声明为final,如下所示:final View dialogView = inflater.inflate(R.layout.edit_license, null);。只有这样才能在匿名类OnClickListener中使用它。 - Aniket Thakur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接