Android——事务处理(续)(十三)

news2024/9/29 5:26:00

1. 长按事件

1.1 知识点

(1)掌握长按事件的操作形式;

(2)可以设置手机的桌面背景;

1.2 具体内容

 

范例:长按一张图片之后,此图片设置为手机桌面背景。

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LongClickActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长按图片设置桌面背景" />
    
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/head11"
        />

</LinearLayout>

 

package com.example.longclickproject;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;

public class LongClickActivity extends Activity {
    ImageView img =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_long_click);
		img = (ImageView) super.findViewById(R.id.img);
		img.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				try {
					LongClickActivity.this.clearWallpaper();
					LongClickActivity.this.setWallpaper(LongClickActivity.this.img.getResources()
							.openRawResource(R.drawable.head11));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//清除桌面背景
				return false;
			}
		});
		
	}


}

以上程序在长按之后,直接报错了,这因为设置桌面背景这是属于手机系统相关的操作,没有设置相应的权限的话,是不能进行这样的操作的,所有我们需要主配文件当中进行设置。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.longclickproject"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />
    
    <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.longclickproject.LongClickActivity"
            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>

 再次运行之后,我们发现程序可以正常地完成相应的工作了。

1.3 小结

(1)长按事件只有在触发2秒之后才会有所反应;

(2)桌面操作方法:

        清除桌面:public void clearWallpaper()

        设置桌面:public void setWallpaper(InputStream data)

2. 键盘事件

2.1 知识点

(1)掌握键盘事件的使用;

(2)可以使用键盘事件进行EMAIL验证;

2.2 具体内容

范例:进行Email验证

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".OnKeyListenerActivity" >

    <EditText 
        android:id="@+id/edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />
    
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

</LinearLayout>

 

package com.example.onkeylistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class OnKeyListenerActivity extends Activity {
    EditText edt = null;
    TextView tv =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_on_key_listener);
		edt = (EditText) super.findViewById(R.id.edt);
		tv = (TextView) super.findViewById(R.id.tv);
		edt.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				String email = edt.getText().toString();
				if(event.getAction()==KeyEvent.ACTION_UP){//键盘松开
					if(email.matches("\\w+@\\w+(.com|.cn|.org|.edu)")){//进行正则的验证
						tv.setText("输入的为正确的邮箱格式");
					}else{
						tv.setText("输入的邮箱格式有问题");
					}
					
				}
				return false;
			}
		});
	}


}

2.3 小结

(1)在输入组件中可以使用正则进行数据的输入验证;

(2)键盘事件是用户在输入数据时所触发的操作。

3. 触摸事件

3.1 知识点

(1)了解触摸事件的基本操作形式;

(2)可以使用触摸事件进行基本的绘图操作。

3.2 具体内容

OnTouchListener本身当然也是Android当中所提供的一个监听接口,对于实际的Android开发而已其实用处不多,一般适用于游戏的开发。

范例:取得触摸点的坐标。 

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".OnTocuhListenerActivity" >

    
    
    
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

</LinearLayout>

package com.example.ontouchlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class OnTocuhListenerActivity extends Activity {
    TextView tv = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_on_tocuh_listener);
		tv = (TextView) super.findViewById(R.id.tv);
		tv.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				float x = event.getX();
				float y = event.getY();
				tv.setText("X轴:"+x+",Y轴:"+y);
				return false;
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.on_tocuh_listener, menu);
		return true;
	}

}

如果想要让OnTouch事件多一些实际的效果,比如说通过触摸进行屏幕划线,就可以通过一些绘图的操作来完成,那么想要完成绘图操作,就需要自定义一个新的组件。既然是一个组件类,就必须继承View这个类。

模糊

package com.example.myView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
    private List<Point> allPoint = new ArrayList<Point>();
	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//在实例化这个组件的同时就进行触摸事件的监听
		super.setOnTouchListener(new OnTouchListenerImpl());
	}
    
	protected void onDraw(Canvas canvas){
		Paint p = new Paint();//画线依靠此类,相当于画笔类
		p.setColor(Color.RED);//设置画笔颜色
		if(MyView.this.allPoint.size()>0){//如果存在坐标点的话,就可以开始绘制图形
			Iterator<Point> it = MyView.this.allPoint.iterator();
			Point frist = null;
			Point last = null;
			while(it.hasNext()){
				if(null==frist){
					frist = it.next();
				}else{
					if(last!=null){//当一段线段接收之后,下端开始的点将会是本段中结束的点
						frist = last;
					}
					last = it.next();
					canvas.drawLine(frist.x, frist.y, last.x, last.y, p);
				}
			}
		}
	}
	class OnTouchListenerImpl implements OnTouchListener{

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			Point p = new Point((int)event.getX(),(int)event.getY());
			if(event.getAction()==MotionEvent.ACTION_DOWN){//如果是触摸下去,就开始保存坐标
				MyView.this.allPoint.add(p);
			}else if(event.getAction()==MotionEvent.ACTION_UP){//如果手指松开,进行画线
				MyView.this.allPoint.add(p);
				MyView.this.postInvalidate();//重新绘制图形
			}else if(event.getAction()==MotionEvent.ACTION_MOVE){//手指一动
				MyView.this.allPoint.add(p);
				MyView.this.postInvalidate();//重新绘制图形
			}
			
			return true;
		}
		
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <com.example.myView.MyView 
        android:id="@+id/mv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    
    

</LinearLayout>

package com.example.ontouchlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class OnTocuhListenerActivity extends Activity {
    TextView tv = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myview_layout);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.on_tocuh_listener, menu);
		return true;
	}

}

3.3 小结

(1)触摸事件是在用户接触到屏幕之后所产生的事件;

(2)如果用户要想使用触摸事件进行图形的绘制,则需要编写自定义的绘图组件。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/670965.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LocalDateTime 和 LocalDate 与 date 有什么区别;LocalDateTime 示例,LocalDate 示例

目录 1 LocalDateTime 和 LocalDate 与 date 有什么区别2 LocalDateTime 示例&#xff1a;2 LocalDate 示例&#xff1a; 1 LocalDateTime 和 LocalDate 与 date 有什么区别 LocalDateTime、LocalDate和Date是 Java中不同的类库中用于表示日期和时间的类&#xff0c; 它们在功…

「Java核心技术大会 2023」6月重磅启动,邀你共同探讨Java生态

前言 &#x1f4d5;作者简介&#xff1a;热爱跑步的恒川&#xff0c;致力于C/C、Java、Python等多编程语言&#xff0c;热爱跑步&#xff0c;喜爱音乐的一位博主。 &#x1f4d7;本文收录于恒川的日常汇报系列&#xff0c;大家有兴趣的可以看一看 &#x1f4d8;相关专栏C语言初…

《机器学习公式推导与代码实现》chapter13-LightGBM

《机器学习公式推导与代码实现》学习笔记&#xff0c;记录一下自己的学习过程&#xff0c;详细的内容请大家购买作者的书籍查阅。 LightGBM 就GBDT系列算法的性能而言&#xff0c;XGBoost已经非常高效了&#xff0c;但并非没有缺陷。LightGBM就是一种针对XGBoost缺陷的改进版…

【C++11】 列表初始化 auto 范围for 新增关键字 新增容器

文章目录 1 列表初始化2 auto && 范围for3 新增关键字3.1 decltype3.2 default3.3 delete3.4 final与override 4 总结 1 列表初始化 由c语言的规则我们知道&#xff1a;一般只有数组才会支持用{}初始化&#xff0c;但是C11新语法规定我们不仅可以用{}初始化各种对象(内…

企业微信 创建应用后如何构造网页授权 获取token 获取用户信息

第一次对接企业微信&#xff0c;上网找了各种方案。 目的是在企业微信中&#xff0c;公司发给员工一个地址&#xff0c;员工点开后打开画面&#xff0c;在这个画面上可以获取到这个员工的userid&#xff08;后续功能操作就是各自不同的功能了哈&#xff0c;就不详细说了&#…

STM32F4_触摸屏

目录 1. 触摸屏原理 2. 电阻式触摸屏检测原理 3. 电容式触摸屏检测原理 4. 硬件配置 4.1 XPT2046驱动芯片 4.2 硬件设计 5. 代码详解 5.1 main.c 5.2 AT24C02.c 5.3 AT24C02.h 5.4 C_Touch_I2C.c 5.5 C_Touch_I2C.h 5.6 Touch.c 5.7 Touch.h 5.8 FT5206.c 5.9 …

ADAudit Plus:保护企业内部IT安全的强大解决方案

随着企业数字化的推进&#xff0c;IT系统和数据安全变得比以往任何时候都更加重要。为了保护企业的机密信息和敏感数据&#xff0c;企业需要一种可靠的IT安全解决方案。在众多选项中&#xff0c;ADAudit Plus是一款备受赞誉的软件&#xff0c;为企业内部的IT安全提供了强大的支…

Python入门自学进阶-Web框架——36、Web微信类实现初步

打开页面&#xff0c;会出现一个二维码&#xff0c;要使用手机微信扫一扫&#xff0c;才能登录。它的实现原理是什么&#xff1f; 下图为打开网页版微信登录的调试工具网络信息&#xff0c;定时发送请求&#xff0c;状态待处理&#xff08;pending&#xff09; 上图的挂起是pe…

网络安全|渗透测试入门案例分析,从零基础入门到精通—登录框页面的快速渗透常用检测手段

目录 引言 1、弱口令 2、万能密码绕过 ​编辑 3、登录认证绕过 3.1.令牌刷新端的错误配置 3.2. 错误的sso配置 3.3.CMS个例的访问问题 3.4.JWT Token的错误解析 3.5.暴力修改Authentication 4、图形验证码不失效 5、短信验证码不失效 6、短信攻击 7、反射型跨站脚…

【MySQL数据库】MySQL 高级SQL 语句二

MySQL 高级 SQL 语句二 一、连接查询1.1 inner join&#xff08;内连接&#xff09;1.2 left join &#xff08;左连接&#xff09;1.3 right join(右连接) 二、CREATE VIEW ---- 视图三、 UNION - - 连集3.1 UNION3.2 UNION ALL 四、交集值&#xff08;取两个SQL语句结果的交集…

经典:商业智能BI解读,值得收藏

关注新闻的朋友们可能注意到了&#xff0c;最近这段时间关于数据要素、数字经济、数字化转型的相关行动越来越多&#xff0c;一方面是各级政府的政策规划以及大规模的发展行动&#xff0c;另一方面是则是各行各业的企业开始探寻数字经济&#xff0c;通过数字化转型进行改革&…

angular学习笔记

目录 1、认识Angular![在这里插入图片描述](https://img-blog.csdnimg.cn/17fe3ec067b64d75bf9d24a4e71403ed.png)2、路由2.1、路由懒加载2.2、路由守卫 3、模版指令4、生命周期4.1、父子生命周期 5、依赖注入6、RXJS7、自定义事件&#xff0c;自定义指令8、自定义管道9、获取d…

内网渗透(八十八)之委派攻击

委派 委派是大型网络中经常部署的应用模式,给多跳认证带来了很大的便利与此同时也带来了很大的安全隐患。利用委派,攻击者可结合其他漏洞进行组合攻击,导致攻击者可获取本地管理员权限甚至域管理员权限,还可以制作深度隐藏的后门。 委派是指将域内用户的权限委派给服务账…

【Adversarial Attack in Object Detection】物理对抗攻击和防御

目录 安全监控 **有无意义**无意义的补丁有意义的补丁 光学对抗攻击对抗灯干扰相机成像 攻击方法White-box attacksGradient-based attacks Optimization-based attacks Black-box attacksQuery-based attacksEvolution algorithm OUTLOOK 在计算机视觉中&#xff0c;根据实现…

征服Go世界,御剑江湖!学习路线与顶级资源一网打尽

引言 Go语言&#xff08;也称为Golang&#xff09;是一种开源的编程语言&#xff0c;由Google开发。它具有简洁的语法、高效的并发性能和良好的内存管理&#xff0c;因此在近年来迅速获得了广泛的关注和采用。本文将为您提供一条学习Go语言的路线图&#xff0c;并介绍一些优质…

二分查找 - 数据结构和算法教程

二分查找被定义为在排序数组中使用的一种搜索算法&#xff0c;它通过重复将搜索间隔分成两半来实现。二分查找的思想是利用数组被排序的信息&#xff0c;将时间复杂度降低到O&#xff08;log N&#xff09;。 在数据结构中应用二分查找的条件 数据结构必须排序。访问数据结构的…

mac为什么读取不了NTFS格式硬盘,Tuxera NTFS for Mac 2022 读写ntfs移动硬盘插件

使用 Mac 的巨大痛点之一&#xff1a;移动硬盘只能打开文件&#xff0c;但是无法写入新的资料。有人说格式化硬盘&#xff0c;改成苹果的 macOS扩展格式&#xff0c;但是原先硬盘的数据要转移&#xff0c;而且拿到 Windows 系统里无法被识别。 有人说格式化硬盘&#xff0c;改…

关于nginx,正向代理和反向代理是什么意思

为什么要使用nginx 很多公司会用到nginx做代理服务器&#xff0c;为什么用nginx&#xff0c;tomcat服务器不行吗&#xff1f; tomcat缺点&#xff1a;并发量小&#xff0c;用户使用的少 nginx&#xff1a;高并发&#xff0c;高性能&#xff0c;cpu、内存等资源消耗却非常低&…

在 Navicat Premium 中管理 MySQL 用户 | 第 4 部分:权限管理员工具

第 4 部分&#xff1a;权限管理员工具 在本系列中&#xff0c;我们一直在探索如何使用 Navicat 的旗舰产品 Navicat Premium 执行常见的用户管理任务。在上一篇文章中&#xff0c;我们研究了新用户对象选项卡的“服务器权限”、“权限”和“SQL预览”选项卡。 在上一篇文章中…

前程无忧guid、acw_sc__v2

文章目录 声明目标网站acw_sc__v2分析python调用测试话外拓展-风控浅析往期逆向文章推荐 声明 本文章中所有内容仅供学习交流&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff0c;若有侵权&#xff0c;请私信我立即删除&#x…