Android之簡單的實現(xiàn)一個自定義Dialog

效果圖:


前言:

最近在項目中遇到一個問題稼稿,就是啟動頁彈窗提示一個隱私政策的操作薄榛。我一開始用的是Dialog,但是為了符合公司的"審美"(裝B),所以為了使彈窗不那么單調(diào)让歼,我換成了PopupWindow,上線一段時間后敞恋,隨著用戶的增加,出現(xiàn)了問題谋右,讓我很是頭痛硬猫。原因是,在Android6.0之后的版本改执,彈窗都是正常居中顯示啸蜜,但是Android6.0之前(包含6.0)彈窗消失了。同時我給設(shè)置了彈窗出現(xiàn)時辈挂,點擊外部不能取消衬横,所以導(dǎo)致6.0的用戶在使用時,卡在了歡迎界面终蒂。網(wǎng)上找了很多博客蜂林,也沒有解決我的問題,所以后豫,又換回了DIalog悉尾。不同的是 為了美觀,實現(xiàn)一個自定義Dialog挫酿。

一构眯、創(chuàng)建自定義Dialog

1.創(chuàng)建一個類:CommonDialog并繼承Dialog。

下面附上我的代碼:

public class CommonDialog extends Dialog {

/**

? ? * 顯示的圖片

? ? */

? ? private ImageViewimageIv ;

? ? /**

? ? * 顯示的標(biāo)題

? ? */

? ? private TextViewtitleTv ;

? ? /**

? ? * 顯示的消息

? ? */

? ? private TextViewmessageTv ;

? ? /**

? ? * 確認(rèn)和取消按鈕

? ? */

? ? private ButtonnegtiveBn ,positiveBn;

? ? /**

? ? * 按鈕之間的分割線

? ? */

? ? private ViewcolumnLineView ;

? ? private TextViewprivacy;

? ? public CommonDialog(Context context) {

super(context, R.style.CustomDialog);

? ? }

/**

? ? * 都是內(nèi)容數(shù)據(jù)

? ? */

? ? private Stringmessage;

? ? private Stringtitle;

? ? private Stringpositive,negtive ;

? ? private int imageResId = -1 ;

? ? /**

? ? * 底部是否只有一個按鈕

? ? */

? ? private boolean isSingle =false;

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

????????super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.base_layout_dialog);

? ? ? ? //按空白處不能取消動畫

? ? ? ? setCanceledOnTouchOutside(false);

? ? ? ? //初始化界面控件

? ? ? ? initView();

? ? ? ? //初始化界面數(shù)據(jù)

? ? ? ? refreshView();

? ? ? ? //初始化界面控件的事件

? ? ? ? initEvent();

? ? }

/**

? ? * 初始化界面的確定和取消監(jiān)聽器

? ? */

? ? private void initEvent() {

????????//設(shè)置確定按鈕被點擊后早龟,向外界提供監(jiān)聽

? ? ? ? positiveBn.setOnClickListener(new View.OnClickListener() {

????????????@Override

? ? ? ? ? ? public void onClick(View v) {

????????????????????if (onClickBottomListener!=null) {

????????????????????????onClickBottomListener.onPositiveClick();

? ? ? ? ? ? ? ? ????}

????????????}

????});

? ? ? ? //設(shè)置取消按鈕被點擊后惫霸,向外界提供監(jiān)聽

? ? ? ? negtiveBn.setOnClickListener(new View.OnClickListener() {

????????????@Override

? ? ? ? ? ? public void onClick(View v) {

????????????????????if (onClickBottomListener!=null) {

????????????????????????????onClickBottomListener.onNegtiveClick();

? ? ? ? ? ? ? ????? }

? ? ? ? ? ? }

????});

? ? ? ? //設(shè)置隱私政策被點擊后,向外界提供監(jiān)聽

? ? ? ? privacy.setOnClickListener(new View.OnClickListener() {

????????????@Override

? ? ? ? ? ? public void onClick(View view) {

????????????????if(onClickBottomListener!=null){

????????????????????????onClickBottomListener.onPrivacyClick();

? ? ? ? ? ? ? ? ????}

????????????}

????});

}

/**

? ? * 初始化界面控件的顯示數(shù)據(jù)

? ? */

? ? private void refreshView() {

//如果用戶自定了title和message

? ? ? ? if (!TextUtils.isEmpty(title)) {

????????????????titleTv.setText(title);

? ? ? ? ? ? ????titleTv.setVisibility(View.VISIBLE);

? ? ? ? }else {

????????????????titleTv.setVisibility(View.GONE);

? ? ? ? }

????????if (!TextUtils.isEmpty(message)) {

????????????????messageTv.setText(message);

? ? ? ? }

????????//如果設(shè)置按鈕的文字

? ? ? ? if (!TextUtils.isEmpty(positive)) {

????????????????positiveBn.setText(positive);

? ? ? ? }else {

????????????????positiveBn.setText("確定");

? ? ? ? }

????????if (!TextUtils.isEmpty(negtive)) {

????????????????negtiveBn.setText(negtive);

? ? ? ? }else {

????????????????negtiveBn.setText("取消");

? ? ? ? }

????????if (imageResId!=-1){

i????????????????mageIv.setImageResource(imageResId);

? ? ? ? ? ? ????imageIv.setVisibility(View.VISIBLE);

? ? ? ? }else {

????????????????imageIv.setVisibility(View.GONE);

? ? ? ? }

/**

? ? ? ? * 只顯示一個按鈕的時候隱藏取消按鈕葱弟,回掉只執(zhí)行確定的事件

? ? ? ? */

? ? ? ? if (isSingle){

? ? ? ? ? ? columnLineView.setVisibility(View.GONE);

? ? ? ? ? ? negtiveBn.setVisibility(View.GONE);

? ? ? ? }else {

????????????negtiveBn.setVisibility(View.VISIBLE);

? ? ? ? ? ? columnLineView.setVisibility(View.VISIBLE);

? ? ? ? }

}

????@Override

? ? public void show() {

????????????super.show();

? ? ? ? ????refreshView();

? ? ????}

/**

? ? * 初始化界面控件

? ? */

? ? private void initView() {

????????negtiveBn = (Button) findViewById(R.id.negtive);

? ? ? ? positiveBn = (Button) findViewById(R.id.positive);

? ? ? ? titleTv = (TextView) findViewById(R.id.title);

? ? ? ? messageTv = (TextView) findViewById(R.id.message);

? ? ? ? imageIv = (ImageView) findViewById(R.id.image);

? ? ? ? columnLineView = findViewById(R.id.column_line);

? ? ? ? privacy = findViewById(R.id.privacy);

? ? }

/**

? ? * 設(shè)置確定取消按鈕的回調(diào)

? ? */

? ? public OnClickBottomListeneronClickBottomListener;

? ? public CommonDialogsetOnClickBottomListener(OnClickBottomListener onClickBottomListener) {

????????????this.onClickBottomListener = onClickBottomListener;

????????????return this;

? ? }

public interface OnClickBottomListener{

/**

? ? ? ? * 點擊確定按鈕事件

? ? ? ? */

? ? ? ? public void onPositiveClick();

? ? ? ? /**

? ? ? ? * 點擊取消按鈕事件

? ? ? ? */

? ? ? ? public void onNegtiveClick();

? ? ? ? /**

? ? ? ? * 點擊隱私政策事件

? ? ? ? */

? ? ? ? public void onPrivacyClick();

? ? }

public StringgetMessage() {

return message;

? ? }

public CommonDialogsetMessage(String message) {

this.message = message;

return this ;

? ? }

public StringgetTitle() {

return title;

? ? }

public CommonDialogsetTitle(String title) {

this.title = title;

return this ;

? ? }

public StringgetPositive() {

return positive;

? ? }

public CommonDialogsetPositive(String positive) {

this.positive = positive;

return this ;

? ? }

public StringgetNegtive() {

return negtive;

? ? }

public CommonDialogsetNegtive(String negtive) {

this.negtive = negtive;

return this ;

? ? }

public int getImageResId() {

return imageResId;

? ? }

public boolean isSingle() {

return isSingle;

? ? }

public CommonDialogsetSingle(boolean single) {

isSingle = single;

return this ;

? ? }

public CommonDialogsetImageResId(int imageResId) {

this.imageResId = imageResId;

return this ;

? ? }

}


上面的代碼自己看看 然后你就可以直接復(fù)制拿去用了壹店。

然后補(bǔ)充一個style文件 和xml文件

style:

style

xml:

<?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="wrap_content"

? ? android:orientation="vertical"

? ? android:layout_centerInParent="true"

? ? android:paddingTop="16dp"

? ? android:background="@drawable/base_dialog_bg"

? ? xmlns:tools="http://schemas.android.com/tools">

? ? ? ? android:id="@+id/title"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_gravity="center"

? ? ? ? android:layout_marginLeft="16dp"

? ? ? ? android:layout_marginRight="16dp"

? ? ? ? android:layout_marginBottom="16dp"

? ? ? ? android:gravity="center"

? ? ? ? tools:text="消息提示"

? ? ? ? android:visibility="visible"

? ? ? ? android:textColor="#333333"

? ? ? ? android:textSize="18sp" />

? ? ? ? android:id="@+id/image"

? ? ? ? tools:src="@mipmap/ic_launcher"

? ? ? ? android:layout_gravity="center"

? ? ? ? android:maxHeight="150dp"

? ? ? ? android:maxWidth="150dp"

? ? ? ? android:layout_marginBottom="10dp"

? ? ? ? android:layout_marginLeft="16dp"

? ? ? ? android:layout_marginRight="16dp"

? ? ? ? android:visibility="visible"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content" />

? ? ? ? android:layout_gravity="center"

? ? ? ? android:layout_width="300dp"

? ? ? ? android:layout_height="400dp">

? ? ? ? ? ? android:id="@+id/message"

? ? ? ? ? ? android:layout_width="fill_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:gravity="center|left"

? ? ? ? ? ? android:layout_marginLeft="20dp"

? ? ? ? ? ? android:layout_marginRight="20dp"

? ? ? ? ? ? android:lineSpacingExtra="3dp"

? ? ? ? ? ? android:lineSpacingMultiplier="1.2"

? ? ? ? ? ? android:textSize="14dp"

? ? ? ? ? ? android:textColor="#999999"

? ? ? ? ? ? tools:text="提示消息" />

? ? ? ? android:id="@+id/privacy"

? ? ? ? android:textSize="@dimen/dp_14"

? ? ? ? android:textColor="#38ADFF"

? ? ? ? android:layout_marginTop="@dimen/dp_16"

? ? ? ? android:layout_gravity="center"

? ? ? ? android:text="隱私政策"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"/>

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="1px"

? ? ? ? android:layout_marginTop="16dp"

? ? ? ? android:background="#E4E4E4" />

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:orientation="horizontal">

? ? ? ? ? ? android:id="@+id/negtive"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_marginLeft="10dp"

? ? ? ? ? ? android:paddingTop="16dp"

? ? ? ? ? ? android:paddingBottom="16dp"

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:background="@null"

? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? android:singleLine="true"

? ? ? ? ? ? tools:text="No"

? ? ? ? ? ? android:textColor="#999999"

? ? ? ? ? ? android:textSize="16sp" />

? ? ? ? ? ? android:id="@+id/column_line"

? ? ? ? ? ? android:layout_width="1px"

? ? ? ? ? ? android:layout_height="match_parent"

? ? ? ? ? ? android:background="#E4E4E4" />

? ? ? ? ? ? android:id="@+id/positive"

? ? ? ? ? ? android:layout_width="0dp"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:layout_marginRight="10dp"

? ? ? ? ? ? android:layout_weight="1"

? ? ? ? ? ? android:paddingTop="16dp"

? ? ? ? ? ? android:paddingBottom="16dp"

? ? ? ? ? ? android:background="@null"

? ? ? ? ? ? android:gravity="center"

? ? ? ? ? ? android:singleLine="true"

? ? ? ? ? ? tools:text="Yes"

? ? ? ? ? ? android:textColor="#38ADFF"

? ? ? ? ? ? android:textSize="16sp" />

</LinearLayout>


以上為止,我們所需的東西就全部湊齊了芝加。接下來看用法:

在我們需要彈窗的Activity或者fragment中創(chuàng)建一個方法:initDialog();

方法中硅卢,new一個我們剛創(chuàng)建的Dialog類。

CommonDialog commonDialog =new CommonDialog(上下文對象);

commonDialog .setTitle("標(biāo)題")

? ? ? ? ? ? ? ? ? ? ? ? ? .setMessage("提示內(nèi)容")

? ? ? ? ? ? ? ? ? ? ? ? ? .setSingle(true/false)//false為雙選,true為單選将塑÷龆伲看自己需求

? ? ? ? ? ? ? ? ? ? ? ? ? .setOnClickBottomListener(new CommonDialog.OnClickBottomListener()){

? ??????????????????????????????@Override

????????????????????????????????????public void onPositiveClick() {

? ? ????????????????????????????????startActivity(MainActivity.class);//做你想做的事。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? finish();

????????????????????????????????????}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onNegtiveClick() {

????????????????????????????????//退出程序

? ? ????????????????????????????System.exit(0);

????????????????????????????}

? ? ? ? ? ? ? ? ? ?}).show();


????????//當(dāng)Dialog出現(xiàn)時点寥,返回鍵失效

????????commonDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

????????????@Override

? ? ????????public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {

????????????????????if(keyEvent.getKeyCode()==KeyEvent.KEYCODE_BACK){

????????????????????????????return true;

? ? ? ? ????????????}

????????????????????????????return false;

? ? ????????????}

????????});

好了艾疟,到這里,一個簡單的自定義Dialog就完成了敢辩,趕緊拿去試試吧蔽莱。

注意:具體需求,直接改動就可以戚长。代碼簡單也好懂盗冷。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市历葛,隨后出現(xiàn)的幾起案子正塌,更是在濱河造成了極大的恐慌,老刑警劉巖恤溶,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異帜羊,居然都是意外死亡咒程,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門讼育,熙熙樓的掌柜王于貴愁眉苦臉地迎上來帐姻,“玉大人,你說我怎么就攤上這事奶段〖⒋桑” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵痹籍,是天一觀的道長呢铆。 經(jīng)常有香客問我,道長蹲缠,這世上最難降的妖魔是什么棺克? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮线定,結(jié)果婚禮上娜谊,老公的妹妹穿的比我還像新娘。我一直安慰自己斤讥,他們只是感情好纱皆,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布个曙。 她就那樣靜靜地躺著邑滨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瑟捣,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音习柠,去河邊找鬼疏魏。 笑死,一個胖子當(dāng)著我的面吹牛钳踊,可吹牛的內(nèi)容都是我干的衷敌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼拓瞪,長吁一口氣:“原來是場噩夢啊……” “哼缴罗!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起祭埂,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤面氓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蛆橡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體舌界,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年泰演,在試婚紗的時候發(fā)現(xiàn)自己被綠了呻拌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡睦焕,死狀恐怖藐握,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情垃喊,我是刑警寧澤猾普,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站本谜,受9級特大地震影響初家,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜耕突,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一笤成、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧眷茁,春花似錦炕泳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浙芙。三九已至,卻和暖如春籽腕,著一層夾襖步出監(jiān)牢的瞬間嗡呼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工皇耗, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留南窗,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓郎楼,卻偏偏與公主長得像万伤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子呜袁,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容