欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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="購買數(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論