欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DialogFragment的使用及利用自定义interface点击事件并解决旋转屏幕点击事件失效问题

程序员文章站 2022-05-16 14:35:27
...

前阵子一直比较忙,所以没怎么写。其实感觉项目中实用好东东还是挺多的,今天就来分享一个DialogFragment的简单使用和利用自定义接口来实现点击事件。
关于DialogFragment和dialog的具体区别,感兴趣的可以自己去了解学习。
因为之前项目是只能竖屏,所以当时没有考虑到横竖屏切换问题,等自己写demo的时候才发现,刚好在这里一并解决此问题。老规矩,先看效果图:
DialogFragment的使用及利用自定义interface点击事件并解决旋转屏幕点击事件失效问题

对就是这个样子。下来看看具体实现
1.ShareScoreDialog

package com.dialogfragmentdemo;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by nanfeilong on 2017/6/6.
 * 查看签到dialog
 */

public class ShareScoreDialog extends DialogFragment implements View.OnClickListener {

    TextView wx, wxq, qq;
    ImageView close;

//    private MyCheckDialogListener myCheckDialogListener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Dialog dialog = null;

            dialog = new Dialog(getActivity(),0);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//代码中取消标题栏
            dialog.setContentView(R.layout.dialog_new_share_layout);
        dialog.getWindow().setBackgroundDrawable(newColorDrawable(android.graphics.Color.TRANSPARENT));
            wx = dialog.findViewById(R.id.share_weichat_dialog); //微信
            wxq = dialog.findViewById(R.id.share_weichat_friendq_dialog);//朋友圈
            qq = dialog.findViewById(R.id.share_qq_dialog);//qq
            close =dialog.findViewById(R.id.close);//右上角的叉
            wx.setOnClickListener(this);
            wxq.setOnClickListener(this);
            qq.setOnClickListener(this);
            close.setOnClickListener(this);
        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                dismiss();
                break;
            case R.id.share_weichat_dialog:
                dismiss();
                if(shareScoreDialogListener!=null)
                shareScoreDialogListener.share(1);
                break;

            case R.id.share_weichat_friendq_dialog:
                dismiss();
                if(shareScoreDialogListener!=null)
                    shareScoreDialogListener.share(2);
                break;

            case R.id.share_qq_dialog:
                dismiss();
                if(shareScoreDialogListener!=null)
                    shareScoreDialogListener.share(3);
                break;

            default:
                break;
        }
    }

    private ShareScoreDialogListener shareScoreDialogListener;
    public void setOnMyCheckDialogListener(ShareScoreDialogListener shareScoreDialogListener) {
        this.shareScoreDialogListener = shareScoreDialogListener;
    }

    public interface ShareScoreDialogListener {
        /**1微信 2朋友圈 3qq*/
        public void share(int type);
    }

}

代码比较简单,主要是这种写法和思路个人感觉比较好。可定制度极高。

然后就是怎么来调用了
2.MainActivity

package com.dialogfragmentdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements ShareScoreDialog.ShareScoreDialogListener {

    private Button button;

    private ShareScoreDialog shareScoreDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState!=null){
            shareScoreDialog = (ShareScoreDialog) getSupportFragmentManager().findFragmentByTag("shareScoreDialog");
        }
        if (shareScoreDialog!=null){
            shareScoreDialog.setOnMyCheckDialogListener(this);
        }
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showShareScoreDialog();
            }
        });
    }

    private void showShareScoreDialog() {
        if (shareScoreDialog == null){
            shareScoreDialog = new ShareScoreDialog();
            shareScoreDialog.setOnMyCheckDialogListener(this);
            shareScoreDialog.show(getSupportFragmentManager(), "shareScoreDialog");
        }else {
            shareScoreDialog.setOnMyCheckDialogListener(this);
            shareScoreDialog.show(getSupportFragmentManager(), "shareScoreDialog");
        }

    }

    @Override
    public void share(int type) {
        /**1微信 2朋友圈 3qq*/
        switch (type) {
            case 1:
                Toast.makeText(this,"微信",Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(this,"朋友圈",Toast.LENGTH_SHORT).show();
                break;
            case 3:
                Toast.makeText(this,"QQ",Toast.LENGTH_SHORT).show();
                break;
        }

    }




}

其中这段是用来解决如果dialog处于show时横竖屏切换后点击事前不起作用的

if (savedInstanceState!=null){
            shareScoreDialog = (ShareScoreDialog) getSupportFragmentManager().findFragmentByTag("shareScoreDialog");
        }
        if (shareScoreDialog!=null){
            shareScoreDialog.setOnMyCheckDialogListener(this);
        }

下来是xml,当时右上角那个xx费了点劲,直接也把这段代码贴上
3. dialog_new_share_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_margin="12dp"
            android:background="@drawable/dialog_circular"
            android:orientation="vertical">

            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/dialog_share_top_bg" />
            </FrameLayout>


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="选择邀请方式"
                android:textColor="@android:color/black"
                android:textSize="16sp" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:text="成功邀请注册后将获得5积分"
                android:textColor="#919191"
                android:textSize="11sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="17dp">

                <TextView
                    android:id="@+id/share_weichat_dialog"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:drawablePadding="5dp"
                    android:drawableTop="@drawable/dialog_fx_wx_selector"
                    android:gravity="center"
                    android:text="微信"
                    android:textColor="#333333" />

                <TextView
                    android:id="@+id/share_weichat_friendq_dialog"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:drawablePadding="5dp"
                    android:drawableTop="@drawable/dialog_fx_wxq_selector"
                    android:gravity="center"
                    android:text="朋友圈"
                    android:textColor="#333333" />

                <TextView
                    android:id="@+id/share_qq_dialog"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:drawablePadding="5dp"
                    android:drawableTop="@drawable/dialog_fx_qq_selector"
                    android:gravity="center"
                    android:text="QQ"
                    android:textColor="#333333" />
            </LinearLayout>

        </LinearLayout>

        <!--叉-->
        <ImageView
            android:id="@+id/close"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/close_dialog" />


    </RelativeLayout>


</LinearLayout>

好了,后面会给出**

源码

**

相关标签: dialog