- 新建xml布局, 此布局为自定义对话框的显示样式
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/title_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#f00"/>
<TextView
android:id="@+id/message_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="好好学习, 天天向上"
android:layout_margin="20dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp">
<Button
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:text="取消"
/>
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="确认"
android:layout_weight="1"
android:background="@null"/>
</LinearLayout>
</LinearLayout>
- 自定义类继承Dialog 重写Dialog的方法并添加需要的功能
package com.example.twoday01;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
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.TextView;
public class DiyDialog extends Dialog {
private Button yes; //确认按钮
private Button no; //取消按钮
private TextView titleTv; //消息标题文本
private TextView message; //消息提示文本
private String titleStr; //从外界设置的title文本
private String messageStr; //从外界设置的消息文本
//确定文本和取消文本的显示的内容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener; //取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener; //确认按钮被点击了的监听器
public DiyDialog(Context context, int themeResId) {
super(context, themeResId);
//设置窗口长和宽
WindowManager windowManager = getWindow().getWindowManager();
// display: 显示
Display display = windowManager.getDefaultDisplay();
// attribute: 属性
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = display.getWidth() * 4 / 5; //给宽赋值
getWindow().setAttributes(lp); //设置窗口的属性
}
public DiyDialog(Context context) {
super(context);
//去掉默认的标题栏 feature: 特色
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
/**
* 设置取消按钮的显示内容和监听
*
* @param str
* @param onNoOnclickListener
*/
public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noOnclickListener = onNoOnclickListener;
}
/**
* 设置确定按钮的显示内容和监听
*
* @param str
* @param yesOnclickListener
*/
public void setYesOnclickListener(String str, onYesOnclickListener yesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesOnclickListener = yesOnclickListener;
}
protected DiyDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_custom);
// 空白处不能取消动画
setCanceledOnTouchOutside(false);
// 初始化界面控件
initView();
// 初始化界面数据
initData();
// 初始化界面控件的事件
initEvent();
}
/**
* 初始化界面控件的事件
*/
private void initEvent() {
// 设置确定按钮被点击后, 向外界提供监听
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener != null) {
yesOnclickListener.onYesOnclick(); //实现接口
}
}
});
// 设置取消按钮被点击后, 向外界提供监听
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener != null) {
noOnclickListener.onNoClick(); //实现接口
}
}
});
}
/**
* 从外界 Activity 为Dialog设置标题 及信息
*
* @param titleStr
*/
public void setTitle(String titleStr) {
this.titleStr = titleStr;
}
public void setMessage(String messageStr) {
this.messageStr = messageStr;
}
/**
* 初始化界面数据
*/
private void initData() {
//用户自定义了 title 和message
if (titleStr != null) {
titleTv.setText(titleStr);
}
if (messageStr != null) {
message.setText(messageStr);
}
//设置按钮文字
if (yesStr != null) {
yes.setText(yesStr);
}
if (noStr != null) {
no.setText(noStr);
}
}
/**
* 初始化界面
*/
private void initView() {
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
titleTv = findViewById(R.id.title_id);
message = findViewById(R.id.message_id);
}
/**
* 两个接口
*/
public interface onNoOnclickListener {
public void onNoClick();
}
public interface onYesOnclickListener {
public void onYesOnclick();
}
}
- 实现自定义对话框的效果
/**
* 自定义对话框
*/
private void custom_dialog() {
DiyDialog diyDialog = new DiyDialog(this);
diyDialog.setCanceledOnTouchOutside(true);
diyDialog.show();
}
英语不好生词笔记:
对话框:
positive 英 [ˈpɒzətɪv]: 积极的, 正的 builder.setPositiveButton()
negative 英 ['negətɪv]: 消极的, 负的 builder.setNegativeButton()
custom 英 ['kʌstəm]: 风俗,习惯; 自定义
attributes 美 ['ætrə,bjʊt]: 属性 getWindow().getAttributes()
display 英 [dɪ’spleɪ]: 显示, 炫耀 windowManager.getDefaultDisplay()
feature 英 ['fiːtʃə]: 特色, 特征 requestWindowFeature(Window.FEATURE_NO_TITLE)
touch 英 [tʌtʃ]: 接触, 触动; 触摸 setCanceledOnTouchOutside(false);
outside 英 [aʊt’saɪd; 'aʊtsaɪd]: 外观, 外侧
canceled 英 ['kæns(ə)l]: 取消
练习源于: 第1天Dialog对话框