国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android自定義Dialog原理實(shí)例解析

瀏覽:34日期:2022-09-23 09:47:48

Android開發(fā)過程中,常常會(huì)遇到一些需求場(chǎng)景——在界面上彈出一個(gè)彈框,對(duì)用戶進(jìn)行提醒并讓用戶進(jìn)行某些選擇性的操作,

如退出登錄時(shí)的彈窗,讓用戶選擇“退出”還是“取消”等操作。

Android系統(tǒng)提供了Dialog類,以及Dialog的子類,常見如AlertDialog來實(shí)現(xiàn)此類功能。

一般情況下,利用Android提供的Dialog及其子類能夠滿足多數(shù)此類需求,然而,其不足之處體現(xiàn)在:

1. 基于Android提供的Dialog及其子類樣式單一,風(fēng)格上與App本身風(fēng)格可能不太協(xié)調(diào);

2. Dialog彈窗在布局和功能上有所限制,有時(shí)不一定能滿足實(shí)際的業(yè)務(wù)需求。

本文將通過在Dialog基礎(chǔ)上構(gòu)建自定義的Dialog彈窗,以最常見的確認(rèn)彈框?yàn)槔?/p>

本樣式相對(duì)比較簡(jiǎn)單:上面有一個(gè)彈框標(biāo)題(提示語),下面左右分別是“確認(rèn)”和“取消”按鈕,當(dāng)用戶點(diǎn)擊“確認(rèn)”按鈕時(shí),彈框執(zhí)行

相應(yīng)的確認(rèn)邏輯,當(dāng)點(diǎn)擊“取消”按鈕時(shí),執(zhí)行相應(yīng)的取消邏輯。

首先,自定義彈框樣式:

<?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:background='@drawable/dialog_bg' android:orientation='vertical' > <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_gravity='center' android:paddingTop='14dp' android:textColor='@color/login_hint' android:textSize='@dimen/text_size_18' /> <LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_marginBottom='14dp' android:layout_marginLeft='20dp' android:layout_marginRight='20dp' android:layout_marginTop='30dp' > <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginRight='10dp' android:layout_weight='1' android:background='@drawable/btn_confirm_selector' android:gravity='center' android:textColor='@color/white' android:textSize='@dimen/text_size_16' /> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='10dp' android:layout_weight='1' android:background='@drawable/btn_cancel_selector' android:gravity='center' android:textColor='@color/login_hint' android:textSize='@dimen/text_size_16' /> </LinearLayout></LinearLayout>

然后,通過繼承Dialog類構(gòu)建確認(rèn)彈框控件ConfirmDialog:

package com.corn.widget;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.TextView;import com.corn.R;public class ConfirmDialog extends Dialog { private Context context; private String title; private String confirmButtonText; private String cacelButtonText; private ClickListenerInterface clickListenerInterface; public interface ClickListenerInterface { public void doConfirm(); public void doCancel(); } public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) { super(context, R.style.MyDialog); this.context = context; this.title = title; this.confirmButtonText = confirmButtonText; this.cacelButtonText = cacelButtonText; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); init(); } public void init() { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.confirm_dialog, null); setContentView(view); TextView tvTitle = (TextView) view.findViewById(R.id.title); TextView tvConfirm = (TextView) view.findViewById(R.id.confirm); TextView tvCancel = (TextView) view.findViewById(R.id.cancel); tvTitle.setText(title); tvConfirm.setText(confirmButtonText); tvCancel.setText(cacelButtonText); tvConfirm.setOnClickListener(new clickListener()); tvCancel.setOnClickListener(new clickListener()); Window dialogWindow = getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); DisplayMetrics d = context.getResources().getDisplayMetrics(); // 獲取屏幕寬、高用 lp.width = (int) (d.widthPixels * 0.8); // 高度設(shè)置為屏幕的0.6 dialogWindow.setAttributes(lp); } public void setClicklistener(ClickListenerInterface clickListenerInterface) { this.clickListenerInterface = clickListenerInterface; } private class clickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub int id = v.getId(); switch (id) { case R.id.confirm:clickListenerInterface.doConfirm();break; case R.id.cancel:clickListenerInterface.doCancel();break; } } };}

在如上空間構(gòu)造代碼中,由于控件的'確認(rèn)'和'取消'邏輯與實(shí)際的應(yīng)用場(chǎng)景有關(guān),因此,控件中通過定義內(nèi)部接口來實(shí)現(xiàn)。

在需要使用此控件的地方,進(jìn)行如下形式調(diào)用:

public static void Exit(final Context context) { final ConfirmDialog confirmDialog = new ConfirmDialog(context, '確定要退出嗎?', '退出', '取消'); confirmDialog.show(); confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() { @Override public void doConfirm() {// TODO Auto-generated method stubconfirmDialog.dismiss();//toUserHome(context);AppManager.getAppManager().AppExit(context); } @Override public void doCancel() {// TODO Auto-generated method stubconfirmDialog.dismiss(); } }); }

調(diào)用中實(shí)現(xiàn)了此控件的內(nèi)部接口,并賦給控件本身,以此在點(diǎn)擊按鈕時(shí)實(shí)現(xiàn)基于外部具體業(yè)務(wù)邏輯的函數(shù)回調(diào)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 广宗县| 安吉县| 陆河县| 平和县| 天水市| 红原县| 越西县| 栾川县| 罗源县| 奎屯市| 无极县| 高要市| 济源市| 澄江县| 阜新市| 饶平县| 涿鹿县| 东台市| 全椒县| 罗田县| 高台县| 镇远县| 察雅县| 合江县| 黎平县| 登封市| 图们市| 襄城县| 辉南县| 三原县| 东山县| 湘乡市| 环江| 七台河市| 信阳市| 吉首市| 金坛市| 元江| 云阳县| 苗栗市| 石家庄市|