自定义DialogFragment无法正常工作

时间:2016-04-03 15:22:51

标签: android android-appcompat android-dialogfragment dialogfragment appcompatdialogfragment

我正在尝试使用dialogFragment创建自定义对话框,这里我无法显示对话框。主要问题是覆盖代码没有被调用。任何人都可以解决这个问题这是我的代码:

BaseDialogFragment.java

public class BaseDialogFragment extends DialogFragment {
    private int layoutId;
    protected Activity mActivity;

    public void setLayoutId(int layoutId){
        this.layoutId = layoutId;
    }

    public BaseDialogFragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setStyle(BaseDialogFragment.STYLE_NO_TITLE, R.style.share_dialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View v = inflater.inflate(layoutId, container, false);
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    public void initViews(View v){
        getDialog().setCanceledOnTouchOutside(true);
    }
}

CustomDialog.java:

@SuppressLint("ValidFragment")
public class CustomDialog extends BaseDialogFragment  {
    private String message;
    private btnOkClick okClickListerner;
    private TextView simpleMsg;
    private WebView termsConditionWeb;
    private Button okBtn;
    Boolean isNormalDialog = false;
    private Typeface fontClanProBold;
    private View v;
    private Context context;

    public interface btnOkClick{
        void clicked();
    }

    public CustomDialog(String message, btnOkClick okClickListerner, Boolean isNormalDialog){
        this.message = message;
        this.okClickListerner = okClickListerner;
        this.isNormalDialog = isNormalDialog;
        this.mActivity = null;
        setLayoutId(R.layout.activity_custom_dialog);
        initViews(v);
           }

    @Override
    public void initViews(View v) {
        super.initViews(v);
        this.simpleMsg = (TextView) v.findViewById(R.id.simpleMsg);
        this.termsConditionWeb= (WebView) v.findViewById(R.id.termsConditionWeb);
        this.okBtn = (Button) v.findViewById(R.id.okBtn);
        fontClanProBold = Typeface.createFromAsset(context.getAssets(), "fonts/ufonts.com_clanpro-bold.ttf");
        Log.e("isNormal", isNormalDialog.toString());
        if(isNormalDialog){
            this.simpleMsg.setVisibility(View.VISIBLE);
            this.simpleMsg.setText(message);
            this.simpleMsg.setTypeface(fontClanProBold);
        } else {
            this.termsConditionWeb.setVisibility(View.VISIBLE);
            this.termsConditionWeb.loadData(message, "text/html", "UTF-8");
        }
        setCancelable(false);
        initEvent(v);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    private void initEvent(View v){
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(okClickListerner != null){
                    okClickListerner.clicked();
                }
                dismiss();
            }
        });
    }

    public static void ShowDialog(FragmentManager fm, String message, btnOkClick okClickListerner, Boolean isNormalDialog){
        CustomDialog dialog = new CustomDialog(message, okClickListerner, isNormalDialog);
        dialog.show(fm, "");
    }
}

MainActivity.java

在onClickListener

CustomDialog.ShowDialog(getSupportFragmentManager(), getResources().getString(R.string.message_register), new CustomDialog.btnOkClick() {
                    @Override
                    public void clicked() {
                        finish();
                    }
                }, isNormalDialog);

1 个答案:

答案 0 :(得分:0)

在Dialog构造函数中设置值是不好的做法。而是将您的值作为参数传递,并在onCreate回调上初始化它们。此外,您应避免在片段上保存活动的实例,这可能会导致内存泄漏。相反,我建议您在CustomDialog或BaseDialogFragment中创建一个接口,以便所有使用它们的活动都必须实现。然后,您需要在Dialog上实现onClickListener接口,在其中可以调用mListener.onButtonClickListener()。请参阅示例DialogFragment

您的CustomDialog看起来像:

public class CustomDialog extends BaseDialogFragment {

private myFragmentInterface mListener;

public static CustomDialog newInstance(String message, Boolean isNormalDialog){
    Bundle args = new Bundle();
    args.putString(MESSAGE_ARG_KEY, message);
    args.putBoolean(TYPE_ARG_KEY, isNormalDialog);
    CustomDialog instance = new CustomDialog();
    instance.setArguments(args);
}

@override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    message = getArguments().getStirng(MESSAGE_ARG_KEY);
    isNormalDialog = getArguments().getBoolean(TYPE_ARG_KEY);
}

@override
public void onAttach(Activity activity){
    super.onAttach();
    try{
        mListener = (myFragmentInterface) activity;
    }catch(ClassCastException e){
        throw new ClassCastException("activiy must implement myFragmentInterface");
    }
}

public void onDetach(){
    super.onDetach();
    mListener = null;
}

public interface myFragmentInterface{
    onButtonClickListener(String... params);
}
}