电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

Android自定義Dialog框樣式

瀏覽:34日期:2022-09-18 09:06:39

本文實例為大家分享了Android自定義Dialog框樣式的具體代碼,供大家參考,具體內(nèi)容如下

首先定義dialog的布局文件,buy_goods_dialog.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:background='#fff' android:orientation='vertical'> <RelativeLayoutandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_marginLeft='10dp'android:layout_marginRight='10dp'> <TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_centerVertical='true' android:layout_gravity='center' android:text='購買數(shù)量' android:textColor='#000' /> <LinearLayout android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_alignParentRight='true' android:layout_centerVertical='true'> <Buttonandroid: android:layout_width='50dp'android:layout_height='40dp'android:text='—' /> <Buttonandroid: android:layout_width='50dp'android:layout_height='40dp'android:text='1' /> <Buttonandroid: android:layout_width='50dp'android:layout_height='40dp'android:text='+' /></LinearLayout> </RelativeLayout> <Buttonandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:layout_alignBottom='@id/relativeLayout'android:text='確定' /></LinearLayout>

接著是創(chuàng)建一個類繼承Dialog寫代碼,BuyGoodsDialog.java如下:

package com.example.administrator.myapplication; import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.Toast; public class BuyGoodsDialog extends Dialog { private Activity context;// 上下文對象 private Button reduceButton;// “-”按鈕 private Button numberButton;// “1”按鈕 private Button plusButton;// “+”按鈕 private Button okButton;// “確定”按鈕 private View.OnClickListener mClickListener;// 確定按鈕的事件監(jiān)聽器 public BuyGoodsDialog(Activity context) {super(context);this.context = context; } public BuyGoodsDialog(Activity context, int theme, View.OnClickListener clickListener) {super(context, theme);this.context = context;this.mClickListener = clickListener; } public BuyGoodsDialog(Context context, int themeResId) {super(context, themeResId); } protected BuyGoodsDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {super(context, cancelable, cancelListener); } @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 指定布局this.setContentView(R.layout.buy_goods_dialog);// 獲取buy_goods_dialog布局中的控件reduceButton = (Button) findViewById(R.id.button_reduce);// 減號(-)按鈕numberButton = (Button) findViewById(R.id.button_number);// 數(shù)字(1)按鈕plusButton = (Button) findViewById(R.id.button_plus);// 加號(+)按鈕okButton = (Button) findViewById(R.id.button_buyGoodsDialog_ok);// 確定按鈕 numberButton.setText('1');// 設(shè)置數(shù)字按鈕初始值為1 // 獲取窗口對象Window dialogWindow = this.getWindow();// 窗口管理器WindowManager m = context.getWindowManager();// 獲取屏幕寬、高用Display d = m.getDefaultDisplay();// 獲取對話框當(dāng)前的參數(shù)值WindowManager.LayoutParams p = dialogWindow.getAttributes();// 這里設(shè)置的寬高優(yōu)先級高于XML中的布局設(shè)置//// 高度設(shè)置為屏幕的0.6//p.height = (int) (d.getHeight() * 0.6);//// 寬度設(shè)置為屏幕的0.8//p.width = (int) (d.getWidth() * 0.8);// 設(shè)置到屬性配置中dialogWindow.setAttributes(p); // “+”號按鈕的事件監(jiān)聽器,使數(shù)字按鈕的值加1plusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {numberButton.setText(String.valueOf(Integer.parseInt(numberButton.getText().toString()) + 1)); }});// “-”號按鈕的事件監(jiān)聽器,使數(shù)字按鈕的值減1reduceButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {int num = Integer.parseInt(numberButton.getText().toString()) - 1;if (num <= 0) { numberButton.setText('1');} else { numberButton.setText(String.valueOf(num));} }}); // 為確定按鈕綁定點擊事件監(jiān)聽器okButton.setOnClickListener(mClickListener);// 使用外部的//okButton.setOnClickListener(onClickListener);// 使用內(nèi)部自定義的 this.setCancelable(true);// 設(shè)置是否點擊周圍空白處可以取消該Dialog,true表示可以,false表示不可以 } /** * 獲取數(shù)字按鈕的數(shù)字 * * @return 返回數(shù)字 */ private String getCount() {return numberButton.getText().toString(); } public View.OnClickListener onClickListener = new View.OnClickListener() {@Overridepublic void onClick(View v) { Toast.makeText(getContext(), '庫存:' + getCount(), Toast.LENGTH_SHORT).show();} }; }

最后就是調(diào)用了

Android自定義Dialog框樣式

BuyGoodsDialog dialog=new BuyGoodsDialog(MainActivity.this, R.style.Theme_AppCompat_Dialog, new View.OnClickListener() { @Override public void onClick(View v) {Toast.makeText(MainActivity.this,'點擊了確定按鈕!',Toast.LENGTH_SHORT).show(); }});dialog.show();

運行,測試如下:

Android自定義Dialog框樣式

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 搪玻璃冷凝器_厂家-越宏化工设备| 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 有声小说,听书,听小说资源库-听世界网 | 鼓风干燥箱_真空烘箱_高温干燥箱_恒温培养箱-上海笃特科学仪器 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 无味渗透剂,泡沫抑尘剂,烷基糖苷-威海威能化工有限公司 | 网站制作优化_网站SEO推广解决方案-无锡首宸信息科技公司 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 专业深孔加工_东莞深孔钻加工_东莞深孔钻_东莞深孔加工_模具深孔钻加工厂-东莞市超耀实业有限公司 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 底部填充胶_电子封装胶_芯片封装胶_芯片底部填充胶厂家-东莞汉思新材料 | 酵素生产厂家_酵素OEM_酵素加盟_酵素ODM_酵素原料厂家_厦门益力康 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 巨野月嫂-家政公司-巨野县红墙安康母婴护理中心 | 合肥废气治理设备_安徽除尘设备_工业废气处理设备厂家-盈凯环保 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 办公室装修_上海办公室设计装修_时尚办公新主张-后街印象 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 断桥铝破碎机_铝合金破碎机_废铁金属破碎机-河南鑫世昌机械制造有限公司 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 脉冲布袋除尘器_除尘布袋-泊头市净化除尘设备生产厂家 | 湖南自考_湖南自学考试| 绿叶|绿叶投资|健康产业_绿叶投资集团有限公司 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | hdpe土工膜-防渗膜-复合土工膜-长丝土工布价格-厂家直销「恒阳新材料」-山东恒阳新材料有限公司 ETFE膜结构_PTFE膜结构_空间钢结构_膜结构_张拉膜_浙江萬豪空间结构集团有限公司 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 微水泥_硅藻泥_艺术涂料_艺术漆_艺术漆加盟-青岛泥之韵环保壁材 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 合肥角钢_合肥槽钢_安徽镀锌管厂家-昆瑟商贸有限公司 | 2025世界机器人大会_IC China_半导体展_集成电路博览会_智能制造展览网 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 陶氏道康宁消泡剂_瓦克消泡剂_蓝星_海明斯德谦_广百进口消泡剂 |