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

Android/Ophone中的悬浮对话框和即点即关对话框

2010-01-11 08:15 369 查看

[b][b][b]本文为原创,如需转载,请注明作者和出处,谢谢![/b][/b][/b]

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方法。



国内最棒的Google Android技术社区(eoeandroid),欢迎访问!

《银河系列原创教程》发布

《Java Web开发速学宝典》出版,欢迎定购
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: