第24章、OnLongClickListener長按事件(從零開始學Android)

在Android App應用中,OnLongClick事件表示長按2秒以上觸發的事件,本章我們通過長按圖像設置爲牆紙來理解其具體用法。

  知識點:OnLongClickListener
  OnLongClickListener接口與之前介紹的OnClickListener接口原理基本相同,只是該接口爲View長按事件的捕捉接口,即當長時間按下某個View時觸發的事件,該接口對應的回調方法簽名如下。
  public boolean onLongClick(View v) 
  參數v:參數v爲事件源控件,當長時間按下此控件時纔會觸發該方法。
  返回值:該方法的返回值爲一個boolean類型的變量,當返回true時,表示已經完整地處理了這個事件,並不希望其他的回調方法再次進行處理;當返回false時,表示並沒有完全處理完該事件,更希望其他方法繼續對其進行處理。

  

一、設計界面

  1、首先把a.jpg、b.jpg、c.jpg、d.jpg、e.jpg、prov.png、next.png圖片複製到res/drawable-hdpi文件夾內。

  

  2、打開“res/layout/activity_main.xml”文件,生成ImageButton按鈕。

  (1)從工具欄向activity拖出1個圖像ImageView、2個圖像按鈕ImageButton。該控件來自Image&Media。

  

  3、打開activity_main.xml文件。

  我們把自動生成的代碼修改成如下代碼,具體爲:

  (1)ImageView的id修改爲picture;

  (2)“上一幅”按鈕ImageButton的id修改爲prov;

  (3)設置android:padding="0dp",按鈕灰色邊框去掉。

  (4)“下一幅”按鈕ImageButton的id修改爲next;

  (5)設置android:padding="0dp",按鈕灰色邊框去掉。

  

  代碼如下:

[html] view plaincopy
  1. <RelativeLayout   
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/picture"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_marginTop="18dp"  
  15.         android:src="@drawable/a"  
  16.         tools:ignore="ContentDescription" />  
  17.   
  18.     <ImageButton  
  19.         android:id="@+id/prov"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentLeft="true"  
  23.         android:layout_below="@+id/picture"  
  24.         android:layout_marginLeft="38dp"  
  25.         android:layout_marginTop="16dp"  
  26.         android:padding="0dp"  
  27.         android:src="@drawable/prov" />  
  28.   
  29.     <ImageButton  
  30.         android:id="@+id/next"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:layout_alignParentRight="true"  
  34.         android:layout_alignTop="@+id/prov"  
  35.         android:layout_marginRight="24dp"  
  36.         android:padding="0dp"  
  37.         android:src="@drawable/next" />  
  38.   
  39. </RelativeLayout>  


  4、界面如下:

  

  

二、長按事件 

  打開“src/com.genwoxue.onlongclick/MainActivity.java”文件。

  然後輸入以下代碼:  

[java] view plaincopy
  1. package com.example.onlongclick;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.widget.ImageButton;  
  6. import android.widget.ImageView;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.View.OnLongClickListener;  
  10. import android.widget.Toast;  
  11. import android.graphics.Bitmap;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     //聲明Image對象與ImageBoutton對象  
  15.     private ImageView ivwPicture=null;  
  16.     private ImageButton ibtnProv=null;  
  17.     private ImageButton ibtnNext=null;  
  18.     //聲明5個圖像  
  19.     private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_main);  
  25.         //獲取ImageView對象ivwPicture  
  26.         ivwPicture=(ImageView)super.findViewById(R.id.picture);  
  27.         //獲取兩個按鈕對象ImageButton  
  28.         ibtnProv=(ImageButton)super.findViewById(R.id.prov);  
  29.         ibtnNext=(ImageButton)super.findViewById(R.id.next);  
  30.         //註冊OnClick監聽器  
  31.         ibtnProv.setOnClickListener(new ProvOnClickListener());  
  32.         ibtnNext.setOnClickListener(new NextOnClickListener());  
  33.         //註冊OnlongClick監聽器  
  34.         ivwPicture.setOnLongClickListener(new PicOnLongClick());  
  35.     }  
  36.     //單擊“上一幅”按鈕顯示前一張圖片  
  37.     private class ProvOnClickListener  implements OnClickListener{  
  38.         private int i=5;  
  39.         public void onClick(View view){           
  40.             if(i > 0){  
  41.                 ivwPicture.setImageResource(iImages[--i]);  
  42.             }  
  43.             else if(i == 0){  
  44.                 i =5;  
  45.                 ivwPicture.setImageResource(iImages[4]);  
  46.             }  
  47.         }  
  48.     }  
  49.     //單擊“下一幅”按鈕顯示後一張圖片  
  50.     private class NextOnClickListener implements OnClickListener{  
  51.         private int i=0;  
  52.         public void onClick(View view){           
  53.             if(i < 5)  
  54.                 ivwPicture.setImageResource(iImages[i++]);  
  55.             else if(i == 5){  
  56.                 i = 0;  
  57.                 ivwPicture.setImageResource(iImages[0]);  
  58.             }  
  59.         }  
  60.     }  
  61.     //長按圖片設置爲桌面牆紙  
  62.     private class PicOnLongClick implements OnLongClickListener{  
  63.         @Override  
  64.         public boolean onLongClick(View view){  
  65.             try{  
  66.                 //清空當前牆紙  
  67.                 MainActivity.this.clearWallpaper();  
  68.                 //當前view轉換爲ImageView對象  
  69.                 ImageView iv=(ImageView)view;  
  70.                 //啓用圖形緩衝  
  71.                 iv.setDrawingCacheEnabled(true);  
  72.                 //使用當前緩衝圖形創建Bitmap  
  73.                 Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());  
  74.                 //當前圖形設置爲牆紙  
  75.                 MainActivity.this.setWallpaper(bmp);  
  76.                 //清理圖形緩衝  
  77.                 iv.setDrawingCacheEnabled(false);  
  78.                 Toast.makeText(getApplicationContext(), "背景設置成功!",Toast.LENGTH_LONG).show();  
  79.             }  
  80.             catch(Exception e){  
  81.                 Toast.makeText(getApplicationContext(), "背景設置失敗!",Toast.LENGTH_LONG).show();  
  82.             }  
  83.             return true;  
  84.         }  
  85.     }  
  86. }  

  通過“上一幅”、“下一幅”按鈕瀏覽圖片,長按圖片可以把當前圖片設置爲桌面牆紙。

三、AndroidManifest.xml文件

  打開AndroidManifest.xml文件,添加:

  <uses-permission android:name="android.permission.SET_WALLPAPER"/>

  完整代碼如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.onlongclick"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  10.     <uses-permission android:name="android.permission.SET_WALLPAPER"/>  
  11.   
  12.     <application  
  13.         android:allowBackup="true"  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name"  
  16.         android:theme="@style/AppTheme" >  
  17.         <activity  
  18.             android:name="com.example.onlongclick.MainActivity"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.   
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.   
  28. </manifest>  



  

  效果如下:

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章