public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
}
/**
* 弹出确定取消对话框的方法
*
@param
viwe
*/
public
void
click1(View viwe) {
AlertDialog.Builder builder =
new
Builder(
this
);
builder.setTitle(
"提醒:"
);
builder.setMessage(
"你确定要关机么?"
);
builder.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
Toast. makeText(getApplicationContext(),
"5秒后关机"
, 0).show();
}
});
builder.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
});
// AlertDialog dialog = builder.create();
// dialog.show();
builder.show();
//对话框都要show出来
}
/**
* 单选对话框
*
@param
view
*/
public
void
click2(View view) {
AlertDialog.Builder builder =
new
Builder(
this
);
builder.setTitle(
"请选择性别:"
);
final
String[] items = {
"男"
,
"女"
,
"其他"
};
builder.setSingleChoiceItems(items, 1,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
Toast. makeText(getApplicationContext(),
"您的性别"
+ items[which],
0).show();
dialog.dismi ss();
//解除会话
}
});
builder.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
});
builder.show();
}
/**
* 多选对话框
*
@param
view
*/
public
void
click3(View view) {
AlertDialog.Builder builder =
new
Builder(
this
);
builder.setTitle(
"您喜欢吃什么:"
);
final
String[] items = {
"大米"
,
"小米"
,
"玉米"
,
"江米"
,
"粑粑"
};
boolean
[] checkitems = {
false
,
false
,
false
,
false
,
true
};
builder.setMultiChoiceItems(items, checkitems,
new
OnMultiChoiceClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which,
boolean
isChecked) {
if
(isChecked){
Toast. makeText(getApplicationContext(),
"您喜欢吃"
+ items[which],
0).show();
}
}
});
bui lder.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
}
});
builder.show();
}
/**
* 进度对话框
*
@param
view
*/
public
void
click4(View view){
ProgressDialog pd =
new
ProgressDialog(
this
);
pd.setTitle(
"提醒"
);
pd.setMessage(
"正在获取数据..."
);
pd.show();
}
/**
* 显示具体进度对话框
*
@param
view
*/
public
void
click5(View view){
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 = 0;i<=100;i++){
pd.setProgress(i);
try
{
Thread. sleep(30);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
}
pd.dismiss();
};
}.start();
}
}