确定取消对话框(带图标)
//(上下文,主题)
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
.setTitle("标题").setMessage("内容").setIcon(R.drawable.ic_launcher)
//响应点击事件
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "确定", 0).show();
}
}).setNegativeButton("取消", null).show();//别忘了show出来
效果
简单单选对话框
final String[] strs=new String[]{"男","女","不告诉你"};
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT)
.setTitle("标题").setIcon(R.drawable.ic_launcher)
//(String数组,默认选择项,响应事件)
.setSingleChoiceItems(strs, 2, new OnClickListener() {
//swich:所选项的数组id
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, strs[which], 0).show();
}
})
.show();
效果
多选对话框
final String[] strs = new String[] { "香蕉", "苹果", "梨子" };
AlertDialog.Builder dialog = new AlertDialog.Builder(this,
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).setTitle("标题")
.setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(strs, new boolean[]{true,true,false}, new OnMultiChoiceClickListener() {
//which:所选项的数组id
//isChecked:所选项的选中状态
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
Log.d("test", "which="+which+":isChecked="+isChecked);
}
});
dialog.create().show();
效果
进度对话框
ProgressDialog pd=new ProgressDialog(this);
pd.setTitle("标题");
pd.setMessage("请捎到.....");
pd.show();
进度条对话框
final ProgressDialog pd = new ProgressDialog(this);
// 只有两种主题
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置最大进度值
pd.setMax(100);
pd.setTitle("标题");
pd.setMessage("请捎到.....");
pd.show();
new Thread() {
public void run() {
for (int i = 1; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pd.setProgress(i);
}
// 在任何线程都可以关闭,
pd.dismiss();
};
}.start();
效果
自定义对话框
final EditText et_text = new EditText(this);
new AlertDialog.Builder(this).setTitle("请输入")
//放入一个TextView
.setIcon(android.R.drawable.ic_dialog_info).setView(et_text)
.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
et_text.getText().toString(), 0).show();
}
}).setNegativeButton("取消", null).show();
效果
自定义布局对话框
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="הҥԻ
- 上一篇解决百度分享链接不存在或失效的问题
- 下一篇linux基础之常用命令(1)