美文网首页
一个简单的DialogFragment用例

一个简单的DialogFragment用例

作者: 颤抖的闪电 | 来源:发表于2018-08-31 15:20 被阅读0次
image.png

1,首先编写layout文件zuji_track_switch_dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_track_tips"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="16dp"
        android:lineSpacingExtra="2dp"
        android:text="@string/track_permissions_tips"
        android:textSize="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_sure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="16dp"
        android:text="@string/sure"
        android:textColor="#4588F5"
        android:textSize="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_track_tips" />

    <TextView
        android:id="@+id/tv_cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="16dp"
        android:text="@string/cancel"
        android:textColor="#4588F5"
        android:textSize="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_sure"
        app:layout_constraintTop_toBottomOf="@+id/tv_track_tips" />

</android.support.constraint.ConstraintLayout>

文字资源 /values/string.xml

<string name="track_permissions_tips">已经帮你记录了%d天在%s的旅行,途径%d个城市,游玩%d个停留点。确定现在要放弃记录吗?</string>

2,自定义ZujiTrackSwitchDialog.java

public class ZujiTrackSwitchDialog extends DialogFragment {

    public interface Onclick {
        void onPositive();

        void onNegative();
    }

    private Onclick onclick;

    TextView tv_track_tips, tv_sure, tv_cancle;
    int stayCount, cityCount, dayCount;
    String countryStr;

    public void show(FragmentManager fragmentManager, int stayCount, int cityCount, int dayCount, String countryStr, Onclick onClick) {
        this.stayCount = stayCount;
        this.cityCount = cityCount;
        this.dayCount = dayCount;
        this.countryStr = countryStr;
        this.onclick = onClick;
        show(fragmentManager, "ZujiTrackSwitchDialog");
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.zuji_track_switch_dialog_layout, null);
        String tips = getContext().getString(R.string.track_permissions_tips,
                dayCount, countryStr, cityCount, stayCount);
        tv_track_tips = view.findViewById(R.id.tv_track_tips);
        tv_track_tips.setText(tips);
        tv_sure = view.findViewById(R.id.tv_sure);
        tv_cancle = view.findViewById(R.id.tv_cancle);
        if (onclick != null) {
            tv_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onclick.onPositive();
                    dismiss();
                }
            });
            tv_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onclick.onNegative();
                    dismiss();
                }
            });
        }

        builder.setView(view);
        return builder.create();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onStart() {
        super.onStart();

        //在此设置大小宽高
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            dialog.getWindow().setLayout((int) (displayMetrics.widthPixels * 0.9),
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    //静态调用方法
    public static void show(FragmentActivity mActivity, int stayCount, int cityCount, int dayCount, String countryStr, Onclick onClick) {
        ZujiTrackSwitchDialog dialogFragment = new ZujiTrackSwitchDialog();
        dialogFragment.show(mActivity.getSupportFragmentManager(), stayCount, cityCount, dayCount, countryStr, onClick);
    }
}

3,调用如下,可以自己灵活重写show()方法:

int stayCount = 1;
int cityCount = 1;
int dayCount = 1;
String countryStr = "中国";
ZujiTrackSwitchDialog.show(getActivity(), stayCount, cityCount, dayCount, countryStr, new ZujiTrackSwitchDialog.Onclick() {
                        @Override
                        public void onPositive() {
                        }

                        @Override
                        public void onNegative() {
                        }
                    });

感谢:
Android Dialog使用详解:多种dialog
自定义DialogFragment:关于宽高设置
DialogFragment的使用(自定义Dialog ):关于样式、点击

相关文章

网友评论

      本文标题:一个简单的DialogFragment用例

      本文链接:https://www.haomeiwen.com/subject/opgswftx.html