【问题标题】:Alertdialog setView() using existing xml widget [duplicate]使用现有 xml 小部件的 Alertdialog setView() [重复]
【发布时间】:2015-08-13 11:36:52
【问题描述】:

我在带有 LinearLayout 的 .xml 文件中有一个 EditText,并希望将其添加为 AlertDialog 上的 setView() 参数。这可能吗?这是我尝试过的:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setView(input);

但是启动时对话框是空白的。我做错了什么?

【问题讨论】:

    标签: java android


    【解决方案1】:

    这样使用

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.your_layout, null);
    builder.setView(dialogView);
    
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
    

    使用dialogView,您可以像这样获得EditText

    EditText editText = (EditText) dialogView.findViewById(R.id.your_edit_text);
    

    【讨论】:

      【解决方案2】:

      可能有帮助

       AlertDialog.Builder builder = new AlertDialog.Builder(this);
         LayoutInflater inflater = getLayoutInflater();
      builder.setView(inflater.inflate(R.layout.YourLayout, null));
      AlertDialog ad = builder.create();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-22
        • 2015-08-10
        • 2013-01-27
        • 1970-01-01
        • 2012-09-04
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多