如何从另一个布局中访问AlertDialog Builder中的EditText值?

时间:2015-05-14 16:56:51

标签: android android-alertdialog layout-inflater

我在Config活动中有这个代码,当我按下add_count TextView时会激活对话框,我的问题似乎是get_cont上的值是null,可能是因为inflater不能正常工作? (用户名和密码是ArrayLists)

add_cont.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

         AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
         LayoutInflater inflater = Config.this.getLayoutInflater();
         final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
         builder.setTitle("Add cont.");
         builder.setView(inflater.inflate(R.layout.dialog_add_cont, null))
         .setPositiveButton(R.string.create_cont, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   EditText get_cont = (EditText)dialog_layout.findViewById(R.id.username);
                   EditText get_pass = (EditText)dialog_layout.findViewById(R.id.password);

                   String test_cont = get_cont.getText().toString();
                   String text_pass = getpass.getText().toString();

                   usernames.add(3, get_cont.getText().toString());
                   passwords.add(3, get_pass.getText().toString());
               }
           })
           .setNegativeButton(R.string.cancel_cont, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
               }
           }); 
         AlertDialog alertDialog = builder.create();
         alertDialog.show();

    }
});

我的dialog_add_cont布局如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
    android:id="@+id/username"
    android:inputType="textEmailAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="username" />

<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="16dp"
    android:fontFamily="sans-serif"
    android:hint="password"/>

我的代码出错了吗?

2 个答案:

答案 0 :(得分:1)

你试过这个吗?

@Override
           public void onClick(DialogInterface dialog, int id) {
               Dialog d = (Dialog) dialog;
               EditText get_cont = (EditText)d.findViewById(R.id.username);
               EditText get_pass = (EditText)d.findViewById(R.id.password);

               String test_cont = get_cont.getText().toString();
               String text_pass = get_cont.getText().toString();

               usernames.add(3, get_cont.getText().toString());
               passwords.add(3, get_pass.getText().toString());
           }

答案 1 :(得分:0)

你正在膨胀dialog_layout并且从不使用它,因为你再次膨胀xml并直接为对话框设置它。

你的代码应该是这样的:

  AlertDialog.Builder builder = new AlertDialog.Builder(Config.this);
  LayoutInflater inflater = Configurare.this.getLayoutInflater();
  final View dialog_layout = inflater.inflate(R.layout.dialog_add_cont, null);
  builder.setTitle("Add cont.");
  builder.setView(dialog_layout)
....