zoukankan      html  css  js  c++  java
  • 第24章、OnLongClickListener长按事件(从零开始学Android)

    转自:http://blog.csdn.net/jianghuiquan/article/details/8348680

    在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>  



      

      效果如下:

      


  • 相关阅读:
    base.View.OpenParameter.CreateFrom打开历史单据的值是default
    创建日期时间大于启动日期时间
    下拉列表不显示空白选项的原因
    复制、下推、选单时计算类的实体服务规则不会执行
    选单返回数据以后会执行的事件方法
    判断单据体是否录入行
    供应商协同平台
    .net core获取运行时文件绝对路径
    gmock函数参数输出 备忘录
    リバース 終章
  • 原文地址:https://www.cnblogs.com/walccott/p/4957601.html
Copyright © 2011-2022 走看看