自定义AlertDialog与布局

时间:2015-05-12 14:47:23

标签: android android-alertdialog

我试图获取editText的内容,但idont知道如何做到这一点。请帮助我。(我使用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);}

3 个答案:

答案 0 :(得分:1)

如果您需要从EditText获取文本,则必须执行此操作:

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

然后,在onclick()方法中,您必须检索文本:

city.getText().toString();

但是如果你想将字符串传递给父类,你必须这样做一个接口: http://developer.android.com/training/basics/fragments/communicating.html

答案 1 :(得分:-2)

你必须找到像这样的editText id

价=(EditText上)builder2.findViewById(R.id.price_search);

答案 2 :(得分:-2)

您应该在对话框方法下声明您的editText,如

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

之后,在为ontive按钮执行onClick侦听器时,可以使用

获取editText的内容
city.getText().toString();

希望这会对你有所帮助。