【问题标题】:How to create custom Material Dialog如何创建自定义材质对话框
【发布时间】:2016-05-22 11:22:25
【问题描述】:

我想知道如何为我的应用程序创建自定义材质对话框。具体来说,我需要实现这样的目标

我一直像这样遵循旧方法:

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Sample");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

但是,我在点击button 时遇到空指针异常。任何教程都会对我有很大的帮助。

【问题讨论】:

    标签: android dialog material-design android-alertdialog


    【解决方案1】:

    编写一个扩展到DialogFragment 的新类。

    public class CustomDialog extends DialogFragment {
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.layout_dialog,container,false);
            getDialog().setTitle("Sample");
            Button doneBtn = (Button) mView.findViewById(R.id.done_convert);
            doneBtn.setOnClickListener(doneAction);
            return view;
        }
    
        View.OnClickListener doneAction = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"Test",Toast.LENGTH_LONG).show();
            }
        };
    
    }
    

    然后从你的活动中调用它

    FragmentManager fm = getSupportFragmentManager();
    CustomDialog custom = new CustomDialog();
    custom.show(fm,"");
    

    希望,它会起作用。

    【讨论】:

    • 嘿 @Adnan 我用过这个,但我的主题没有应用于自定义 dialog.xml 文件然后我找到了一个解决方法,即 View view = inflater.inflate(R.layout.layout_dialog,container,错误的);查看视图 = getActivity().getLayoutInflater().inflate(R.layout.layout_dialog, null);;
    【解决方案2】:

    我认为你应该创建自己的类来扩展 DialogFragment

    public class YourDialog extends DialogFragment {
    
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
    
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.custom, null);
    
        Button dialogButton = (Button) dialogView.findViewById(R.id.dialogButtonOK);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(dialogView);
        return builder.create();
    }
    

    并在您的活动中使用此代码显示对话框

    YourDialog yourDialog = new YourDialog();
    yourDialog.show(getFragmentManager(), "YOUR_DIALOG_TAG");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2021-05-27
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-30
      相关资源
      最近更新 更多