Android自定义Dialog

这段时间在做一个项目,需要使用到自定义Dialog,先在网上找了一下资料,发现还是有很多没有讲清楚的,在此给出一个Demo,一来可以方便广大码农,二来也可以方便自己,以备不时之需。。。

一个app更新对话框

java代码:

package com.xiebao.util.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.xiebao.R;
import com.xiebao.bean.VersionBean;

public class UpdateDialog extends Dialog {



    @Override
    public void setOnDismissListener(OnDismissListener listener) {
        // TODO Auto-generated method stub
        super.setOnDismissListener(listener);
        
    }

    private View updateButton;
    private View cancelButton;
    private TextView mVersionNoText;
    private TextView mUpdateMessageText;
    private VersionBean mVersion;
    private Context context;

    public UpdateDialog(Context context, VersionBean mVersion) {
        this(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.mVersion = mVersion;
        this.context=context;
        showDialog();
    }

    private void showDialog() {
        if(!isShowing()){
            show();
        }
        
    }

    public UpdateDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public UpdateDialog(Context context, int theme) {
        super(context, theme);
        // TODO Auto-generated constructor stub
    }
   
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_version_view);
        initView();
        initListener();

    }

    private void initListener() {
        mVersionNoText.setText("版本 : " +mVersion.getNo());
        mUpdateMessageText.setText(mVersion.getDescription());
        updateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //要处理的业务
                link();
                canDialog();
                

            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                canDialog();

            }
        });

    }
    /**
     * 跳转浏览器
     */
    private void link() {
        // TODO Auto-generated method stub
        String url = mVersion.getUrl(); // web address
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
        }
    protected void canDialog() {
    if(isShowing()){
        dismiss();
    }
        
    }

    private void initView() {
        updateButton = findViewById(R.id.update_dialog_ok);
        cancelButton = findViewById(R.id.update_dialog_cancel);
        mVersionNoText = (TextView) findViewById(R.id.update_version);
        mUpdateMessageText = (TextView) findViewById(R.id.update_info);
    }

}

布局文件
R.layout.update_version_view

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="#60000000">
  6 
  7     <LinearLayout
  8         android:layout_width="match_parent"
  9         android:layout_height="wrap_content"
 10         android:layout_centerInParent="true"
 11         android:layout_marginLeft="32dp"
 12         android:layout_marginRight="32dp"
 13         android:background="@drawable/bdg_pop_tomato"
 14         android:orientation="vertical">
 15 
 16         <LinearLayout
 17             android:layout_width="match_parent"
 18             android:layout_height="48dp"
 19             android:orientation="vertical">
 20 
 21             <TextView
 22                 android:id="@+id/dialog_title"
 23                 android:layout_width="wrap_content"
 24                 android:layout_height="wrap_content"
 25                 android:layout_marginLeft="16dp"
 26                 android:layout_marginTop="8dp"
 27                 android:text="@string/updateinfor"
 28                 android:textColor="#9D968D"
 29                 android:textSize="14sp" />
 30 
 31             <TextView
 32                 android:id="@+id/update_version"
 33                 android:layout_width="wrap_content"
 34                 android:layout_height="wrap_content"
 35                 android:layout_marginBottom="4dp"
 36                 android:layout_marginLeft="16dp"
 37                 android:textColor="#9D968D"
 38                 android:textSize="12sp" />
 39         </LinearLayout>
 40 
 41         <View
 42             android:layout_width="match_parent"
 43             android:layout_height="1.0dp"
 44             android:background="@color/view_color" />
 45 
 46         <RelativeLayout
 47             android:layout_width="match_parent"
 48             android:layout_height="wrap_content"
 49             android:minHeight="48dp">
 50 
 51             <TextView
 52                 android:id="@+id/update_info"
 53                 android:layout_width="wrap_content"
 54                 android:layout_height="wrap_content"
 55                 android:layout_marginLeft="16dp"
 56                 android:layout_marginRight="16dp"
 57                 android:layout_marginTop="4dp"
 58                 android:textColor="#9D968D"
 59                 android:textSize="14sp" />
 60         </RelativeLayout>
 61 
 62         <View
 63             android:id="@+id/heng_view"
 64             android:layout_width="match_parent"
 65             android:layout_height="1.0dp"
 66             android:background="@color/view_color" />
 67 
 68         <LinearLayout
 69             android:layout_width="match_parent"
 70             android:layout_height="50dp"
 71             android:orientation="horizontal">
 72 
 73             <TextView
 74                 android:id="@+id/update_dialog_ok"
 75                 android:layout_width="match_parent"
 76                 android:layout_height="match_parent"
 77                 android:layout_gravity="center_vertical"
 78                 android:layout_weight="1.0"
 79                 android:gravity="center"
 80                 android:text="@string/update"
 81                 android:textColor="#9D968D"
 82                 android:textSize="16sp" />
 83 
 84             <View
 85                 android:id="@+id/shu_view"
 86                 android:layout_width="1dp"
 87                 android:layout_height="match_parent"
 88                 android:layout_gravity="center_horizontal"
 89                 android:background="@color/view_color" />
 90 
 91             <TextView
 92                 android:id="@+id/update_dialog_cancel"
 93                 android:layout_width="match_parent"
 94                 android:layout_height="match_parent"
 95                 android:layout_gravity="center_vertical"
 96                 android:layout_weight="1.0"
 97                 android:gravity="center"
 98                 android:text="@string/cancel"
 99                 android:textColor="#9D968D"
100                 android:textSize="16sp" />
101         </LinearLayout>
102     </LinearLayout>
103 </RelativeLayout>

效果图:

 效果图:

原文地址:https://www.cnblogs.com/xiaobijia/p/3844347.html