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中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
随机推荐
- SQL: ROW_NUMBER
- 可提高工作效率的 PL/SQL Developer 设置
1.将Window List 列表展示出来并保存当前布局 ①Tools-->Windows List (展示窗口列表) ②Window-->Save Layout (保存当前布局) 2.设 ...
- ArcGIS Engine渲染
符号化之Renderer( 渲染)体系 ArcGIS Engine9.3对GIS数据的符号化分为矢量数据渲染和栅格数据渲染两大类.接下来分别介绍FeatureRender和RasterRender. ...
- ssh 服务器之间公钥认证方式的配置
前言 项目中需要编写脚本在服务器之间上传或者下载文件,但没有相关服务器来测试脚本,于是就着手安装两台server,然后用ssh的相关命令去配置server之间公钥认证登录. 步骤 1. 在VM Box ...
- Shift的用法
Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的 ...
- VMware Workstation 11, 客户机Ubuntu14.04.1 LTS 64bit,宿主机Windows 8.1 64bit,剪贴板共享(copy and paste)失效问题
Ubuntu14.04是从12.04升级上来的,因为GUI性能的原因相继装了Xubunbu和Lubuntu的包(Lubuntu的桌面果然轻量级,但是请神容易送神难,卸载Xubuntu很麻烦,就先放下了 ...
- GHOST(幽灵)重大漏洞
cd /usr/local/srcwget https://webshare.uchicago.edu/orgs/ITServices/itsec/Downloads/GHOST.cgcc GHOST ...
- 在 Hibernate 中出现 database product name cannot be null 时怎么解决?
今 天在做一个SH项目结合的时候忽然出现了,这样的错误,我开始也不知道怎么办,便上网查,看一些高手回答都是说,检查 hibernate.cfg.xml 这个配置文件,或是一些其它的配置,于是我便看了一 ...
- Android自学笔记:Git下载源代码
Info:做J2ME几年了,现在基本没有公司用了,是时候向Android领域进军了. 自学中,难免会有疏漏,有问题请及时提出,共同学习共同进步. 2014-10-13:初版 2014-10-14:添加 ...
- 百度echart使用心得,百度图表。
百度echart算是百度针对数据展示做的一个图表插件吧,一般我们使用都不是问题,主要还是对于对动态数据的解析.我这里使用饼状图,和柱状图为例: 首先,我们需要定义一个绘图的容器:(class是我自己定 ...