最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義Dialog框樣式

 更新時間:2021年06月17日 10:55:51   作者:二木成林  
這篇文章主要為大家詳細(xì)介紹了Android自定義Dialog框樣式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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)建一個類繼承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ù)字按鈕的值加1
        plusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberButton.setText(String.valueOf(Integer.parseInt(numberButton.getText().toString()) + 1));
            }
        });
        // “-”號按鈕的事件監(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));
                }
            }
        });
 
        // 為確定按鈕綁定點擊事件監(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() {
        @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,"點擊了確定按鈕!",Toast.LENGTH_SHORT).show();
            }
});
dialog.show();

運行,測試如下:

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

相關(guān)文章

  • PowerManagerService之自動滅屏流程解析

    PowerManagerService之自動滅屏流程解析

    這篇文章主要為大家介紹了PowerManagerService之自動滅屏流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Android拍照上傳功能示例代碼

    Android拍照上傳功能示例代碼

    這篇文章主要介紹了Android拍照上傳功能用法,結(jié)合實例形式詳細(xì)分析了Android拍照上傳功能所涉及的相關(guān)知識點與功能實現(xiàn)技巧,需要的朋友可以參考下
    2016-08-08
  • Android加載View中Background詳解

    Android加載View中Background詳解

    本文講解的是Android什么時候進(jìn)行View中Background的加載,十分的詳盡,十分全面細(xì)致,附上所有代碼,這里推薦給大家,希望大家能夠喜歡。
    2015-03-03
  • Android?Camera實現(xiàn)旋轉(zhuǎn)角度

    Android?Camera實現(xiàn)旋轉(zhuǎn)角度

    這篇文章主要為大家詳細(xì)介紹了Android?Camera實現(xiàn)旋轉(zhuǎn)角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • Android仿新浪微博、QQ空間等帖子顯示(1)

    Android仿新浪微博、QQ空間等帖子顯示(1)

    這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博、QQ空間等帖子顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android 掃碼槍不使用輸入框獲取掃描值的操作方法

    Android 掃碼槍不使用輸入框獲取掃描值的操作方法

    這篇文章主要介紹了Android 掃碼槍不使用輸入框獲取掃描值的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Android高仿抖音照片電影功能的實現(xiàn)代碼

    Android高仿抖音照片電影功能的實現(xiàn)代碼

    這篇文章主要介紹了Android高仿抖音照片電影功能的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • 解決AMD無法使用Android studio問題

    解決AMD無法使用Android studio問題

    這篇文章主要介紹了AMD無法使用Android studio解決方法,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Flutter路由的跳轉(zhuǎn)、動畫和傳參詳解(最簡單)

    Flutter路由的跳轉(zhuǎn)、動畫和傳參詳解(最簡單)

    這篇文章主要給大家介紹了關(guān)于Flutter路由的跳轉(zhuǎn)、動畫和傳參的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Android webview 遇到android.os.FileUriExposedException錯誤解決辦法

    Android webview 遇到android.os.FileUriExposedException錯誤解決辦法

    這篇文章主要介紹了Android webview 遇到android.os.FileUriExposedException錯誤解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題解決,需要的朋友可以參考下
    2017-10-10

最新評論

龙岩市| 斗六市| 清流县| 宝兴县| 油尖旺区| 当阳市| 铁力市| 康马县| 广饶县| 法库县| 鄂州市| 绥棱县| 集贤县| 楚雄市| 通山县| 顺昌县| 新余市| 德安县| 阿合奇县| 安平县| 东安县| 宝丰县| 邹平县| 溧水县| 吴川市| 两当县| 莱西市| 金寨县| 大渡口区| 广平县| 绥棱县| 利辛县| 博白县| 大渡口区| 宜川县| 无棣县| 泌阳县| 汕头市| 华宁县| 扎鲁特旗| 什邡市|