对话框消息提示机制,常用于向用户传递信息、提示或警告用户的行为。下面是一些常见的对话框例子:
一、提示对话框
关键代码如下:
//提示对话框
private void promptDialog() {
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("提示对话框")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("取消",null)
.setNeutralButton("忽略",null)
.create().show();
}
运行效果:
二、单选列表对话框
//单选列表对话框
private void singleListDialog() {
final String hobby[]={"篮球","游戏","象棋","网上冲浪"};
AlertDialog dialog=new AlertDialog.Builder(this)
//参数1:列表数组,参数2:默认选中索引
.setSingleChoiceItems(hobby, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
index =which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择的爱好是:"+hobby[index],Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
}
运行效果:
三、多选列表对话框
//多选列表对话框
private void multiSelectDialog() {
//列表
final String hobby[]={"篮球","游戏","象棋","网上冲浪"};
//是否默认选中标识
boolean checkItems[]={false,false,false,false};
str = "";
AlertDialog dialog=new AlertDialog.Builder(this)
.setIcon(R.drawable.star)
.setTitle("多选列表对话框")
.setMultiChoiceItems(hobby, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//选中的
if (isChecked){
str+=hobby[which]+" ";
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择的爱好是:"+str,Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
}
运行效果:
四、进度条对话框
进度条对话框是一种常用的对话框,通常是当用户请求网络时显示的一种对话框,默认是圆形进度条的对话框,
//进度条对话框
private void progressDialog() {
//创建水平进度条对话框
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("水平对话框");
progressDialog.setMax(100);
progressDialog.setMessage("正在下载");
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
int max=progressDialog.getMax();
int i=0;
while (i<max){
try {
Thread.sleep(100);
progressDialog.setProgress(i);
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
运行效果:
五、DatePickerDialog 日期对话框
需要注意的是DatePickerDialog对话框构造方法中的参数表示的意义,该方法中共有5个参数,分别是上下文对象,日期设置的监听器,当前的年份,当前的月份,当前的日期数,写好后展示对话框就可以了。
//日期对话框
private void datePickerDialog() {
//获取当前日期
Calendar c=Calendar.getInstance();
DatePickerDialog datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
//这里的month小1,比如选5月,会显示4月
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
str=String.valueOf(year)+"年"+String.valueOf(month+1)+"月"+String.valueOf(dayOfMonth)+"日";
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
},c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
运行效果:
六、TimePickerDialog时间设置对话框
需要注意的还是构造方法中参数的问题该方法中公有5个参数分别表示上下文对象,时间设置的监听器,当前的小时数,当前的分钟数和是否采用24进制的计数法。
//TimePickerDialog时间设置对话框
private void timePickerDialog() {
//获取当前日期
Calendar c=Calendar.getInstance();
TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
str=String.valueOf(hourOfDay)+"时"+String.valueOf(minute)+"分";
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
},c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),true);
timePickerDialog.show();
}
运行效果:
七、自定义对话框
自定义对话框可以分为以下步骤:
1、设计自定义对话框样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:background="@mipmap/dialog_bg"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/holo_red_light"
android:layout_marginTop="160dp"
android:text="真的要退出吗?"></TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:layout_width="70dp"
android:layout_height="50dp"
android:id="@+id/yes_btn"
android:background="@mipmap/yes_btn"></Button>
<Button
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginLeft="80dp"
android:id="@+id/no_btn"
android:background="@mipmap/no_btn"></Button>
</LinearLayout>
</LinearLayout>
2、设计style(去标题栏,去背景)
到styles.xml文件中添加:
<style name="mydialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
3、将第一步的布局应用到当前自定义对话框
//自定义对话框的java文件
public class MyAlert extends Dialog {
//这里的构造方法必须要用有themeResId参数的构造方法,使之可以把第2部设计的style传进去
protected MyAlert(final Context context, int themeResId) {
super(context, themeResId);
setContentView(R.layout.myalert_layout);
findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.exit(0);
}
});
findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
4、实例化对话框(参1:环境上下文参数2:第二步创建的style R.style.mydialog),并展示show()
//自定义对话框
private void customAlert() {
MyAlert alert=new MyAlert(this, R.style.mydialog);
alert.show();
}
运行效果:
八、PopupWindow
//PopupWindow
private void showPopupWindow(View view) {
//准备弹窗所需要的视图对象,此代码是将布局文件转换成View(布局文件资源,父容器)
View v= LayoutInflater.from(this).inflate(R.layout.popup_layout,null);
//1.实例化对象
//参数1:用在弹窗中的View
//参数2、3:弹窗的宽高
//参数4(focusable):能否获取焦点
final PopupWindow window=new PopupWindow(v,250,60,true);
//2.设置(背景、动画)
//设置背景
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//window.setOutsideTouchable();
//3.显示
//参数1(anchor):锚
//参数2、3:相对于锚在x、y方向上的偏移量
window.showAsDropDown(view,-250,0);
v.findViewById(R.id.select).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"选择",Toast.LENGTH_SHORT).show();
window.dismiss();
}
});
v.findViewById(R.id.copy).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"复制",Toast.LENGTH_SHORT).show();
window.dismiss();
}
});
}
九、ArrayAdapter
数组适配器,只能用来显示单一的文本。默认的构造方法规定布局资源文件第一个控件必须是textView控件,4个参数的构造可以解决这一限制,但必须指定textView控件id
//数据适配器对话框
private void showArrayDialog() {
/**
* //数组适配器
* 参数1:环境
* 参数2:布局资源索引,指的是每一项数据所呈现的伴式androicd.R.layout:xxx
* 参数3:数据源
* 参数4:int textviewlal.指定文本需要放在布局中对应id文本控制的位置
*/
AlertDialog.Builder alert=new AlertDialog.Builder(this);
final String [] items={"Java","Mysql","Android","HTML","C","JavaScript"};
ArrayAdapter adapter=new ArrayAdapter(this, R.layout.array_item_layout, R.id.text,items);
//参数1:适配器对象(对数据显示样式的规则制定器)
//参数2:监听器
alert.setTitle("请选择").setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,items[which],Toast.LENGTH_SHORT).show();
}
}).show();
}
运行效果:
有什么不对的地方麻烦指出,我也是菜鸟,让我们一起进步!