【问题标题】:AlertDialog.Builder with custom layout and EditText; cannot access view具有自定义布局和 EditText 的 AlertDialog.Builder;无法访问视图
【发布时间】:2014-03-26 08:48:03
【问题描述】:

我正在尝试使用 EditText 对象创建警报对话框。我需要以编程方式设置EditText 的初始文本。这就是我所拥有的。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null));
EditText editText = (EditText) findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

我需要进行哪些更改才能拥有有效的EditText 对象?

[编辑]

所以,user370305 和其他人指出我应该使用alertDialog.findViewById(R.id.label_field);

不幸的是,这里还有另一个问题。显然,在AlertDialog 上设置内容视图会导致程序在运行时崩溃。您必须在构建器上设置它。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// ...Irrelevant code for customizing the buttons and title
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));
AlertDialog alertDialog = dialogBuilder.create();
LayoutInflater inflater = this.getLayoutInflater();
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
editText.setText("test label");
alertDialog.show();

不幸的是,当您执行此操作时,alertDialog.findViewById(R.id.label_field); 现在会返回 null

[/编辑]

【问题讨论】:

    标签: android


    【解决方案1】:

    editTextalertDialog 布局的一部分,所以只需访问editText 并参考alertDialog

    EditText editText = (EditText) alertDialog.findViewById(R.id.label_field);
    

    更新:

    因为在代码行dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

    inflaterNull

    如下所示更新您的代码,并尝试理解每一行代码

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    // ...Irrelevant code for customizing the buttons and title
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
    dialogBuilder.setView(dialogView);
    
    EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
    editText.setText("test label");
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
    

    更新 2:

    当您使用 Inflater 创建的 View 对象来更新 UI 组件时,您可以直接使用 AlertDialog.Builder 类的 setView(int layourResId) 方法,该方法可从 API 21 及更高版本开始使用。

    【讨论】:

    • 你也可以这样做:dialogBuilder.setView(R.layout.dialog_layout);
    • @SiavA 此方法仅适用于 API 21。
    • 我一直在尝试显示对话框,但它在 RecyclerView 中不起作用,但这个可以。
    • inflater未定义时,您可以使用getLayoutInflater()
    • @saigopi 当你覆盖 onClick 时,会有参数(DialogInterface 对话框,int id)。在这个 onClick 方法中,只需传递 dialog.cancel();
    【解决方案2】:

    用这个

       AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        // Get the layout inflater
        LayoutInflater inflater = (activity).getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the
        // dialog layout
        builder.setTitle(title);
        builder.setCancelable(false);
        builder.setIcon(R.drawable.galleryalart);
        builder.setView(inflater.inflate(R.layout.dialogue, null))
        // Add action buttons
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
    
                        }
                    }
                });
        builder.create();
        builder.show();
    

    【讨论】:

    • 应该是builder.create().show();,你可以查看builder.show();代码了解更多详情
    【解决方案3】:

    你可以写:

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    
     // ...Irrelevant code for customizing the buttons and title
    
    LayoutInflater inflater = this.getLayoutInflater(); 
    
    View dialogView= inflater.inflate(R.layout.alert_label_editor, null);                    
    dialogBuilder.setView(dialogView);
    
    Button button = (Button)dialogView.findViewById(R.id.btnName);
    
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
    
             //Commond here......
    
           }
       });
    
    EditText editText = (EditText)
    dialogView.findViewById(R.id.label_field); 
    
    editText.setText("test label"); 
    
    dialogBuilder.create().show();
    

    【讨论】:

      【解决方案4】:

      如果有人想要在 Kotlin 中使用它:

      val dialogBuilder = AlertDialog.Builder(this)
      // ...Irrelevant code for customizing the buttons and title
      val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
      dialogBuilder.setView(dialogView)
      
      val editText =  dialogView.findViewById(R.id.label_field)
      editText.setText("test label")
      val alertDialog = dialogBuilder.create()
      alertDialog.show()
      

      转贴@user370305's答案。

      【讨论】:

        【解决方案5】:

        改变这个:

        EditText editText = (EditText) findViewById(R.id.label_field);
        

        到这里:

        EditText editText = (EditText)  v.findViewById(R.id.label_field);
        

        【讨论】:

          【解决方案6】:
          View v=inflater.inflate(R.layout.alert_label_editor, null);
          alertDialog.setContentView(v);
          EditText editText = (EditText)v.findViewById(R.id.label_field);
          editText.setText("test label");
          alertDialog.show();
          

          【讨论】:

            【解决方案7】:
            /**
             * Shows  confirmation dialog about signing in.
             */
            private void startAuthDialog() {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();
            
                alertDialog.getWindow().setLayout(800, 1400);
                LayoutInflater inflater = this.getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.auth_dialog, null);
                alertDialog.getWindow().setContentView(dialogView);
                EditText editText = (EditText) dialogView.findViewById(R.id.label_field);
                editText.setText("test label");
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-25
              • 2012-06-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多