【问题标题】:How to acces EditText value from AlertDialog Builder from another layout?如何从另一个布局访问来自 AlertDialog Builder 的编辑文本值?
【发布时间】:2015-05-14 16:56:51
【问题描述】:

我的 Config 活动中有这段代码,当我按下 add_count TextView 时对话框被激活,我的问题似乎是我在 get_cont 上获得的值为 null,可能是因为充气机不能正常工作? (用户名和密码都是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"/>

我是不是把代码弄错了?

【问题讨论】:

    标签: android android-alertdialog layout-inflater


    【解决方案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());
               }
    

    【讨论】:

    • 是的,但仍然为空。
    • 嘿,我还测试了新代码并且它正在工作。 :D
    • 我很高兴它成功了,但你没有接受它作为答案。我想知道为什么。 :P
    • 好吧,在我写信告诉你它不起作用之后,我尝试了@Mr.Me 代码,它做得很好。所以这是一个时机问题:D。
    【解决方案2】:

    您正在膨胀 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)
    ....
    

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多