Dialog的继承关系
对话框
布局文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通Dialog"
android:onClick="click"/>
Java代码
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher) //设置对话框的图标
.setTitle("普通对话框") //设置对话框的标题
.setMessage("正在拼命加载中") //设置对话框的信息
//添加消极按钮
.setNegativeButton("消极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了消极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加积极按钮
.setPositiveButton("积极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了积极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加中性按钮
.setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了中性按钮", Toast.LENGTH_SHORT).show();
}
})
.create() //创建对话框
.show(); //显示对话框
}
}
普通对话框
布局文件
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度Dialog"
android:onClick="click"/>
Java代码
import android.app.ProgressDialog;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
//设置对话框的图标
progressDialog.setIcon(R.mipmap.ic_launcher);
//设置对话框的标题
progressDialog.setTitle("进度对话框");
//设置对话框的信息
progressDialog.setMessage("正在拼命加载中");
//设置对话框的样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置最大进度
progressDialog.setMax(100);
//设置当前进度
progressDialog.setProgress(0);
//显示对话框
progressDialog.show();
/***
* millisInFuture 总时间
* countDownInterval 间隔时间
*/
new CountDownTimer(5100,1000){
//每次执行的方法
@Override
public void onTick(long millisUntilFinished) {
//增加进度
progressDialog.incrementProgressBy(20);
}
//执行完毕后的方法
@Override
public void onFinish() {
//关闭对话框
progressDialog.dismiss();
}
}.start();
//ProgressDialog同样也可以设置三个按钮
}
}
布局文件同上
Java代码
import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
//获得日历类对象
Calendar calendar = Calendar.getInstance();
/***
* contexrt 上下文
* listener 监听器
* year 年份
* month 月份
* dayOfMonth 天数
*/
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year + "年" + (month + 1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
布局文件同上
Java代码
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
Calendar calendar = Calendar.getInstance();
/***
* contexrt 上下文
* listener 监听器
* hourOfDay 小时
* minute 分钟
* is24HourView 是否是24小时制
*/
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay + "点" + minute + "分", Toast.LENGTH_SHORT).show();
}
}, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), true).show();
}
}
列表对话框
布局文件同上
Java代码
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
//数据源
final String[] datas = {"情网", "风继续吹", "饿狼传说", "当年情", "李香兰", "春夏秋冬", "吻别", "爱慕"};
new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher) //设置对话框的图标
.setTitle("普通对话框") //设置对话框的标题
/***
* adapter 适配器
* listener 监听器
*/
.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, datas), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了" + datas[which], Toast.LENGTH_SHORT).show();
}
}) //设置对话框的信息
//添加消极按钮
.setNegativeButton("消极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了消极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加积极按钮
.setPositiveButton("积极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了积极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加中性按钮
.setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了中性按钮", Toast.LENGTH_SHORT).show();
}
})
.create() //创建对话框
.show(); //显示对话框
}
}
布局文件同上
Java代码
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
final String[] datas = {"情网", "风继续吹", "饿狼传说", "当年情", "李香兰", "春夏秋冬", "吻别", "爱慕"};
new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher) //设置对话框的图标
.setTitle("单选列表对话框") //设置对话框的标题
/***
* items 选项
* checkedItem 默认选项
* listener 监听器
*/
.setSingleChoiceItems(datas, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择了" + datas[which], Toast.LENGTH_SHORT).show();
}
})
//添加消极按钮
.setNegativeButton("消极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了消极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加积极按钮
.setPositiveButton("积极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了积极按钮", Toast.LENGTH_SHORT).show();
}
})
//添加中性按钮
.setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了中性按钮", Toast.LENGTH_SHORT).show();
}
})
.create() //创建对话框
.show(); //显示对话框
}
}
多选列表对话框
布局文件同上
Java代码
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
final String[] datas = {"情网", "风继续吹", "饿狼传说", "当年情", "李香兰", "春夏秋冬", "吻别", "爱慕"};
boolean[] flag = {false, false, false, false, false, false, false, false};
new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher) //设置对话框的图标
.setTitle("多选列表对话框") //设置对话框的标题
/***
* items 选项
* checkedItems 选项是否被选中
* listener 监听器
*/
.setMultiChoiceItems(datas, flag, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "选择了" + datas[which], Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "取消了" + datas[which], Toast.LENGTH_SHORT).show();
}
}
})
//添加取消按钮
.setNegativeButton("消极按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
}
})
//添加确定按钮
.setPositiveButton("确定按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
}
})
//添加中性按钮
.setNeutralButton("中性按钮", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "点击了中性按钮", Toast.LENGTH_SHORT).show();
}
})
.create() //创建对话框
.show(); //显示对话框
}
}
自定义对话框
新建自定义Dialog的布局文件
<EditText
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:gravity="center"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:gravity="center"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
ialog类继承Dialog并重写方法
package com.example.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyDialog extends Dialog {
private Context context;
public MyDialog(Context context) {
super(context);
this.context = context;
}
private EditText userName, password;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_dialog_layout);
userName = findViewById(R.id.userName);
password = findViewById(R.id.password);
login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "用户名:" + userName.getText().toString() + ",密码:" + password.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
创建并显示
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
MyDialog myDialog = new MyDialog(this);
//获得窗口对象
Window window = myDialog.getWindow();
//设置透明背景色
window.setBackgroundDrawableResource(android.R.color.transparent);
//获得窗口参数对象
WindowManager.LayoutParams attributes = window.getAttributes();
//设置高
attributes.height = 100;
//设置宽
attributes.width = 100;
//将窗口参数设置给窗口l
window.setAttributes(attributes);
//显示对话框
myDialog.show();
}
}
自定义日期对话框
新建自定义MyDateDialog布局文件
</DatePicker>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/data_picker"
android:layout_alignRight="@id/data_picker"
android:orientation="horizontal">
<Button
android:id="@+id/queding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_weight="1"
android:text="确定"/>
<Button
android:id="@+id/quxiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:layout_weight="1"
android:text="取消"/>
</LinearLayout>
新建MyDateDialog类继承Dialog并重写方法
package com.example.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class MyDateDialog extends Dialog {
private Context context;
public MyDateDialog(Context context) {
super(context);
this.context = context;
}
private DatePicker data_picker;
private Button confirm, cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_date_layout);
data_picker = findViewById(R.id.data_picker);
confirm = findViewById(R.id.confirm);
cancel = findViewById(R.id.cancel);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, data_picker.getYear() + "年" + (data_picker.getMonth() + 1) + "月" + data_picker.getDayOfMonth() + "日", Toast.LENGTH_SHORT).show();
MyDateDialog.this.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "取消", Toast.LENGTH_SHORT).show();
MyDateDialog.this.dismiss();
}
});
}
}
创建自定义日期对话框并显示
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
new MyDateDialog(this).show();
}
}