注意事项
创建一个对话框的时候需要使用一个上下文,这个上下文必须是activity.
因为对话框要显示到当前的activity中,对话框是activity的一部分。
创建对话框的时候,需要告诉系统当前的对话框要显示到哪个activity中。
所以必须使用activity对象作为上下文参数,如果使用getApplicationContext就会崩溃。
Toast 土司是系统级别的显示控件,所以传入getApplicationContext作为上下文没有影响。
进度条对话框
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建 进度条对话框对象
final ProgressDialog progressDialog = new ProgressDialog(this);
//设置进度条 样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
progressDialog.setTitle("正在疯狂下载。。。");
//设置最大值
progressDialog.setMax(100);
//创建线程 更新进度
new Thread() {
@Override
public void run() {
for(int i = 0; i <=100;i++)
{
progressDialog.setProgress(i);
SystemClock.sleep(100);
}
//进度完成时 关闭对话框
progressDialog.dismiss();
}
}.start();
//打开 进度条对话框
progressDialog.show();
}
}
消息对话框
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder build = new Builder(this);
build.setTitle("标题");
build.setMessage("测试");
build.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
build.show();
}
}
单选对话框
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder build = new Builder(this);
final String[] items = new String[] {"西瓜","香蕉","苹果"};
build.setSingleChoiceItems(items, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
String selectItem = items[arg1];
Toast.makeText(getApplicationContext(), selectItem, Toast.LENGTH_SHORT).show();
}
});
build.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
build.show();
}
}
多选对话框
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder build = new Builder(this);
final String[] items = new String[] {"西瓜","香蕉","苹果","桔子"};
final boolean[] checkedItems = new boolean[] {true,true,false,false};
build.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
checkedItems[arg1] = arg2;
}
});
build.setPositiveButton("确认",new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
StringBuilder strs = new StringBuilder();
for(int i = 0 ; i < checkedItems.length;i++)
{
if(checkedItems[i])
strs.append(items[i]).append(",");
}
if(strs.length()>0)
strs.delete(strs.length()-1, strs.length());
Toast.makeText(getApplicationContext(), strs, Toast.LENGTH_SHORT).show();
arg0.dismiss();
}
});
build.show();
}
}