您的位置:首页 > 移动开发 > Android开发

Android中的互动交流 之 Dialog

2014-04-26 21:42 225 查看
一、Dialog

本来按照书上的打了一遍,发现有showDialog()方法,但是有过时警告。。。才发现这个方法已经被DialogFragment给替代了。。。无语了。。。发展太快了啊。现在又4.x了,我写了个alertdialog发现取消居然在左边,Google太反人类了吧,这太不习惯了。。。

恩言归正传,这个总结主要是dialog的几种用法。

1、只有确定和取消的dialog

protected void dialog() {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setMessage("确认退出吗?").setTitle("提示").setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
}




2、3个button

protected void dialog() {
AlertDialog dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star)
.setTitle("调查").setMessage("你喜欢coding吗?")
.setPositiveButton("很喜欢",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我很喜欢coding。",Toast.LENGTH_LONG).show(); }})
.setNegativeButton("不喜欢", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我不喜欢coding。", Toast.LENGTH_LONG).show();}})
.setNeutralButton("一般", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();}}).create();
dialog.show();

}






3、可以输入文字的dialog

protected void dialog() {
new AlertDialog.Builder(this).setTitle("请输入").setIcon(
android.R.drawable.ic_dialog_info).setView(
new EditText(this)).setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
}




4、单选框

protected void dialog() {
new AlertDialog.Builder(this).setTitle("单选框").setIcon(
android.R.drawable.ic_dialog_info).setSingleChoiceItems(
new String[] { "Item1", "Item2" }, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();}
}).setNegativeButton("取消", null).show();
}




5、复选框

protected void dialog() {
new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
new String[] { "Item1", "Item2" }, null, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
}




6、自定义

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#ffffffff"
android:orientation="horizontal"
android:id="@+id/dialog" >
   <TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/name"
android:text="姓名:" />
   <EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/etname"
android:minWidth="100dip" />
</LinearLayout>


protected void dialog() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.layout,
(ViewGroup) findViewById(R.id.dialog));
new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();
}




7、日期选择

protected void dialog() {
Calendar calendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker,
int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this,
year + "年" + (month+1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show();
}
};
DatePickerDialog dlg = new DatePickerDialog(
MainActivity.this,dateListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
dlg.show();
}




8、时间选择

protected void dialog() {
Calendar calendar = Calendar.getInstance();
TimePickerDialog.OnTimeSetListener timeListener =
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this,
hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
}

};
TimePickerDialog dlg = new TimePickerDialog(
MainActivity.this,timeListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),true);
dlg.show();
}




参考文章:
http://android.tgbus.com/Android/tutorial/201107/359812.shtml http://blog.csdn.net/wuwo333/article/details/7666158
& my out-of-time textbook -。=
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: