Android笔记之——事件的发生
Java:
package com.example.helloworld; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,listener2Activity.class);
startActivity(intent);
}
}); //焦点事件
button2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
System.out.println("焦点按钮被点击..........");
}
}); button1.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
System.out.println("长按.....");
Toast.makeText(MainActivity.this,"按钮被长安了....0.0",Toast.LENGTH_SHORT);
return true;
}
}); //元事件:action_down,action_move,action_up
ViewGroup viewGroup = (ViewGroup) findViewById(R.id.activity_main);
viewGroup.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int actionType = motionEvent.getAction();
if (actionType==MotionEvent.ACTION_DOWN){
System.out.println("touch 按下");
}else if (actionType==MotionEvent.ACTION_MOVE){
System.out.println("touch 移动");
button2.setX(motionEvent.getX());
button2.setY(motionEvent.getY());
}else if (actionType==MotionEvent.ACTION_UP){
System.out.println("touch 松开");
}
return true;
}
});
//焦点事件
button2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
System.out.println("焦点按钮被点击..........");
}
});
button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//motionEvent:对象记录了事件发生现场情况
int actionType = motionEvent.getAction();
if (actionType==MotionEvent.ACTION_DOWN){
System.out.println("touch 按下");
}else if (actionType==MotionEvent.ACTION_MOVE){
System.out.println("touch 移动");
button2.setX(motionEvent.getX());
button2.setY(motionEvent.getY());
}else if (actionType==MotionEvent.ACTION_UP){
System.out.println("touch 松开");
} return true;
}
}); //动态设置
button1.setOnClickListener(myListener);
button2.setOnClickListener(myListener);
button3.setOnClickListener(myListener);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("发生点击事件");
Toast.makeText(MainActivity.this,"被点击了",Toast.LENGTH_SHORT).show();
}
}); //配置方式设置
android:onClick="button2"
public void button3(View view){
Toast.makeText(MainActivity.this,"点击了按钮",Toast.LENGTH_SHORT).show();
}
// 可复用式 } private View.OnClickListener myListener= new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
Toast.makeText(MainActivity.this,"button1被点击了",Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"button2被点击了",Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(MainActivity.this,"button3被点击了",Toast.LENGTH_SHORT).show();
break;
}
}
};
} XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.helloworld.MainActivity"> <TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"> </TableLayout> <Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="button1"
/> <Button
android:text="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_marginTop="38dp"
android:id="@+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="button3"
/> <Button
android:text="Button2"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:onClick="button2"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="77dp"
android:layout_marginStart="77dp" /> </RelativeLayout>
Android笔记之——事件的发生的更多相关文章
- Android笔记之——事件的发生(2)
Java: package com.example.helloworld;import android.os.Bundle;import android.view.KeyEvent;import an ...
- Android笔记:触摸事件的分析与总结----TouchEvent处理机制
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320 ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- Android中的事件传递机制
Android源码版本:API Level 19(Android 4.4) Android事件构成 在Android中,事件主要包括点按.长按.拖拽.滑动等,点按又包括单击和双击,另外还包括单指操作和 ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- 《JavaScript高级程序设计》笔记:事件(十三)
事件流 事件冒泡 IE的事件流叫做事件冒泡,即事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的节点(文档).如下代码: <body> <div id="myDi ...
- [JS学习笔记]Javascript事件阶段:捕获、目标、冒泡
当你在浏览器上点击一个按钮时,点击的事件不仅仅发生在按钮上,同时点击的还有这个按钮的容器元素,甚至也点击了整个页面. 事件流 事件流描述了从页面接收事件的顺序,但在浏览器发展到第四代时,浏览器开发团队 ...
- jquery阻止冒泡事件行为发生
<div onclick="a()"> <p onclick="b()"></p> </div> div和p元素 ...
- android中的事件传递和处理机制
一直以来,都被android中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
随机推荐
- linux 安装beyond compare
安装好后,在终端运行bcompare 就ok
- <2016-2-2 总结>
真正的伟人直率真诚,真正的贤人虚怀若谷,真正的强者温文尔雅,有足够的幽默感,尽可能的庄重而不盛气凌人! 真正的伟人直率真诚,真正的贤人虚怀若谷,真正的强者温文尔雅,有足够的幽默感,尽可能的庄重而不盛气 ...
- nyoj 82 迷宫寻宝(二)
http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...
- C语言读写文件
对文件的读和写是最常用的文件操作.在C语言中提供了多种文件读写的函数: 字符读写函数 :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式 ...
- OpenGL学习笔记1——第一个程序
学习的参考书基本是按照GL编程指南,如果有消息机制概念,对于GLUT的理解是很自然的.下面就按照自己写的第一个程序详细解释一下GL,还是比较容易上手的. 程序实现的功能是,根据当前随即种子摇出来的结果 ...
- IOS 中openGL使用教程1(openGL ES 入门篇 | 搭建openGL环境)
OpenGL版本 iOS系统默认支持OpenGl ES1.0.ES2.0以及ES3.0 3个版本,三者之间并不是简单的版本升级,设计理念甚至完全不同,在开发OpenGL项目前,需要根据业务需求选择合适 ...
- 不安装Oracle客户端情况下使用PL/SQL 远程连接数据库
附送PL/SQL Developer11中文版下载地址 1.先到Oracle网站下载Instant Client : http://www.oracle.com/technetwork/databas ...
- 谈谈Java面向对象的三大特性
Java面向对象的三大特性就是指封装.继承.多态了. 一.封装: 概念:封装是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. (举例:笔记本电脑就是一个封装体,Java语言中最小的封装体就是函数 ...
- C语言实现GPT头和分区表的读取(gcc)
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h&g ...
- ubuntu访问supermicro ikvm
https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04 安装 ...