【问题标题】:How to put logic inside a dialog? Or make dynamic dialog?如何将逻辑放入对话框中?还是做动态对话?
【发布时间】:2018-05-27 09:36:57
【问题描述】:

我有这个对话框,允许用户选择一些复选框和微调器,以自定义项目。

如果我在 // 此处计算自定义的行中添加逻辑,我可以在用户按下保存后计算选项。

但我想在对话框打开时计算选项,并在对话框打开时在对话框中显示动态文本。这可能吗?或者我该怎么做?

btnCustomize.setOnClickListener(new View.OnClickListener(){
    @Override public void onClick(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext(), R.style.CustomDialogTheme);
        builder.setTitle("Customize");
        final View view = inflater.inflate(R.layout.popup_customize, null);
        builder.setView(view);
        builder.setPositiveButton("Save", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                // User clicked OK button
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                // User cancelled the dialog
            }
        });
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener(){
            @Override public void onClick(View v){
                // calculation customization here, after user clicks "Save"
                dialog.dismiss();
            }
        });
    }
});

【问题讨论】:

    标签: android dialog android-alertdialog


    【解决方案1】:

    最简单最好的方法是使用DialogFragment,你可以定义每一个对象并改变它,例如:

    public class CustomDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.CustomDialog2);
        LayoutInflater inflater = getActivity().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.activity_main,null ));
                // Add action buttons
    
        Dialog dialog = builder.create();
        return dialog;
    }  }
    

    在活动中创建并调用它:

    CustomDialog dialog=new CustomDialog();
    dialog.show(getFragmentManager(),"tag");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-08
      • 2012-09-24
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多