长按事件OnLongClickListener
1.MainActivity.java
package com.example.administrator.hello4; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
//声明
private TextView textView;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化:通过组件id获取组件实例
textView = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.iv); imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
imageView.setBackgroundResource(R.drawable.a);
textView.setText("图片已经设置为手机桌面");
return false;
}
}); }
}
2.activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.administrator.hello4.MainActivity"> <TextView
android:id="@+id/tv"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长按图片将其设置为背景" />
<ImageView
android:id="@+id/iv"
android:src="@drawable/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3

在长按事件中的onLongClick中返回false:表示事件没有完全处理完毕。这样就可以继续执行setOnclicklistener事件 弹出“显示信息。。。”
在长按事件中的onLongClick中返回ture:表示事件完全处理完毕。不执行setOnclicklistener事件 所以不弹出“显示信息。。。”
3.按照前面的例子只能在软件上面显示已经更改设置为壁纸,但是在手机壁纸还没有更改,因为没有管理者权限
(1)MianActivity.java此时增加了设置壁纸的方法setWallPaper.
package com.example.administrator.hello4; import android.app.WallpaperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView; import java.io.IOException; public class MainActivity extends AppCompatActivity {
//声明
private TextView textView;
private ImageView imageView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化:通过组件id获取组件实例
textView = (TextView) findViewById(R.id.tv);
imageView = (ImageView) findViewById(R.id.iv); imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
imageView.setBackgroundResource(R.drawable.a);
textView.setText("图片已经设置为手机桌面");
return false;
}
}); setWallPaper(); }
public void setWallPaper(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setResource(R.drawable.a);
} catch (IOException e) {
e.printStackTrace();
}
} }
(2)在AndroidManiifest.xml配置管理者权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.hello4"> <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission> //配置的管理者权限
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
这样就可以运行软件之后真正的将图片设置为手机壁纸
长按事件OnLongClickListener的更多相关文章
- 第24章、OnLongClickListener长按事件(从零开始学Android)
在Android App应用中,OnLongClick事件表示长按2秒以上触发的事件,本章我们通过长按图像设置为墙纸来理解其具体用法. 知识点:OnLongClickListener OnLongCl ...
- ListView item 中TextView 如何获取长按事件
昨天晚上小伙伴突然来信, ListView item中嵌套的TextView 无法获取长按事件 从前从来没有仔细留意过, coding后发现...果然没什么动静 而且没有合适的API让我调用获取Tex ...
- Android Button四种点击事件和长按事件
项目XML代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- RecyclerView的单击和长按事件(转)
转自:http://www.jianshu.com/p/f2e0463e5aef 前言 上一篇文章揭开RecyclerView的神秘面纱(一):RecyclerView的基本使用中,主要讲述了Recy ...
- Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录
一.问题描述 记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!! 在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...
- Android学习笔记长按事件的处理
常见的长按事件 代码示例: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns ...
- Android系统中自定义按键的短按、双击、长按事件
在项目中碰到这样的问题: 由于系统中的按键在底层做了重新定义或者新增了按键,此时需要在APP层对按键事件(keyevent)做分解处理,模拟Android系统做法,把keyevent分解成: 1.单击 ...
- 长按事件jquery mobile
chat_enlarge.addEventListener('touchend', function(event) { if(fingers == 1){ event.preventDefault() ...
- Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类
Android RecyclerView单击.长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类 我写的附录文章2,介绍了 ...
随机推荐
- 《vue.js实战》练习---标签页组件
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- bzoj4602 [Sdoi2016]齿轮
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4602 [题解] 对于每组齿轮(u, v)连边,权值为y/x(反向边x/y) 那么直接dfs计 ...
- python module: csv
转自:sislcb 读 syntax : reader(csvfile[, dialect='excel'][, fmtparam]) csvfile:需要是支持迭代(Iterator)的对象,并且每 ...
- swift出师作,史丹佛大学游戏制作案例,计算器,小游戏
这两个案例得好好弄清楚,感觉在任何方面既然能够作为公开课被提到这所名校的课程里面自然有不得不学习的理由,感觉应该去入手一下,毕竟这种课,价格不匪,难以接触,能看到就当再教育了.
- MYSQL学习心得 优化
这一篇主要介绍MYSQL的优化,优化MYSQL数据库是DBA和开发人员的必备技能 MYSQL优化一方面是找出系统瓶颈,提高MYSQL数据库整体性能:另一方面需要合理的结构设计和参数调整,以提高 用户操 ...
- C++格式输出控制
#include<iostream> #include<string> #include<vector> #include<set> #include& ...
- javascript的有效校验
//年月日期有效性检验 function yearAndMonthCheck() { var flag = true; var currentyear = new Date().getFullYear ...
- solr in action 第三章
document: 每个document由一个或者多个域(field)组成,每个域都有自己的类型:string, text, etc. 理论上域的类型有无限多个,因为一个域的类型可以由零个或多个分析阶 ...
- 厦门大学XMUNET+ ubuntu连接方法
编辑连接 输入用户名和密码就好了 ubuntu16.04
- springBoot 发布war包
1.packaging 改为war <packaging>war</packaging> 2.剔除内置tomcat <dependency> <groupId ...