ArticleRemoveDelDialog【基於AlertDialog的回收刪除對話框】

版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!java

前言

回收刪除對話框,繼承AlertDialog。android

仿照釘釘的長按彈出的移除置頂對話框。git

效果圖

代碼分析

繼承AlertDialog;github

實現標題文本變化的狀況下,對話框寬度保持不變。app

使用步驟

1、項目組織結構圖

注意事項:ide

一、  導入類文件後須要change包名以及從新import R文件路徑佈局

二、  Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋ui

2、導入步驟

將ArticleRemoveDelDialog複製到項目中(注意,須要從新import R文件)

package com.why.project.articleremovedeldialogdemo.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.why.project.articleremovedeldialogdemo.R;


/**
 * Created by HaiyuKing
 * Used 回收、刪除對話框
 */

public class ArticleRemoveDelDialog extends AlertDialog {
    private static final String TAG = ArticleRemoveDelDialog.class.getSimpleName();

    private Context context;//上下文
    private String buildTitle;//標題

    private TextView title_tv;
    private TextView remove_tv;
    private TextView del_tv;

    public ArticleRemoveDelDialog(Context context, String buildTitle) {
        super(context, R.style.style_alert_confirm_dialog);

        //設置爲false,按對話框之外的地方不起做用
        setCanceledOnTouchOutside(true);
        //設置爲false,按返回鍵不能退出
        setCancelable(true);

        this.context = context;
        this.buildTitle = buildTitle;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog_remove_del);

        initViews();
        initDatas();
        initEvents();
    }

    /**初始化view*/
    private void initViews() {
        title_tv = findViewById(R.id.title_tv);
        remove_tv = findViewById(R.id.remove_tv);
        del_tv = findViewById(R.id.del_tv);
    }

    /**初始化數據*/
    private void initDatas() {
        title_tv.setText(buildTitle);//設置標題
        //設置標題區域的寬度
        final DisplayMetrics display = new DisplayMetrics();
        ((Activity)this.context).getWindowManager().getDefaultDisplay().getMetrics(display);
        title_tv.setLayoutParams(new LinearLayout.LayoutParams(display.widthPixels - context.getResources().getDimensionPixelOffset(R.dimen.alert_dialog_confirm_margin) * 2, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    /**初始化監聽事件*/
    private void initEvents() {
        //回收
        remove_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnRemoveDelButtonClickListener != null){
                    mOnRemoveDelButtonClickListener.onRemoveButtonClick();
                }
                dismiss();
            }
        });
        //刪除
        del_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnRemoveDelButtonClickListener != null){
                    mOnRemoveDelButtonClickListener.onDelButtonClick();
                }
                dismiss();
            }
        });
    }

    public static abstract interface OnRemoveDelButtonClickListener
    {
        //回收按鈕的點擊事件接口
        public abstract void onRemoveButtonClick();
        //刪除按鈕的點擊事件接口
        public abstract void onDelButtonClick();
    }

    private OnRemoveDelButtonClickListener mOnRemoveDelButtonClickListener;

    public void setOnRemoveDelButtonClickListener(OnRemoveDelButtonClickListener mOnRemoveDelButtonClickListener)
    {
        this.mOnRemoveDelButtonClickListener = mOnRemoveDelButtonClickListener;
    }


}
ArticleRemoveDelDialog.java

在res/drawable目錄中添加如下文件

將默認圖標複製到項目中

將alert_dialog_remove_del.xml佈局文件複製到項目中

在colors.xml文件中添加如下代碼

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <!-- ==================確認取消對話框(alertdialog)================== -->
    <!-- 對話框的填充顏色 -->
    <color name="alert_dialog_confirm_solid_color">#ffffffff</color>
</resources>

在dimens.xml文件中添加如下代碼

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- ==================確認取消對話框(alertdialog)================== -->
    <!-- 對話框的圓角角度 -->
    <dimen name="alert_dialog_confirm_corners">5dp</dimen>
    <!-- 對話框的總體外邊距 -->
    <dimen name="alert_dialog_confirm_margin">30dp</dimen>
</resources>

在styles.xml文件中添加如下代碼

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- ==================確認取消對話框(alertdialog)================== -->
    <style name="style_alert_confirm_dialog">
        <!-- Dialog的windowFrame框爲無 -->
        <item name="android:windowFrame">@null</item>
        <!-- 是否顯示title -->
        <item name="android:windowNoTitle">false</item>
        <!-- 是否浮如今activity之上 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 半透明 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 設置dialog的背景:#00000000透明色 -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- 背景變灰:整個屏幕變灰,配合setCanceledOnTouchOutside(false) -->
        <item name="android:backgroundDimEnabled">true</item>
    </style>

</resources>

3、使用方法

package com.why.project.articleremovedeldialogdemo;

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

import com.why.project.articleremovedeldialogdemo.dialog.ArticleRemoveDelDialog;

public class MainActivity extends AppCompatActivity {

    private Button btn_open;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initEvents();
    }

    private void initViews() {
        btn_open = findViewById(R.id.btn_open);
    }

    private void initEvents() {
        btn_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArticleRemoveDelDialog articleRemoveDelDialog = new ArticleRemoveDelDialog(MainActivity.this,"這是一個標題,可能會很長文本,若是超過寬度的話,會省略號"); articleRemoveDelDialog.setOnRemoveDelButtonClickListener(new ArticleRemoveDelDialog.OnRemoveDelButtonClickListener() { @Override public void onRemoveButtonClick() { Toast.makeText(MainActivity.this,"回收",Toast.LENGTH_SHORT).show(); } @Override public void onDelButtonClick() { Toast.makeText(MainActivity.this,"刪除",Toast.LENGTH_SHORT).show(); } }); articleRemoveDelDialog.show();
            }
        });
    }
}

混淆配置

this

參考資料

暫時空缺spa

項目demo下載地址

https://github.com/haiyuKing/ArticleRemoveDelDialogDemo

相關文章
相關標籤/搜索