从alertDialog builder获取editText值

时间:2015-07-01 05:15:59

标签: android android-dialog

我是android的新手。我需要获得editText's值来搜索某些内容。但是当我运行程序时,错误来自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);
    }
}

3 个答案:

答案 0 :(得分:4)

要从Diloag查找视图,您必须重新参考Dialog的视图。

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

答案 1 :(得分:1)

替换此代码:

final EditText etSearch = (EditText) findViewById(R.id.etSearchText);

final EditText etSearch = (EditText)dialog. findViewById(R.id.etSearchText);

答案 2 :(得分:0)

查看v = inflater.inflate(R.layout.dialog_save,null);     builder.setView(V)

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

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });