0

我在我的列表视图活动布局顶部显示用户输入的自定义对话框。但是当我尝试在 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();

     }


}
4

1 回答 1

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);
于 2013-02-13T19:22:33.147 回答