Android之消息通知的那些事儿
作者:互联网
Toast类显示消息提示框
用法:
第一个参数:要显示在哪一个上下文当中
第二个参数:消息提示的内容
第三个参数:以显示某种持续方式(Toast.LENGTH_SHORT,Toast.LENGTH_LONG)
Toast.makeText(medicineMain.this,"药品删除成功!",Toast.LENGTH_SHORT).show();
AlertDialog类实现对话框
第一种:只带按钮和文字以及标题的对话框
需要有AlertDialog 对象,通过set方法来进行对话框的一些内容的设置
AlertDialog alertDialog=new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("乔布斯");
alertDialog.setMessage("活着就是为了改变世界");
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你单机了否定按钮",Toast.LENGTH_SHORT).show();
}
});
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你单机了是定按钮",Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
第二种:带有列表的对话框
通过setItems来显示列表,以及每一项的点击事件
//创建列表显示的内容
final String[] items=new String[]{"a","b","c"};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("你喜欢的字母");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择了"+items[which],Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
第三种:带有单选列表的对话框
通过setSingleChoiceItems
//创建列表显示的内容
final String[] items=new String[]{"a","b","c"};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("你喜欢的名言");
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择单选框"+items[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你选择确定按钮",Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
第四种:带有多选列表的对话框
需要有两个字符串数组,一个记录显示的内容,一个用于标记状态;通过setMultiChoiceItems方法来实现多选列表
checkItems=new boolean[]{false,true,false,true,false};
items=new String[]{"a","b","c","d","e"};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择字母");
builder.setMultiChoiceItems(items, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//点击之后,状态就为选中状态
checkItems[which]=isChecked;
}
});
//添加确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result="";
for(int i=0;i<checkItems.length;i++){
if(checkItems[i]){
result+=items[i]+" ";
}
}
if(!"".equals(result)){
Toast.makeText(MainActivity.this,"你选择了["+result+"]",Toast.LENGTH_SHORT).show();
}
标签:Toast,show,通知,builder,DialogInterface,AlertDialog,new,Android,事儿 来源: https://blog.51cto.com/u_15270205/2908818