Android开发中的AlertDialog
2011-01-03 14:44 雪夜&流星 阅读(971) 评论(0) 编辑 收藏 举报最近做了一个Android项目,对AlertDialog有一些了解,贴一些心得上来。
日期选择对话框:
图片
代码:
代码
Calendar c = Calendar.getInstance();
DatePickerDialog ad = new DatePickerDialog(this,
new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
String date = String.valueOf(year) + "-"
+ String.valueOf(monthOfYear + 1) + "-"
+ String.valueOf(dayOfMonth);
}
}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DATE));
ad.show();
时间选择对话框:
图片
代码:
代码
TimePickerDialog ad = new TimePickerDialog(this,
new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
String timer =“”;
if (hourOfDay < 10) {
timer = "0" + String.valueOf(hourOfDay) + ":"
+ String.valueOf(minute);
} else if (minute < 10) {
timer = String.valueOf(hourOfDay) + ":" + "0"
+ String.valueOf(minute);
} else if (hourOfDay < 10 && minute < 10) {
timer = "0" + String.valueOf(hourOfDay) + ":" + "0"
+ String.valueOf(minute);
} else {
timer = String.valueOf(hourOfDay) + ":"
+ String.valueOf(minute);
}
}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
ad.show();
多选对话框:
图片
代码:
代码
provinces = new String[] { getResources().getString(R.string.Monday),
getResources().getString(R.string.Tuesday),
getResources().getString(R.string.Wednesday),
getResources().getString(R.string.Thursday),
getResources().getString(R.string.Friday),
getResources().getString(R.string.Saturday),
getResources().getString(R.string.Sunday) };
AlertDialog ad = new AlertDialog.Builder(this)
.setIcon(R.drawable.image).setTitle(R.string.repeattitle)
.setMultiChoiceItems(provinces, -1,
new DialogInterface.OnMultiChoiceClickListener()
{
public void onClick(DialogInterface dialog,int whichButton, boolean isChecked)
{
}
}).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int whichButton)
{
@SuppressWarnings("unused")
int count = lv.getCount();
String s = String.valueOf(R.string.chocies);
String s1="";
for (int i = 0; i < provinces.length; i++)
{
if (lv.getCheckedItemPositions().get(i))
{
s += i + ":"+ lv.getAdapter().getItem(i)+ " ";
}
}
if (lv.getCheckedItemPositions().size() > 0)
{
Toast.makeText(getApplicationContext(),getResources().getString(R.string.chocies)+s, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.nochocies), Toast.LENGTH_LONG).show();
}
}
}).setNegativeButton(R.string.cancle, null).create();
lv = ad.getListView();
ad.show();
单选对话框:
图片
代码:
代码
provinces = new String[] { getResources().getString(R.string.Monday),
getResources().getString(R.string.Tuesday),
getResources().getString(R.string.Wednesday),
getResources().getString(R.string.Thursday),
getResources().getString(R.string.Friday),
getResources().getString(R.string.Saturday),
getResources().getString(R.string.Sunday) };
AlertDialog ad = new AlertDialog.Builder(this)
.setIcon(R.drawable.image).setTitle(R.string.repeattitle)
.setSingleChoiceItems(provinces, -1, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
int count = lv.getCount();
String s = String.valueOf(R.string.chocies);
String s1 = "";
for (int i = 0; i < provinces.length; i++) {
if (lv.getCheckedItemPositions().get(i)) {
s = (String) lv.getAdapter().getItem(i);
} else {
Toast
.makeText(
getApplicationContext(),
getResources()
.getString(
R.string.updateenablewishfail),
Toast.LENGTH_LONG)
.show();
}
}
}
if (lv.getCheckedItemPositions().size() > 0) {
Toast.makeText(
getApplicationContext(),
getResources().getString(
R.string.chocies)
+ s, Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(
getApplicationContext(),
getResources().getString(
R.string.nochocies),
Toast.LENGTH_LONG).show();
}
}
}).setNegativeButton(R.string.cancle, null).create();
lv = ad.getListView();
ad.show();
待输入框的对话框:
图片
代码:
代码
EditText edt = new EditText(ActivityNoteBooks.this);
//弹出对话框的时候里面带有文字
edt.setText(text);
AlertDialog ad = new AlertDialog.Builder(this).setTitle(
R.string.wishnametitle).setView(edt).setPositiveButton(
R.string.ok, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String s = edt.getText().toString();
}
}).setNegativeButton(R.string.cancle, null).create();
ad.show();
}
感悟:对于AlertDialog一类的对话框有很多布局格式,可以根据自己的需要自己去通过XML布局,然后嵌套到AlertDialog中去。
参考:http://www.eoeandroid.com/thread-45576-1-1.html 我在eoeAndroid中的帖子