android - 从 alertDialog 构建器获取 editText 值

标签 android android-dialog

我是 android 的新手。我需要获取 editText 的 值来搜索某些内容。但是当我运行程序时,出现了null pointer exception 错误。

“EditText etSearch”没有获取它的文本。

请帮帮我。 谢谢。

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_search) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            // Get the layout inflater
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();

            // 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_save, null))

                    // Add action buttons
                    .setPositiveButton("Search",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    final EditText etSearch = (EditText) findViewById(R.id.etSearchText);
                                    mSearchText = etSearch.getText().toString();

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            // return builder.create();
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

最佳答案

要从 Diloag 中查找 View ,您必须提供该 Dialog View 的引用。

 LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            View mView = inflater.inflate(R.layout.dialog_save, null);
            final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
            builder.setView(mView)

完整代码:

public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_search) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            // Get the layout inflater
            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            View mView = inflater.inflate(R.layout.dialog_save, null);
            final EditText etSearch = (EditText)mView.findViewById(R.id.etSearchText);
            builder.setView(mView)

                    // Add action buttons
                    .setPositiveButton("Search",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int id) {

                                    mSearchText = etSearch.getText().toString();

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            });
            // return builder.create();
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

关于android - 从 alertDialog 构建器获取 editText 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31153018/

相关文章:

Android - 中间的Edittext垂直线

java - 如何在没有NDK的情况下在Android中处理原始相机数据

android - 是/否对话框和生命周期

android - 来自类型 Activity 的方法 showDialog(int) 在 android 中被弃用了吗?

android - Material 对话框库 - 防止在 onPositive 函数调用时关闭/关闭对话框

android - ConnectivityManager.NetworkCallback 未按预期工作

java - 从 android 在 openfire 服务器中创建新用户

android - 如何忽略 DialogFragment 中的 'search' 按键

android - 如何在警报对话框中为按钮设置可绘制对象?在安卓系统中

android - 如何在android中修复对话框的大小