Android Studio 1-1
Dialog对话框
一 常用对话框
1、普通对话框
效果:
//构建对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置图标
builder.setIcon(R.drawable.ic_launcher_background);
//设置标题
builder.setTitle("hello world");
//设置内容
builder.setMessage("我是Android");
//设置确定按钮
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了ok", Toast.LENGTH_SHORT).show();
}
});
//设置取消按钮
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
//使用构建者 创建对话框
AlertDialog dialog = builder.create();
//切记显示
dialog.show();
2、单选对话框
//构建者
AlertDialog.Builder builderSingle = new AlertDialog.Builder(this);
//设置图标
builderSingle.setIcon(R.drawable.ic_launcher_background);
//设置标题
builderSingle.setTitle("昵称");
builderSingle.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了ok", Toast.LENGTH_SHORT).show();
}
});
builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你点击了cancel", Toast.LENGTH_SHORT).show();
}
});
//设置单选列表
final String[] items = {
"朔風", "岚月", "金鸿"};
//参数一 列表 参数二 默认选中下表 参数三 事件监听
builderSingle.setSingleChoiceItems(items, 2,