Android Dialog 的一些使用和优化心得(DialogFragment的使用和优化)

在我们的Android Application 中,会经常使用一些不太重要的提示或者确认,例如Toast,Dialog,popuwindow....等

Dialog使用有时候也会很多,基本使用要么继承dialog,实现业务逻辑,要么是new AlertDialog.Builer().setXX.show()

但遇到屏幕翻转 或者其他因素,会导致数据不会保存,影响用户体验,所以DialogFragment就派上用场了,Google官方控件

先看一下它的结构

V4包下的继承于Fragment,那么继承了Fragment  像 onCreateView , onResume  onStop等等方法也是可以使用的,大家都知道Fragment的生命周期,所以DialogFragment就可以处理像屏幕翻转的一些业务数据的恢复和优化,

创建视图:

这是一个简单的退出提示的Dialog,我们也可以将布局扩展为可输入的交互效果,如果需要传递参数,可以将Dialog当做Fragment通过setArgument(Bundle bundler)进行传值,在生命周期内将数据进行合理的处理。

代码:

package com.android.angola.wenna.plus.ui.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.angola.wenna.R;

public class ExitFragment extends DialogFragment
{
    private View rootView;
    private TextView tv_yes;
    private TextView tv_no;
    private OnYesClickListener onYesClickListener;
    private onNoClickListener onNoClickListener;

    public void setOnNoClickListener(ExitFragment.onNoClickListener onNoClickListener) {
        this.onNoClickListener = onNoClickListener;
    }

    public void setOnYesClickListener(OnYesClickListener onYesClickListener) {
        this.onYesClickListener = onYesClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.fragment_exit,container,false);
            this.tv_yes = rootView.findViewById(R.id.tv_dialog_yes);
            this.tv_no = rootView.findViewById(R.id.tv_dialog_no);
        }

        return rootView;
    }

    public interface OnYesClickListener{
        void onYesClickener();
    }

    public interface onNoClickListener{
        void onNoClicked();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.tv_yes.setOnClickListener(v -> onYesClicked());
        this.tv_no.setOnClickListener(v -> onNoClicked());
    }

    private void onNoClicked() {
        if (onNoClickListener != null)
        {
            onNoClickListener.onNoClicked();
        }
    }

    private void onYesClicked() {
        if (onYesClickListener != null)
        {
            onYesClickListener.onYesClickener();
        }
    }
}

布局:

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


    <TextView
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:background="@drawable/ripper_white_bg"
        android:textSize="16sp"
        android:textColor="#4A4A4A"
        android:id="@+id/tv_dialog_title"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:gravity="center_horizontal"
        android:text="@string/tv_confirm_exit_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <View
        android:id="@+id/view_dialog_line_hor"
        android:layout_marginTop="6dp"
        app:layout_constraintTop_toBottomOf="@id/tv_dialog_title"
        android:background="@color/black_ef"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <TextView
        android:layout_marginBottom="20dp"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/tv_dialog_no"
        android:background="@drawable/ripper_white_bg"
        app:layout_constraintRight_toLeftOf="@id/view_dialog_ver_line"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textColor="#9B9B9B"
        android:textSize="14sp"
        android:layout_marginTop="20dp"
        android:text="@string/tv_confirm_no"
        app:layout_constraintTop_toBottomOf="@id/view_dialog_line_hor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <View
        android:id="@+id/view_dialog_ver_line"
        android:layout_marginTop="20dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view_dialog_line_hor"
        android:background="@color/black_ef"
        android:layout_width="1dp"
        android:layout_height="40dp" />

    <TextView
        android:layout_marginBottom="20dp"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:id="@+id/tv_dialog_yes"
        android:background="@drawable/ripper_white_bg"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/view_dialog_ver_line"
        android:textSize="14sp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toBottomOf="@id/view_dialog_line_hor"
        android:text="@string/tv_confirm_yes"
        android:textColor="@android:color/holo_red_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
发布了42 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_29769851/article/details/96311491
今日推荐