Android/Ophone 中的悬浮对话框和即点即关对话框
2010-09-06 17:35
369 查看
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:黑体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimHei;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:1;
mso-generic-font-family:roman;
mso-font-format:other;
mso-font-pitch:variable;
mso-font-signature:0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@黑体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:宋体;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
Activity
是
Ophone
系统的
4
个应用程序组件之一。通过传统方法显示的
Activity
都是充满整个屏幕,也就是全屏的
Activity
。事实上,
Activity
不仅可以全屏显示,还可以象对话框一样直接显示在屏幕上。而且可以通过单击屏幕的任何位置(包括
Activity
内部和
Activity
外部)来关闭
Activity
。
Activity
的 传统风格
Activity
是学习
Ophone
的入门技术。几乎所有的初学者都会从
Activity
学起。因此,
Activity
这个组件对于
Ophone
的开发人员是再熟悉不过了。下面来看一下
Activity
的基本配置。
<
activity
android:name
=".Main"
android:label
="@string/app_name">
<
intent-filter
>
<
action
android:name
="android.intent.action.MAIN"
/>
<
category
android:name
="android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
上面的配置代码是一个典型的
Activity
配置。在这个配置中主要指定了
action
和
category
。按着这个配置显示的
Activity
会充满整个屏幕。在
Ophone
中也内置了很多程序,大多数都会包含
Activity
,例如,图
1
是一个时钟程序,也是一个典型的
Activity
。
悬浮
Activity
所谓悬浮
Activity
,就是悬浮在桌面上,看起来象一个对话框。如图
2
所示。
事实上,实现上面的效果并不复杂,只需要在
AndroidManifest.xml
文件中定义
Activity
的
<activity>
标签中添加一个
android:theme
属性,并指定对话框主题即可,代码如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
manifest
xmlns:android
="http://schemas.android.com/apk/res/android"
package
="net.blogjava.mobile"
android:versionCode
="1"
android:versionName
="1.0">
<
application
android:icon
="@drawable/date"
android:label
="@string/app_name"
>
<
activity
android:name
=".Main"
android:label
="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<
intent-filter
>
<
action
android:name
="android.intent.action.MAIN"
/>
<
category
android:name
="android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
<
uses-sdk
android:minSdkVersion
="3"
/>
</
manifest
>
当使用上面的配置代码时,显示的
Activity
就会如图
2
所示。在本例中向
Activity
添加了两个按钮,分别用来显示当前日期和关闭对话框。
Activity
的布局文件的内容如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<
TextView
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="
这是一个悬浮对话框"
android:layout_marginLeft
="20dp" android:layout_marginRight
="20dp"
/>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:gravity
="center"
android:layout_marginTop
="20dp">
<
Button
android:id
="@+id/btnCurrentDate"
android:layout_width
="100dp" android:layout_height
="wrap_content"
android:text
="
当前 日期"
/>
<
Button
android:id
="@+id/btnFinish"
android:layout_width
="80dp"
android:layout_height
="wrap_content" android:text
="
关闭"
/>
</
LinearLayout
>
</
LinearLayout
>
这两个按钮的单击事件代码如下:
public
void
onClick(View view)
{
switch
(view.getId())
{
case
R.id.btnCurrentDate:
//
显示当前日期对话框
SimpleDateFormat simpleDateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd");
dateDialog.setIcon(R.drawable.date);
dateDialog.setTitle(
"
当前日期:"
+ simpleDateFormat.format(
new
Date()));
dateDialog.setButton("
确定",
new
OnClickListener()
{
@Override
public
void onClick(DialogInterface dialog,
int
which)
{
}
});
dateDialog.setOnDismissListener(new
OnDismissListener()
{
@Override
public
void onDismiss(DialogInterface dialog)
{
new
DateDialog.Builder(Main.
this
).setMessage(
"
您已经关闭的当前对话框.").create().show();
}
});
dateDialog.show();
break
;
case
R.id.btnFinish:
//
关闭悬浮Activity
finish();
break
;
}
}
单击“显示日期”按钮后,效果如图
4
所示。
触摸任何位置都可以关闭的对话框
通常需要单击“关闭”或其他类似的按钮来关闭
Activity
或对话框。但有时需要单击(触摸)屏幕的任何位置来关闭
Activity
或对话框。关闭
Activity
很好处理,只需要处理
Activity
的触摸事件即可,代码如
下:
@Override
public
boolean onTouchEvent(MotionEvent event)
{
finish();
return
true;
}
如果是对话框,也同样可以使用
onTouchEvent
事件方法。不过一般使用了
AlertDialog
对话框都是封装好的。因此,要使用
onTouchEvent
事件方法,就需要继承
AlertDialog
类。在上一节给出的
onClick
方法中弹出当前显示对话框的代码中使用了一个
DateDialog
类,该类是
AlertDialog
的子类,代码如下:
package
net.blogjava.mobile;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.view.MotionEvent;
public
class DateDialog
extends
AlertDialog
{
public
DateDialog(Context context)
{
super
(context);
}
@Override
public
boolean onTouchEvent(MotionEvent event)
{
//
关闭显示日期对话框
dismiss();
return
super.onTouchEvent(event);
}
}
在上面的代码中也使用了
onTouchEvent
事件方法。在该方法中调用了
dismiss
方法来关闭对话框。读者可以运行本文的例子,看看是否能通过单击屏幕的任何位置来关闭对话框和悬浮
Activity
。
总结
本文介绍了悬浮
Activity
和触摸任何位置都可以关闭的对话框的实现。悬浮
Activity
只需要在
<activity>
元素中添
加
android:theme="@android:style/Theme.Dialog"
即可。要想触摸任何位置关闭对话框或
Activity
,需要使用触摸事件(
onTouchEvent
方法)。如果是对话框,需要通过继承
AlertDialog
类的方式来使用
onTouchEvent
方法。
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:黑体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimHei;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;
mso-font-charset:1;
mso-generic-font-family:roman;
mso-font-format:other;
mso-font-pitch:variable;
mso-font-signature:0 0 0 0 0 0;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:-1610611985 1073750139 0 0 159 0;}
@font-face
{font-family:"/@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"/@黑体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:宋体;
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
.MsoChpDefault
{mso-style-type:export-only;
mso-default-props:yes;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
Activity
是
Ophone
系统的
4
个应用程序组件之一。通过传统方法显示的
Activity
都是充满整个屏幕,也就是全屏的
Activity
。事实上,
Activity
不仅可以全屏显示,还可以象对话框一样直接显示在屏幕上。而且可以通过单击屏幕的任何位置(包括
Activity
内部和
Activity
外部)来关闭
Activity
。
Activity
的 传统风格
Activity
是学习
Ophone
的入门技术。几乎所有的初学者都会从
Activity
学起。因此,
Activity
这个组件对于
Ophone
的开发人员是再熟悉不过了。下面来看一下
Activity
的基本配置。
<
activity
android:name
=".Main"
android:label
="@string/app_name">
<
intent-filter
>
<
action
android:name
="android.intent.action.MAIN"
/>
<
category
android:name
="android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
上面的配置代码是一个典型的
Activity
配置。在这个配置中主要指定了
action
和
category
。按着这个配置显示的
Activity
会充满整个屏幕。在
Ophone
中也内置了很多程序,大多数都会包含
Activity
,例如,图
1
是一个时钟程序,也是一个典型的
Activity
。
悬浮
Activity
所谓悬浮
Activity
,就是悬浮在桌面上,看起来象一个对话框。如图
2
所示。
事实上,实现上面的效果并不复杂,只需要在
AndroidManifest.xml
文件中定义
Activity
的
<activity>
标签中添加一个
android:theme
属性,并指定对话框主题即可,代码如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
manifest
xmlns:android
="http://schemas.android.com/apk/res/android"
package
="net.blogjava.mobile"
android:versionCode
="1"
android:versionName
="1.0">
<
application
android:icon
="@drawable/date"
android:label
="@string/app_name"
>
<
activity
android:name
=".Main"
android:label
="@string/app_name"
android:theme="@android:style/Theme.Dialog">
<
intent-filter
>
<
action
android:name
="android.intent.action.MAIN"
/>
<
category
android:name
="android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
<
uses-sdk
android:minSdkVersion
="3"
/>
</
manifest
>
当使用上面的配置代码时,显示的
Activity
就会如图
2
所示。在本例中向
Activity
添加了两个按钮,分别用来显示当前日期和关闭对话框。
Activity
的布局文件的内容如下:
<?
xml version="1.0" encoding="utf-8"
?>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<
TextView
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="
这是一个悬浮对话框"
android:layout_marginLeft
="20dp" android:layout_marginRight
="20dp"
/>
<
LinearLayout
xmlns:android
="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:gravity
="center"
android:layout_marginTop
="20dp">
<
Button
android:id
="@+id/btnCurrentDate"
android:layout_width
="100dp" android:layout_height
="wrap_content"
android:text
="
当前 日期"
/>
<
Button
android:id
="@+id/btnFinish"
android:layout_width
="80dp"
android:layout_height
="wrap_content" android:text
="
关闭"
/>
</
LinearLayout
>
</
LinearLayout
>
这两个按钮的单击事件代码如下:
public
void
onClick(View view)
{
switch
(view.getId())
{
case
R.id.btnCurrentDate:
//
显示当前日期对话框
SimpleDateFormat simpleDateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd");
dateDialog.setIcon(R.drawable.date);
dateDialog.setTitle(
"
当前日期:"
+ simpleDateFormat.format(
new
Date()));
dateDialog.setButton("
确定",
new
OnClickListener()
{
@Override
public
void onClick(DialogInterface dialog,
int
which)
{
}
});
dateDialog.setOnDismissListener(new
OnDismissListener()
{
@Override
public
void onDismiss(DialogInterface dialog)
{
new
DateDialog.Builder(Main.
this
).setMessage(
"
您已经关闭的当前对话框.").create().show();
}
});
dateDialog.show();
break
;
case
R.id.btnFinish:
//
关闭悬浮Activity
finish();
break
;
}
}
单击“显示日期”按钮后,效果如图
4
所示。
触摸任何位置都可以关闭的对话框
通常需要单击“关闭”或其他类似的按钮来关闭
Activity
或对话框。但有时需要单击(触摸)屏幕的任何位置来关闭
Activity
或对话框。关闭
Activity
很好处理,只需要处理
Activity
的触摸事件即可,代码如
下:
@Override
public
boolean onTouchEvent(MotionEvent event)
{
finish();
return
true;
}
如果是对话框,也同样可以使用
onTouchEvent
事件方法。不过一般使用了
AlertDialog
对话框都是封装好的。因此,要使用
onTouchEvent
事件方法,就需要继承
AlertDialog
类。在上一节给出的
onClick
方法中弹出当前显示对话框的代码中使用了一个
DateDialog
类,该类是
AlertDialog
的子类,代码如下:
package
net.blogjava.mobile;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.view.MotionEvent;
public
class DateDialog
extends
AlertDialog
{
public
DateDialog(Context context)
{
super
(context);
}
@Override
public
boolean onTouchEvent(MotionEvent event)
{
//
关闭显示日期对话框
dismiss();
return
super.onTouchEvent(event);
}
}
在上面的代码中也使用了
onTouchEvent
事件方法。在该方法中调用了
dismiss
方法来关闭对话框。读者可以运行本文的例子,看看是否能通过单击屏幕的任何位置来关闭对话框和悬浮
Activity
。
总结
本文介绍了悬浮
Activity
和触摸任何位置都可以关闭的对话框的实现。悬浮
Activity
只需要在
<activity>
元素中添
加
android:theme="@android:style/Theme.Dialog"
即可。要想触摸任何位置关闭对话框或
Activity
,需要使用触摸事件(
onTouchEvent
方法)。如果是对话框,需要通过继承
AlertDialog
类的方式来使用
onTouchEvent
方法。
相关文章推荐
- Android/Ophone中的悬浮对话框和即点即关对话框
- Android/Ophone中的悬浮对话框和即点即关对话框
- Android/Ophone中的悬浮对话框和即点即关对话框
- (转)Android/Ophone中的悬浮对话框和即点即关对话框
- 悬浮对话框Android代码实现
- Android中的悬浮对话框和即点即关对话框
- Android悬浮对话框(即点即关对话框)实现代码
- 2017最新详解Android中对话框之PopupWindow(悬浮框)
- Android的悬浮窗式对话框
- Android实现悬浮对话框代码
- 【Android】【Framework】悬浮窗口相关(WindowManager)
- Android 官方推荐 : DialogFragment 创建对话框
- Android实现带磁性的悬浮窗体效果
- Android软件开发之盘点所有Dialog对话框大合集(一)
- 利用半透明对话框实现新浪微博android客户端首次运行时的提示界面
- Android开发(19)---常见dialog对话框的运用
- Android学习(十九)Dialog对话框
- Android自定义AlertDialog对话框并回传Activity参数
- Android 常见对话框
- android横屏时对话框设置全屏显示