自古一楼先上图
package com.example.mydialog;import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;public class OKCancelTipsDialog extends Dialog {private Context context;private String titleValue, contentValue, okValue, cancelValue;private Button okBtn, cancelBtn;private TextView titleTv, contentTv;private DialogClickListenerInterface clickListenerInterface;public interface DialogClickListenerInterface {public void doOk();public void doCancel();}public OKCancelTipsDialog(Context context, String titleValue,String contentValue, String okValue, String cancelValue) {super(context);this.context = context;this.titleValue = titleValue;this.contentValue = contentValue;this.okValue = okValue;this.cancelValue = cancelValue;// this.confirmButtonText = confirmButtonText;// this.cacelButtonText = cacelButtonText;}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setCancelable(false);// 设置点击屏幕Dialog不消失init();}public void init() {LayoutInflater inflater = LayoutInflater.from(context);View view = inflater.inflate(R.layout.dialog_view_ok_cancel_tips, null);setContentView(view);okBtn = (Button) view.findViewById(R.id.ok_btn);cancelBtn = (Button) view.findViewById(R.id.cancel_btn);titleTv = (TextView) view.findViewById(R.id.title_tv);contentTv = (TextView) view.findViewById(R.id.content_tv);titleTv.setText(titleValue);contentTv.setText(contentValue);okBtn.setText(okValue);cancelBtn.setText(cancelValue);okBtn.setOnClickListener(new clickListener());cancelBtn.setOnClickListener(new clickListener());Window dialogWindow = getWindow();WindowManager.LayoutParams lp = dialogWindow.getAttributes();DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6dialogWindow.setAttributes(lp);}public void setClicklistener(DialogClickListenerInterface clickListenerInterface) {this.clickListenerInterface = clickListenerInterface;}private class clickListener implements View.OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint id = v.getId();switch (id) {case R.id.ok_btn:clickListenerInterface.doOk();break;case R.id.cancel_btn:clickListenerInterface.doCancel();break;}}};}
MainActivity中调用
package com.example.mydialog;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//-----------------------------------OKCancelTipsDialog logInOtherDivice = new OKCancelTipsDialog(MainActivity.this, "下线通知","你的账号已经在其它设备登陆,如非本人操作,请及时修改密码", "确定","Hello");logInOtherDivice.setClicklistener(new OKCancelTipsDialog.DialogClickListenerInterface() {@Overridepublic void doOk() {Log.d("OK","66666");}@Overridepublic void doCancel() {Log.d("OdoCancel","66666");}});logInOtherDivice.show();}}
自定义的对话框推荐设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="320dp"android:layout_height="wrap_content"android:background="@android:color/transparent"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_bg"android:layout_width="match_parent"android:layout_height="140dp"android:background="@drawable/ic_dialog_default" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_white_radius_6"android:orientation="vertical"><TextViewandroid:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:textColor="@android:color/black"android:textSize="15sp"tools:text="发现新版v2.0.1可以下载啦!" /><TextViewandroid:id="@+id/tv_size"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="16dp"android:layout_marginTop="8dp"android:layout_marginRight="16dp"android:textColor="#757575"android:textSize="14sp"android:visibility="gone"tools:text="新版本大小:5M" /><ScrollViewandroid:layout_width="match_parent"android:layout_height="90dp"android:layout_marginLeft="16dp"android:layout_marginTop="8dp"android:layout_marginRight="16dp"android:overScrollMode="never"><TextViewandroid:id="@+id/tv_description"android:layout_width="wrap_content"android:layout_height="wrap_content"android:lineSpacingMultiplier="1.1"android:textColor="#757575"android:textSize="14sp"tools:text="" /></ScrollView><!-- <com.azhon.appupdate.dialog.NumberProgressBar--><!-- android:id="@+id/np_bar"--><!-- android:layout_width="match_parent"--><!-- android:layout_height="wrap_content"--><!-- android:layout_marginLeft="16dp"--><!-- android:layout_marginTop="8dp"--><!-- android:layout_marginRight="16dp" />--><Buttonandroid:id="@+id/btn_update"android:layout_width="match_parent"android:layout_height="40dp"android:layout_marginLeft="16dp"android:layout_marginTop="8dp"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"android:background="@drawable/bg_button"android:text="@string/update"android:textAllCaps="false"android:textColor="@android:color/white" /></LinearLayout><Viewandroid:id="@+id/line"android:layout_width="2dp"android:layout_height="30dp"android:layout_gravity="center"android:background="@android:color/white" /><ImageButtonandroid:id="@+id/ib_close"android:layout_width="30dp"android:layout_height="30dp"android:layout_gravity="center"android:background="@drawable/ic_dialog_close" />
</LinearLayout>
样式
<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style><!--对话框的样式--><style name="DialogActivity" parent="Theme.AppCompat.Dialog"><!--对话框背景 --><item name="android:windowBackground">@android:color/transparent</item><!--边框 --><item name="android:windowFrame">@null</item><!--没有标题 --><item name="android:windowNoTitle">false</item><!-- 是否浮现在Activity之上 --><item name="android:windowIsFloating">true</item><!--背景透明 --><item name="android:windowIsTranslucent">true</item><!-- 是否有覆盖 --><item name="android:windowContentOverlay">@null</item><!--背景变暗--><item name="android:backgroundDimEnabled">false</item></style><!--对话框的样式--><style name="UpdateDialog" parent="DialogActivity"><!--背景变暗--><item name="android:backgroundDimEnabled">true</item></style><!-- 进度条样式--><declare-styleable name="NumberProgressBar"><attr name="progress_unreached_color" format="color" /><attr name="progress_reached_color" format="color" /><attr name="progress_text_size" format="dimension" /><attr name="progress_text_color" format="color" /></declare-styleable></resources>