对话框之AlertDialog、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择)、ProgressDialog(进度)、PopupWindow
2018-01-05 21:00
751 查看
AlertDialog:(对话框是基于Activity的)
1.概述:
1)对话框分为两种,一种是简单的对话框,一种悬浮窗。 4.0之后,确定按钮在右边,而在2.3之前,是在左边,要注意。
2)AlertDialog生成的对话框可分为4个区域:
图标区
标题区
内容区
按钮区
2.使用方法:
步骤: 1)建立builder,并set其图片、标题、内容、以及各种按钮
(注:1>按钮的传入参数除了其名称,还有一个点击事件,而dialog中按钮的点击事件是利用new DialogInterface.OnClickListener()来完成的。2>由于对话框式基于activity的,所以传入的context应是当前界面.this)
由于set方法后得到的仍是builder,所以可以连续set。
2)builder的create()成为对话框。
3)对话框dialog显示。
builder的界面:
指定对话框内容的几种方法:
setMessage(); //设置简单内容
setItems(); //设置对话框内容为简单列表项。
setSingleChoiceItems(); //设置内容为单选列表项
setMultiChoiceItems(); //设置内容为多选列表项
setAdapter(); //设置内容为自定义列表项
setView(); //设置内容自定义View.
对话框风格第窗口
只要在AndroidManifest.xml中的activity中加上一句:
android:theme=”@android:style/Theme.Dialog”
对对话框进行属性的设置
setCancelable():对话框可否取消,参数为true时:鼠标点击别处,对话框会自动消失;参数为false时:对话框不能消失 ,默认为true
范例1:点击出现简单对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);//1.创建builder builder.setIcon(R.drawable.liyifeng).setTitle("这是一个对话框").setMessage("这是其中的内容").setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "取消", Toast.LENGTH_LONG).show(); } }).setNeutralButton("中性按钮", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "中性按钮", Toast.LENGTH_LONG).show(); } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "确定", Toast.LENGTH_SHORT).show(); } }); Dialog dialog = builder.create(); dialog.show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
结果演示:
延伸:也可以在create()之后,由dialog来set,区别在于set按钮时,此方法是setButton1/2/3……
范例2:点击出现带列表的对话框
思路:builder中利用setItems(),其中第一个参数是传入的数据,第二个参数是添加的点击事件,监听第几个列表子项被点击。
注:setItems(),中的点击事件仍旧是用DialogInterface.OnClickListener()。
AlertDialog.Builder builderList = new AlertDialog.Builder(MainActivity.this); builderList.setTitle("请选择:"); builderList.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int which) { Toast.makeText(getApplicationContext(), "点击了第"+which+"个", Toast.LENGTH_SHORT).show(); } }); builderList.create().show();1
2
3
4
5
6
7
8
9
10
11
结果演示:
范例3:带单选框的对话框
思路:1.builder中利用setSingleChoiceItems(数据,默认选中的项,点击事件仍用DialogInterface.OnClickListener());2.其中重写的onClick()方法中参数int型which是选中的位置。
AlertDialog.Builder builderRadio = new AlertDialog.Builder(MainActivity.this); builderRadio.setTitle("单选框"); builderRadio.setSingleChoiceItems(itemsRadio, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int which) { //选中单选框的子项时 str=itemsRadio[which]; Toast.makeText(getApplicationContext(), "选中了"+itemsRadio[which], Toast.LENGTH_SHORT).show(); } }); builderRadio.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) {//确定时,将点击得到的字符重写到按钮处。 mButnDialogRadio.setText("你选中了"+str); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); builderRadio.create().show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
结果演示:
范例4:带多选框的对话框
思路:利用setMultiChoiceItems()方法,参数1:数据;参数2:存放选中子项的true和false,参数3:点击事件仍为DialogInterface.OnMultiChoiceClickListener()
注:
1>需要一个数据来存放被选中的子项的true和false,来管理,可以确定那个被选中了
AlertDialog.Builder builderCheck = new AlertDialog.Builder(MainActivity.this); builderCheck.setMultiChoiceItems(items,mIsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface arg0, int which, boolean isChecked) { mIsChecked[which]=isChecked; Log.d("message", "第"+which+"个是"+isChecked); } }); builderCheck.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) {//确定时,将所有选中的选项添加到一个buffer中 mstringBuffer = new StringBuffer();//每点击时建立新对象 for(int i=0;i<mIsChecked.length;i++){//循环数据判断,当为true即选中时,将数据加到buffer中 if(mIsChecked[i]){ mstringBuffer.append(items[i]); } } //将buffer中所有的数据显示到按钮上 mButtonDialogChecked.setText(mstringBuffer); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); builderCheck.show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
结果演示:
范例5:自定义dialog
思路:
1)由于builder是已经是创好的view。区域已经规定好,这里不用builder来自定义。直接新建dialog
2)利用dialog的setContentView(view);利用flater
小知识:对话框消失:对象.dismiss();
xml:自定义的布局:
<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:id="@+id/textView_titile" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/liminhao"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btn_cacel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消"/> <Button android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定"/> </LinearLayout> </LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
java:
dialog_ziding = new Dialog(MainActivity.this); LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.mydialog, null); TextView titile = (TextView) view.findViewById(R.id.textView_titile); Button cacel = (Button) view.findViewById(R.id.btn_cacel); Button ok = (Button) view.findViewById(R.id.btn_ok); titile.setText("这是一个自定义的对话框"); cacel.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { dialog_ziding.dismiss(); } }); ok.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(getApplicationContext(), "已经确定", Toast.LENGTH_SHORT).show(); dialog_ziding.dismiss(); } }); dialog_ziding.setContentView(view); dialog_ziding.show(); break;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
结果演示:
注:标题栏还是利用dialog中的settile()。
范例5的延伸:1.)对按钮和对话框背景样式进行自定义
方法:在drawable中建立xml文件,利用shape更改其圆角、实体、边框属性:(注:大小的值可以都存放到resource中的dimens中,方便以后更改)
radius=”” //设置圆角大小
solid=“” //实体的设置,可以设置其颜色@coloar
stroke=“” //设置边框 ,可以设置其颜色和宽度
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dp"/> <solid android:color="@color/green"/> <stroke android:color="@color/white" android:width="3dp"/> </shape>1
2
3
4
5
6
2.)对话框自定义背景时,需要去掉原来默认的背景,方法:
1>在style中设置:这是设置的name=”NoDialogTitle”
2>在activity中新建对话框时,需要用另一个构造函数,传入R.style.名称。
dialog_ziding = new Dialog(MainActivity.this,R.style.NoDialogTitle);
<style parent="@android:Theme.Dialog" name="NoDialogTitle"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>1
2
3
4
5
6
7
8
9
10
11
12
13
经过一系列更改,结果演示:
附:style:
<1>概述:
style是专门用于给app,dialog,textview等写界面风格的,常加到xml设置的最后一行。
<2>style格式:
<style name="TextViewStyle"> <item name="android:background">#00ff00</item> <item name="android:padding">5dp</item> </style>1
2
3
4
用时:
style="@style/TextViewStyle"1
DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择对话框)
1.概述:
DatePickerDialog显示年月日;TimePickerDialog显示分钟秒。
2用法:
步骤: 1)通过new关键字创建实例
2)为其绑定监听器
3)show()方法显示。
范例:
功能:点击按钮,弹出日历。选择时间,确定时出现提示时间。 思路:这里用Calendar来获取的时间,否则,在传入时月是从0开始的。且这里用到转型。
mCalendar = Calendar.getInstance(); DatePickerDialog pickerDialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker arg0, int year, int month, int day) { mCalendar.set(year, month, day);//将点击获得的年月日获取到calendar中。 SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");//转型 Toast.makeText(getApplicationContext(), format.format(mCalendar.getTime()), Toast.LENGTH_LONG).show(); } }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)); pickerDialog.show();1
2
3
4
5
6
7
8
9
10
11
12
13
TimePickerDialog范例:和DatePickerDialog一样。
mCalendar = Calendar.getInstance(); TimePickerDialog pickerDialog = new TimePickerDialog pickerDialog = new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker arg0, int hour, int minite) { mCalendar.set(Calendar.HOUR_OF_DAY, hour);//设置时间的另一种方式 mCalendar.set(Calendar.MINUTE, minite); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH:mm");//转型 Toast.makeText(getApplicationContext(), format.format(mCalendar.getTime()), Toast.LENGTH_LONG).show(); } }, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), true); pickerDialog.show();1
2
3
4
5
6
7
8
9
10
11
12
13
结果:
小结:这里用到了
calendar的创建方式:getInstance();
设置时间set(年,月,日)或者set(哪个量field,要变成的值value);
造型SimpleDateFormat。使用.format(时间).
ProgressDialog(进度对话框)
PopupWindow:
1.概述:
可以创建类似对话框风格的窗口。可以实现将创建的对话框窗口下拉显示,或者在指定位置显示,所以非常适合显示一些需要浮动的内容。 与Dialog的区别:
PopupWindow必须要有个View,用setContentView()来添加,而Dialog是有默认的界面的。
2.用法:
步骤: 1>调用PopupWindow构造器创建PopupWindow对象。
2>必须有个布局文件,作为弹出窗口的布局
3>必须设置popup窗口的宽度和高度:setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
4>利用setContentView()添加view
5)调用showAsDropDown(View v)将窗口做诶v组件的下拉组件显示;
或者调用showAtLocation()方法将窗口在指定位置显示。
其他方法:
setOutsideTouchable(true);//设置使点击外部可以消失
3.范例:
popup布局:<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:id="@+id/textView_pop1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp" android:text="文本1"/> <TextView android:id="@+id/textView_pop2" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp" android:text="文本2"/> <TextView android:id="@+id/textView_pop3" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp" android:text="文本3"/> </LinearLayout>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
java:
popup = new PopupWindow(SecondActivity.this); View view = getLayoutInflater().inflate(R.layout.popup, null); popup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置popup的宽度(必须设置) popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popup.setContentView(view); popup.setOutsideTouchable(true);//设置使点击外部可以消失 popup.showAsDropDown(v);1
2
3
4
5
6
7
延伸:监听手机Back按键:
//重写方法,当Back键按下时处理事件。
//重写方法,使点击Back键时能使pop消失。 @Override public boolean onKeyDown(int keyCode, KeyEvent event) {//按键按下时运行此方法 if(keyCode==KeyEvent.KEYCODE_BACK){//当返回键按下时 if(popup!=null&&popup.isShowing()){//再判断popup窗口如果不为空,而且正在运行,那么关闭掉。注意一定用短路与&&, popup.dismiss(); return true; //必须返回true,否则不响应。 } } return super.onKeyDown(keyCode, event); }
相关文章推荐
- 对话框之AlertDialog、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择)、ProgressDialog(进度)、PopupWindow
- 第八章—进度对话框ProgressDialog、日期选择器DatePickerDialog、时间选择器TimePickerDialog
- android中的ProgressDialog(进度),DatePickerDialog(日期选择)与TimePickerDialog(时间选择)
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- 使用DatePickerDialog和TimePickerDialog实现日期和时间选择对话框
- 30-datePickerDialog和TimePickerDialog对话框设置日期和时间
- 时间选择器和日期选择器 对话框 TimePickerDialog DatePickerDialog
- Android之日期和时间对话框DatePickerDialog和TimePickerDialog
- 【Android学习笔记】DatePickerDialog和TimePickerDialog日期选择器和时间选择器
- DatePickerDialog和TimePickerDialog时间选择对话框
- [控件]DatePicker日期,TimePicker时间,Calendar日历,DatePickerDialog弹出的日期,TimePickerDialog弹出的时间
- 使用DatePickerDialog和TimePickerDialog设置日期和时间
- android 实现点击输入框弹出日期选择对话框(DatePickerDialog)
- 安卓原生自带的日期选择器DatePickerDialog、时间选择器TimePickerDialog使用方法
- Android原生日期选择器对话框DatePickerDialog
- 各种杂项组件(3)之--CalendarView(日历视图)、DatePicker/TimePicker(日期、时间选择器)、NumberPicker(数值选择器)
- 在android中用TimePicker和DatePicker或者TimePickerDialog和DatePickerDialog来动态调整时间和日期
- android-UI组件实例大全(十五)------其他对话框:ProgressDialog,Date/TimePickerDialog
- Android UI系列--对话框(一)(AlertDialog,TimePickerDialog,DatePickerDialog,ProgressDialog)
- 时间对话框。。。datePickerDialog&timePickerDialog