第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",按钮灰色边框去掉。
代码如下:
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <ImageView
- android:id="@+id/picture"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="18dp"
- android:src="@drawable/a"
- tools:ignore="ContentDescription" />
- <ImageButton
- android:id="@+id/prov"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_below="@+id/picture"
- android:layout_marginLeft="38dp"
- android:layout_marginTop="16dp"
- android:padding="0dp"
- android:src="@drawable/prov" />
- <ImageButton
- android:id="@+id/next"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignTop="@+id/prov"
- android:layout_marginRight="24dp"
- android:padding="0dp"
- android:src="@drawable/next" />
- </RelativeLayout>
4、界面如下:
二、长按事件
打开“src/com.genwoxue.onlongclick/MainActivity.java”文件。
然后输入以下代码:
- package com.example.onlongclick;
- import android.os.Bundle;
- import android.app.Activity;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnLongClickListener;
- import android.widget.Toast;
- import android.graphics.Bitmap;
- public class MainActivity extends Activity {
- //声明Image对象与ImageBoutton对象
- private ImageView ivwPicture=null;
- private ImageButton ibtnProv=null;
- private ImageButton ibtnNext=null;
- //声明5个图像
- private Integer[] iImages = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //获取ImageView对象ivwPicture
- ivwPicture=(ImageView)super.findViewById(R.id.picture);
- //获取两个按钮对象ImageButton
- ibtnProv=(ImageButton)super.findViewById(R.id.prov);
- ibtnNext=(ImageButton)super.findViewById(R.id.next);
- //注册OnClick监听器
- ibtnProv.setOnClickListener(new ProvOnClickListener());
- ibtnNext.setOnClickListener(new NextOnClickListener());
- //注册OnlongClick监听器
- ivwPicture.setOnLongClickListener(new PicOnLongClick());
- }
- //单击“上一幅”按钮显示前一张图片
- private class ProvOnClickListener implements OnClickListener{
- private int i=5;
- public void onClick(View view){
- if(i > 0){
- ivwPicture.setImageResource(iImages[--i]);
- }
- else if(i == 0){
- i =5;
- ivwPicture.setImageResource(iImages[4]);
- }
- }
- }
- //单击“下一幅”按钮显示后一张图片
- private class NextOnClickListener implements OnClickListener{
- private int i=0;
- public void onClick(View view){
- if(i < 5)
- ivwPicture.setImageResource(iImages[i++]);
- else if(i == 5){
- i = 0;
- ivwPicture.setImageResource(iImages[0]);
- }
- }
- }
- //长按图片设置为桌面墙纸
- private class PicOnLongClick implements OnLongClickListener{
- @Override
- public boolean onLongClick(View view){
- try{
- //清空当前墙纸
- MainActivity.this.clearWallpaper();
- //当前view转换为ImageView对象
- ImageView iv=(ImageView)view;
- //启用图形缓冲
- iv.setDrawingCacheEnabled(true);
- //使用当前缓冲图形创建Bitmap
- Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());
- //当前图形设置为墙纸
- MainActivity.this.setWallpaper(bmp);
- //清理图形缓冲
- iv.setDrawingCacheEnabled(false);
- Toast.makeText(getApplicationContext(), "背景设置成功!",Toast.LENGTH_LONG).show();
- }
- catch(Exception e){
- Toast.makeText(getApplicationContext(), "背景设置失败!",Toast.LENGTH_LONG).show();
- }
- return true;
- }
- }
- }
通过“上一幅”、“下一幅”按钮浏览图片,长按图片可以把当前图片设置为桌面墙纸。
三、AndroidManifest.xml文件
打开AndroidManifest.xml文件,添加:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
完整代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.onlongclick"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <uses-permission android:name="android.permission.SET_WALLPAPER"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.onlongclick.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
效果如下:
第24章、OnLongClickListener长按事件(从零开始学Android)的更多相关文章
- 第23章、OnFocuChangeListener焦点事件(从零开始学Android)
在Android App应用中,OnFocuChangeListener焦点事件是必不可少的,我们在上一章的基础上来学习一下如何实现. 基本知识点:OnFocuChangeListener事件 一 ...
- 第21章、OnItemSelectedListener事件(从零开始学Android)
在Android App应用中,OnItemSelectedListener事件也会经常用到,我们一起来了解一下. 基本知识点:OnItemSelectedListener事件 一.界面 1.新建pr ...
- 从零开始学android开发-View的setOnClickListener的添加方法
1)第一种,也是最长见的添加方法(一下都以Button为例) Button btn = (Button) findViewById(R.id.myButton); btn .setOnClickLis ...
- 从零开始学android -- Service
废话不多说了,Service是四大组件之一,是一个后台处理长时间运行在主线程不需要依赖ui界面显示的应用组件,切记不能在service中做耗时操作,会阻塞主线程,要做也要在service中开个子线程做 ...
- 从零开始学android开发-获取控件
mBtnNews = (Button)findViewById(R.id.btn_news);//获取控件
- 从零开始学android开发-项目打包发布
右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...
- 从零开始学android开发-adt-bundle-eclipse下的修改android app名称
eclipse中,打开项目根目录中的AndoirManifest.xml文件,找到如下内容 <application android:allowBackup="true" a ...
- 从零开始学android开发-项目重命名
--修改项目名称 选中项目-[refactor]-[rename] --修改package名称 选中需要重命名的包-[refactor]-[rename] --修改gen下面的名称 打开Android ...
- 从零开始学android开发-通过WebService进行网络编程,使用工具类轻松实现
相信大家在平常的开发中,对网络的操作用到HTTP协议比较多,通过我们使用Get或者Post的方法调用一个数据接口,然后服务器给我们返回JSON格式的数据,我们解析JSON数据然后展现给用户,相信很多人 ...
随机推荐
- (33)zabbix proxy分布式监控配置
概述 zabbix proxy可以代替zabbix server检索客户端的数据,然后把数据汇报给zabbix server,并且在一定程度上分担了zabbix server的压力.zabbix pr ...
- php-7.0.16 , apache2.4.25 配置
官网下载php,apache 修改apache E:\php\Apache24\conf\httpd.conf Define SRVROOT "E:/php/Apache24" - ...
- Hive 执行sql命令报错
Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxExce ...
- Python开发:网络编程
Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的全部方法. 高级别的网络 ...
- Java-终止应用程序
参考了:http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html 理论在上面链接中有详细的解释 package com.tj; ...
- Ubuntu Software Center has closed unexpectly解决方案
打开软件中心Ubuntu Software Center的时候 出现crash report :The application Ubuntu Software Center has closed un ...
- Python3常用模块的安装
1.mysql驱动:mysql-connector-python 1.安装 $ pip3 install mysql-connector-python --allow-external mysql-c ...
- hihoCoder #1471 拥堵的城市
这道题目是hihoCoder Challenge 27的C题,我考虑了5天:(. 计数问题.由于树的结构的特殊性(树具有递归结构),不难想到思路是树形DP.由于这是[计数问题]而非[优化问题],我们思 ...
- apache kafka参考
apache kafka参考 消息队列分类: 点对点: 消息生产者生产消息发送到queue中,然后消息消费者从queue中取出并且消费消息.这里要注意: 消息被消费以后,queue中不再有存储,所以 ...
- P3799 妖梦拼木棒 (组合数学)
题目背景 上道题中,妖梦斩了一地的木棒,现在她想要将木棒拼起来. 题目描述 有n根木棒,现在从中选4根,想要组成一个正三角形,问有几种选法? 输入输出格式 输入格式: 第一行一个整数n 第二行n个整数 ...