Android懸浮對話框(即點(diǎn)即關(guān)對話框)實(shí)現(xiàn)代碼
Activity是Android系統(tǒng)的4個(gè)應(yīng)用程序組件之一。通過傳統(tǒng)方法顯示的Activity都是充滿整個(gè)屏幕,也就是全屏的Activity。事實(shí)上,Activity不僅可以全屏顯示,還可以象對話框一樣直接顯示在屏幕上。而且可以通過單擊屏幕的任何位置(包括Activity內(nèi)部和Activity外部)來關(guān)閉Activity。 Activity的傳統(tǒng)風(fēng)格 Activity是學(xué)習(xí)Android的入門技術(shù)。幾乎所有的初學(xué)者都會從Activity學(xué)起。因此,Activity這個(gè)組件對于Android的開發(fā)人員是再熟悉不過了。下面來看一下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>
上面的配置代碼是一個(gè)典型的Activity配置。在這個(gè)配置中主要指定了action和category。按著這個(gè)配置顯示的Activity會充滿整個(gè)屏幕。在Android中也內(nèi)置了很多程序,大多數(shù)都會包含Activity,例如,圖1是一個(gè)時(shí)鐘程序,也是一個(gè)典型的Activity。
懸浮Activity
所謂懸浮Activity,就是懸浮在桌面上,看起來象一個(gè)對話框。如圖2所示。
事實(shí)上,實(shí)現(xiàn)上面的效果并不復(fù)雜,只需要在AndroidManifest.xml文件中定義Activity的<activity>標(biāo)簽中添加一個(gè)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> <?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="這是一個(gè)懸浮對話框" 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="當(dāng)前日期" /> <Button android:id="@+id/btnFinish" android:layout_width="80dp" android:layout_height="wrap_content" android:text="關(guān)閉" /> </LinearLayout> </LinearLayout>
這兩個(gè)按鈕的單擊事件代碼如下:
public void onClick(View view) { switch (view.getId()) { case R.id.btnCurrentDate: // 顯示當(dāng)前日期對話框 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd"); dateDialog.setIcon(R.drawable.date); dateDialog.setTitle("當(dāng)前日期:" + 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( "您已經(jīng)關(guān)閉的當(dāng)前對話框.").create().show(); } }); dateDialog.show(); break; case R.id.btnFinish: // 關(guān)閉懸浮Activity finish(); break; } }
單擊“顯示日期”按鈕后,效果如圖4所示。
觸摸任何位置都可以關(guān)閉的對話框
通常需要單擊“關(guān)閉”或其他類似的按鈕來關(guān)閉Activity或?qū)υ捒?。但有時(shí)需要單擊(觸摸)屏幕的任何位置來關(guān)閉Activity或?qū)υ捒颉jP(guān)閉Activity很好處理,只需要處理Activity的觸摸事件即可,代碼如下:
@Override public boolean onTouchEvent(MotionEvent event) { finish(); return true; }
如果是對話框,也同樣可以使用onTouchEvent事件方法。不過一般使用了AlertDialog對話框都是封裝好的。因此,要使用onTouchEvent事件方法,就需要繼承AlertDialog類。在上一節(jié)給出的onClick方法中彈出當(dāng)前顯示對話框的代碼中使用了一個(gè)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) { // 關(guān)閉顯示日期對話框 dismiss(); return super.onTouchEvent(event); } }
在上面的代碼中也使用了onTouchEvent事件方法。在該方法中調(diào)用了dismiss方法來關(guān)閉對話框。讀者可以運(yùn)行本文的例子,看看是否能通過單擊屏幕的任何位置來關(guān)閉對話框和懸浮Activity。
總結(jié)
本文介紹了懸浮Activity和觸摸任何位置都可以關(guān)閉的對話框的實(shí)現(xiàn)。懸浮Activity只需要在<activity>元素中添加android:theme="@android:style/Theme.Dialog"即可。要想觸摸任何位置關(guān)閉對話框或Activity,需要使用觸摸事件(onTouchEvent方法)。如果是對話框,需要通過繼承AlertDialog類的方式來使用onTouchEvent方法。
當(dāng)使用上面的配置代碼時(shí),顯示的Activity就會如圖2所示。在本例中向Activity添加了兩個(gè)按鈕,分別用來顯示當(dāng)前日期和關(guān)閉對話框。
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android實(shí)現(xiàn)類似360,QQ管家那樣的懸浮窗
- android編程實(shí)現(xiàn)懸浮窗體的方法
- Android實(shí)現(xiàn)桌面懸浮窗、蒙板效果實(shí)例代碼
- Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析
- Android利用懸浮按鈕實(shí)現(xiàn)翻頁效果
- 圣誕節(jié),寫個(gè)程序練練手————Android 全界面懸浮按鈕實(shí)現(xiàn)
- Android實(shí)現(xiàn)仿360桌面懸浮清理內(nèi)存
- 模仿美團(tuán)點(diǎn)評的Android應(yīng)用中價(jià)格和購買欄懸浮固定的效果
- Android實(shí)現(xiàn)App中導(dǎo)航Tab欄懸浮的功能
相關(guān)文章
深入解析Android中View創(chuàng)建的全過程
這篇文章主要給大家深入的解析了關(guān)于Android中View創(chuàng)建的全過程,文中介紹的非常詳細(xì),相信對大家會有一定的參考借鑒,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-03-03Android多媒體應(yīng)用使用SoundPool播放音頻
這篇文章主要為大家詳細(xì)介紹了Android多媒體應(yīng)用使用SoundPool播放音頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 實(shí)例開發(fā)基于ArcSoft實(shí)現(xiàn)人臉識別
人臉識別,是基于人的臉部特征信息進(jìn)行身份識別的一種生物識別技術(shù)。用攝像機(jī)或攝像頭采集含有人臉的圖像或視頻流,并自動(dòng)在圖像中檢測和跟蹤人臉,進(jìn)而對檢測到的人臉進(jìn)行識別的一系列相關(guān)技術(shù),通常也叫做人像識別、面部識別2021-11-11Android程序開發(fā)之給背景圖加上移動(dòng)的手勢
這篇文章主要介紹了Android程序開發(fā)之給背景圖加上移動(dòng)的手勢 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android將圖片上傳到php服務(wù)器的實(shí)例代碼
這篇文章主要介紹了Android將圖片上傳到php服務(wù)器的實(shí)例代碼,需要的朋友可以參考下2017-07-07Android編程之SurfaceView實(shí)例詳解
這篇文章主要介紹了Android編程之SurfaceView用法,簡要分析了View和SurfaceView的區(qū)別,并結(jié)合實(shí)例形式分析了SurfaceView的具體使用步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02