简体   繁体   English

具有布局的自定义AlertDialog

[英]custom AlertDialog with layout

Im trying to get the content of the editText, but idont know how to do it. 我试图获取editText的内容,但是idont知道如何做到这一点。 Please help me in this.(Im using the method showDialog) 请帮助我。(我使用showDialog方法)

protected  Dialog onCreateDialog(int id, Bundle args)
{
        AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        builder2.setView(inflater.inflate(R.layout.dialog_search_teacher,null));
        final EditText price,city;
        price=(EditText)findViewById(R.id.price_search);
        city=(EditText)findViewById(R.id.city_search);
        builder2.setPositiveButton("Serach", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                serach(city.getText().toString(),price.getText().toString());

            }
        });
        builder2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });
        AlertDialog alert2 = builder2.create();
        return(alert2);}

If you need to get the text from the EditText you have to do this: 如果需要从EditText获取文本,则必须执行以下操作:

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);

and then, on the onclick() method you have to retrieve the text: 然后,必须在onclick()方法上检索文本:

city.getText().toString();

but if you want to pass the string to the parent class you have to do an interface like this: http://developer.android.com/training/basics/fragments/communicating.html 但是,如果要将字符串传递给父类,则必须执行如下所示的接口: http : //developer.android.com/training/basics/fragments/communicating.html

You have to find editText id like this 您必须找到像这样的editText id

price=(EditText)builder2.findViewById(R.id.price_search); price =(EditText)builder2.findViewById(R.id.price_search);

You should declaration your editText under the dialog method like, 您应在对话框方法下声明您的editText,

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_search_teacher, null);
city=(EditText)view.findViewById(R.id.city_search);

After that, while doing onClick listener for postive button, you can get the content of editText using 之后,在对正按钮执行onClick侦听器时,可以使用以下命令获取editText的内容

city.getText().toString();

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM