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中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
随机推荐
- pip install tushare
1.sudo apt-get install libxml2-dev libxslt1-dev python-dev apt-get install libevent-dev pip install ...
- JAVA 教程推荐
JAVA 教程 学习地址:http://www.manongjc.com/mysql/mysql_tutorial.html Java 简介 Java是由Sun Microsystems公司于1995 ...
- AndroidManifest.xml的android:name是否带.的区别
android项目里面的AndroidManifest.xml,会有这样的定义 <activity android:name=".Main" ...
- IIS 7 中设置文件上传大小的方法
在IIS 6.0中设置文件上传大小的方法,就是配置如下节点: <system.web> <httpRuntime maxRequestLength="1918200&quo ...
- oracle计算是否是同一周
函数已经解决跨年问题 select to_char(date'2016-12-31','iW') from dual; select to_char(date'2017-01-01','iW') fr ...
- onethink入门笔记(二)
5.onethink页面端获得后台服务器传值的方法 1:一般后台通过assign的值前台通过{$value}显示出来; 2:如果需要在js中使用 则可以通过 在js中写 var m = "{ ...
- PHP中类自动加载的方式
最近在学习composer,发现从接触PHP到现在已经遇到了三种关于PHP中类的自动加载方式,这其中包括PHP自带的类的自动加载方式.PHP的第三方的依赖管理工具composer的加载方式以及PHP的 ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- VUE 入门基础(4)
四,计算属性 基础例子 <div id='example'> <p>0riginal message: "{{message}}"</p> &l ...
- 最简单的Github入门基础
起因是小伙伴分享给我github上的一个FQ工具,让我看实现过程.于是,就由关键字"github"搜索开始. 一言之,是个开源的SVN.和CVS.SVN类似,但是,里面有千千万万程 ...