从alertDialog构建器获取editText值 [英] Get editText value from alertDialog builder

查看:183
本文介绍了从alertDialog构建器获取editText值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to android. I need to get editText's value to search something. But when I run the program, error comes as null pointer exception.

"EditText etSearch"没有得到它的文本.

"EditText etSearch" isn't getting the text of it.

请帮助我. 谢谢.

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中查找视图,则必须引用Dialog的视图.

For find view from Diloag then you have to give refference of that view of 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);
    }

这篇关于从alertDialog构建器获取editText值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆