刚刚接触到对话框内容,通过一些文档的查阅,做了以下笔记,以备忘!
1、普通对话框
public void showNormalDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(图标位置); //设置图标
builder.setTitle("我是一个普通的对话框"); //设置标题
builder.setMessage("请点击确定按钮"); //设置提醒的消息
builder.setPositiveButton("确定", new DialogInterface.OnclickListener(){ //设置下方确定取消按钮
public void onClick(DialogInterface dialogInterface,int i){
Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnclickListener(){
public void onClick(DialogInterface dialogInterface,int i){
Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
}
});
builder.show(); //最后一定要show出来,否则对话框不显示}
2、列表对话框
public void showDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
final String[ ] colors={"黑色","白色","黄色"}; //列表项
builder.setTitle("我是一个列表对话框"); //标题
builder.setItems(colors,new DialogInterface.OnCliskListener(){ //监听
public void onClick(DialogInterface dialogInterface,int i){
Toast.makeText(MainActivity.this,colors[i],Tosat.LENGTH_SHORT).show();
}
});
builder.show();
}