无法从自定义片段对话框获取输入

时间:2013-02-13 19:13:08

标签: android

我正在列表视图活动布局的顶部显示用户输入的自定义对话框。但是当我尝试在onCreateDialog()方法中引用编辑文本时,它总是变为空白。以下是我的代码,如果有人可以指出我做错了哪一点,我们会感激不尽 -

public class QuestionDialog extends DialogFragment
{

     public Dialog onCreateDialog(Bundle savedInstanceState) 
     {

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
         // Inflate and set the layout for the dialog
            final View v =inflater.inflate(R.layout.ques_dialog,null);
            builder.setView(inflater.inflate(R.layout.ques_dialog, null))

            .setTitle("Title Message!")  
            .setCancelable(true)
            .setPositiveButton("GO", new DialogInterface.OnClickListener(){
                       public void onClick(DialogInterface dialog, int whichButton)
                        {

                          EditText et1 = (EditText)v.findViewById(R.id.et1);

                      // here not able to get the data from edit text 

                          String s1 = et1.getText().toString().trim();
                          Log.d("debug","data -"+s1+" and length"+s1.length());

                        .... rest code goes here 

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

                            ..rest code goes here

                         }

                       });              

        return builder.create();

     }


}

1 个答案:

答案 0 :(得分:0)

正是因为这条线而发生:

final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(inflater.inflate(R.layout.ques_dialog, null))

您首先对 v View进行充气,然后将新增的视图设置为对话框的内容(所以最后您最终需要查找视图不是对话框的实际内容)。使用此:

final View v =inflater.inflate(R.layout.ques_dialog,null);
builder.setView(v);