隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數據加載中……

          Android/Ophone中的懸浮對話框和即點即關對話框

          本文為原創,如需轉載,請注明作者和出處,謝謝!

          ActivityOphone系統的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配置。在這個配置中主要指定了actioncategory。按著這個配置顯示的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開發完全講義(第2版)(本書版權已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2010-01-11 08:31 銀河使者 閱讀(3276) 評論(0)  編輯  收藏 所屬分類: java 原創移動(mobile)Google

          主站蜘蛛池模板: 衡阳县| 米林县| 秦安县| 商都县| 兴文县| 苏州市| 汪清县| 临武县| 章丘市| 咸阳市| 冀州市| 陆丰市| 峨眉山市| 涿鹿县| 浑源县| 图们市| 金堂县| 泸定县| 油尖旺区| 集安市| 孟连| 治多县| 合川市| 防城港市| 永城市| 土默特左旗| 姜堰市| 伊宁市| 道孚县| 胶州市| 武功县| 扶余县| 广南县| 宁晋县| 公安县| 渭南市| 邯郸县| 宿迁市| 岳阳县| 青河县| 滦平县|