Android自定義Dialog框樣式
本文實(shí)例為大家分享了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"> <RelativeLayout android:id="@+id/relativeLayout" 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"> <Button android:id="@+id/button_reduce" android:layout_width="50dp" android:layout_height="40dp" android:text="—" /> <Button android:id="@+id/button_number" android:layout_width="50dp" android:layout_height="40dp" android:text="1" /> <Button android:id="@+id/button_plus" android:layout_width="50dp" android:layout_height="40dp" android:text="+" /> </LinearLayout> </RelativeLayout> <Button android:id="@+id/button_buyGoodsDialog_ok" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/relativeLayout" android:text="確定" /> </LinearLayout>
接著是創(chuàng)建一個(gè)類繼承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;// 上下文對(duì)象 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);// 減號(hào)(-)按鈕 numberButton = (Button) findViewById(R.id.button_number);// 數(shù)字(1)按鈕 plusButton = (Button) findViewById(R.id.button_plus);// 加號(hào)(+)按鈕 okButton = (Button) findViewById(R.id.button_buyGoodsDialog_ok);// 確定按鈕 numberButton.setText("1");// 設(shè)置數(shù)字按鈕初始值為1 // 獲取窗口對(duì)象 Window dialogWindow = this.getWindow(); // 窗口管理器 WindowManager m = context.getWindowManager(); // 獲取屏幕寬、高用 Display d = m.getDefaultDisplay(); // 獲取對(duì)話框當(dāng)前的參數(shù)值 WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 這里設(shè)置的寬高優(yōu)先級(jí)高于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); // “+”號(hào)按鈕的事件監(jiān)聽器,使數(shù)字按鈕的值加1 plusButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { numberButton.setText(String.valueOf(Integer.parseInt(numberButton.getText().toString()) + 1)); } }); // “-”號(hào)按鈕的事件監(jiān)聽器,使數(shù)字按鈕的值減1 reduceButton.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)); } } }); // 為確定按鈕綁定點(diǎn)擊事件監(jiān)聽器 okButton.setOnClickListener(mClickListener);// 使用外部的 // okButton.setOnClickListener(onClickListener);// 使用內(nèi)部自定義的 this.setCancelable(true);// 設(shè)置是否點(diǎn)擊周圍空白處可以取消該Dialog,true表示可以,false表示不可以 } /** * 獲取數(shù)字按鈕的數(shù)字 * * @return 返回?cái)?shù)字 */ private String getCount() { return numberButton.getText().toString(); } public View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "庫存:" + getCount(), Toast.LENGTH_SHORT).show(); } }; }
最后就是調(diào)用了
BuyGoodsDialog dialog=new BuyGoodsDialog(MainActivity.this, R.style.Theme_AppCompat_Dialog, new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"點(diǎn)擊了確定按鈕!",Toast.LENGTH_SHORT).show(); } }); dialog.show();
運(yùn)行,測(cè)試如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義Dialog的2種常見方法
- Android自定義Dialog原理實(shí)例解析
- Android 自定義加載動(dòng)畫Dialog彈窗效果的示例代碼
- Android自定義底部彈出框ButtomDialog
- android自定義Dialog彈框和背景陰影顯示效果
- Android自定義Dialog實(shí)現(xiàn)通用圓角對(duì)話框
- Android自定義dialog 自下往上彈出的實(shí)例代碼
- Android 自定義Dialog去除title導(dǎo)航欄的解決方法
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- Android編程自定義AlertDialog樣式的方法詳解
- 解決Android中自定義DialogFragment解決寬度和高度問題
- Android?自定義?Dialog?實(shí)現(xiàn)列表?單選、多選、搜索功能
相關(guān)文章
Android編程實(shí)現(xiàn)通話錄音功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)通話錄音功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android廣播接收機(jī)制實(shí)現(xiàn)錄音功能的操作技巧,需要的朋友可以參考下2017-06-06簡(jiǎn)單了解Android性能優(yōu)化方向及相關(guān)工具
這篇文章主要介紹了簡(jiǎn)單了解Android性能優(yōu)化方向及相關(guān)工具,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Android AsyncTask用法巧用實(shí)例代碼
這篇文章主要介紹了Android AsyncTask用法巧用實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01Android?補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解
這篇文章主要為大家介紹了Android?補(bǔ)間動(dòng)畫及組合AnimationSet常用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android 應(yīng)用啟動(dòng)歡迎界面廣告的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 應(yīng)用啟動(dòng)歡迎界面廣告的相關(guān)資料,需要的朋友可以參考下2017-05-05android開發(fā)教程之用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小
用命令啟動(dòng)android模擬器并設(shè)置其內(nèi)存大小的方法,,需要的朋友可以參考下2014-02-02