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

Android自定義Dialog框樣式

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

本文實(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="購(gòu)買(mǎi)數(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è)類(lèi)繼承Dialog寫(xiě)代碼,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)聽(tīng)器
 
    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)聽(tīng)器,使數(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)聽(tīng)器,使數(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)聽(tīng)器
        okButton.setOnClickListener(mClickListener);// 使用外部的
//        okButton.setOnClickListener(onClickListener);// 使用內(nèi)部自定義的
 
        this.setCancelable(true);// 設(shè)置是否點(diǎn)擊周?chē)瞻滋幙梢匀∠揇ialog,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(), "庫(kù)存:" + 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è)試如下:

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

相關(guān)文章

  • PowerManagerService之自動(dòng)滅屏流程解析

    PowerManagerService之自動(dòng)滅屏流程解析

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

    Android拍照上傳功能示例代碼

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

    Android加載View中Background詳解

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

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

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

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

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

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

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

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

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

    解決AMD無(wú)法使用Android studio問(wèn)題

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

    Flutter路由的跳轉(zhuǎn)、動(dòng)畫(huà)和傳參詳解(最簡(jiǎn)單)

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

    Android webview 遇到android.os.FileUriExposedException錯(cuò)誤解決辦法

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

最新評(píng)論

凤翔县| 南昌市| 大洼县| 四平市| 滁州市| 桐庐县| 鹰潭市| 东安县| 华阴市| 隆德县| 将乐县| 青冈县| 凤阳县| 右玉县| 南开区| 澄江县| 泽州县| 宜川县| 遂平县| 册亨县| 亳州市| 静乐县| 古浪县| 修文县| 无棣县| 上虞市| 加查县| 黎川县| 瑞丽市| 洪泽县| 贞丰县| 获嘉县| 连云港市| 固原市| 长乐市| 尼玛县| 运城市| 清流县| 咸丰县| 文安县| 海门市|