具有自定义 View 的 Android AlertDialog : get input data

标签 android android-dialogfragment

我有一个带有显示单个 EditText 的 AlertDialog 的应用程序。我按照 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();

    }
}

在我的 Activity 中,我执行以下操作:

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 的值?

谢谢!

最佳答案

我认为那是因为您试图在其中查找编辑文本的 View 不正确。

应该是这样的:

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);
            }
        })

关于具有自定义 View 的 Android AlertDialog : get input data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829456/

相关文章:

java - 不需要 Android JNI System.loadLibrary?

Android Dialogfragment消除黑边(上下)

android - 如何导航用户到 "Searchable Items"设置?

android - 是否可以在 firebase 测试实验室中添加自定义 15 英寸设备

android - 有没有办法阻止将日志消息打印到 LogCat?

android - R无法解析-android

android - Dialog Fragment 无法将事件回传给 Android 中的调用 Fragment?

android - 为什么我的 DialogFragment 没有显示自定义布局?

android - 获取默认对话框的 View

android - 对话框 fragment 问题 : onCreateDialog not called; can't get back key press