【问题标题】:android AlertDialog setView rulesandroid AlertDialog setView 规则
【发布时间】:2013-01-27 21:15:01
【问题描述】:

AlertDialog 类的setView() 方法允许为对话框指定自定义视图。对于可以在此自定义视图中包含哪些控件有任何限制吗?

另外,如果我们设置一个自定义视图,我们还可以使用setPositiveButton()setNegativeButton()等添加按钮吗?

【问题讨论】:

    标签: android android-alertdialog android-custom-view


    【解决方案1】:

    AlertDialog 类的 setView() 方法允许为对话框指定自定义视图。对于可以在此自定义视图中包含哪些控件有任何限制吗?

    AlertDialog.Builder 中的setView() 方法采用从View 扩展的任何类(参见它的子类及其子类)。

    这意味着 EditTexts、Buttons 等。但也包括从 viewGroups 扩展的布局。

    另外,如果我们设置一个自定义视图,我们还能使用 setPositiveButton、setNegativeButton 等添加按钮吗?

    是的,它只影响身体。 在布局下方添加按钮。

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog
        // layout
        builder.setView(inflater.inflate(R.layout.YourLayout, null))
            .setPositiveButton(AlertDialog.BUTTON_NEGATIVE, "Yes!",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        //
                    }
             })
            .setNegativeButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        LoginDialogFragment.this.getDialog().cancel();
                    }
             });
        return builder.create();
    }
    

    更新:

    自 2 年前以来,这个答案似乎有了一些新的活动,并且有些事情发生了变化。

    由于当前的最佳实践状态,我稍微更新了代码以改进格式并添加了以下提示。

    AlertDialog 定义了对话框的样式和结构,但是 您应该使用 DialogFragment 作为对话框的容器。这 DialogFragment 类提供了创建所需的所有控件 对话框并管理其外观,而不是调用 对话框对象。

    上面的例子是指当你扩展DialogFragment并在onCreateDialog()回调方法中创建一个AlertDialog

    【讨论】:

    • 我想在这里按照你的回答。但是,每当我输入LayoutInflater inflater = getLayoutInflater(); 行时,我总是会收到一条错误消息,指出方法调用需要 Bundle 参数。我尝试使用 onCreateDialog 参数中的 savedInstanceState,但这会导致它自己的新错误。
    • 你可能在一个片段中,需要getActivity().getLayoutInflater()LayoutInflater.from(getActivity())
    • 由于 SdkVersion 21 builder.setView() 可以采用布局资源。这样您就不必获得LayoutInflater。例如:builder.setView(R.layout.your_layout)
    【解决方案2】:

    在为AlertDialog 提供的文档中,您可以在 AlertDialog 的视图中设置的内容没有任何限制。

    因此自定义视图将位于对话框标题下方和按钮上方,根本不会受到影响。

    【讨论】:

      【解决方案3】:

      据我所知,您可以在 setView() 中添加任何您想要的内容。正/负按钮不受影响。

      【讨论】:

        【解决方案4】:

        试试这个例子

          android.support.v7.app.AlertDialog.Builder adb =
            new android.support.v7.app.AlertDialog.Builder(YoreActivity.this);
        
          ViewGroup subView = (ViewGroup) getLayoutInflater().// inflater view
                              inflate(R.layout.yore_layout_alert, null, false);
        
          try {// set data of yore layout
        
             ((TextView) subView.findViewById(R.id.messageAlert))//get element TextView 
                                                    .setText(SomeText);//set text
        
          } catch (NullPointerException npe) {
            npe.printStackTrace();
          }
        
          adb.setPositiveButton("textPOSITIVE", new DialogInterface.OnClickListener() {//one method go
            @Override
            public void onClick(DialogInterface dialog, int which) {
              //  TODO some code
            }
          });//one method end
        
          final android.support.v7.app.AlertDialog alertDialog =
                adb.setTitle(SomeText)// set ttile 
                .setView( subView ).create();// add view in AlertDialog.Builder, and create AlertDialog
        
          try { //two method go
        
            ((Button) subView.findViewById(R.id.customPositivButton))
            .setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                //  TODO some code
                alertDialog.dismiss();
              }
            });
        
          } catch (NullPointerException npe) {
            npe.printStackTrace();
          }  //two method end
        
          alertDialog.show(); //show in YoreActivity
        

        【讨论】:

          猜你喜欢
          • 2022-01-22
          • 1970-01-01
          • 2015-08-10
          • 1970-01-01
          • 2012-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          相关资源
          最近更新 更多