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中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
随机推荐
- selenium2 Webdriver + Java 自动化测试实战和完全教程
selenium2 Webdriver + Java 自动化测试实战和完全教程一.快速开始 博客分类: Selenium-webdriverselenium webdriver 学习selenium ...
- mac 10.9 安装 gevent
安装步骤: Gevent依赖libevent和greenlet,需要分别安装. 1,安装 macport (如已安装,可以跳过) 2,通过终端 键入: sudo port install libeve ...
- AE开发示例之RunGPAsync
using System; using System.Collections.Generic;using System.ComponentModel;using System.Data;using S ...
- gcc for windows(mingw)编译多个c文件
myString.c myString.h main.c 其中,myString.c与myString.h对应,myString.h文件中是一些函数的声明,而myString.c文件中是.h文件中声明 ...
- AngularJS-系统代码的配置和翻译
前言:在Web开发中常常会遇到这样的情况,有些页面的下拉选项是固定不变的几个,比如:性别,一般有男.女.保密等.对于这样的情形我们一般在数据库中存储的是数字或者其对应的代码,如果是可维护的需要系统给出 ...
- form中的GET与POST
form标签是强大的:如果没有form标签,Internet将变成一个枯燥文档的只读存储库.Web Forms没有完全利用form标签的强大功能(也可以说是Web Forms为实现自己的目标才管理和 ...
- 1 《JavaScript高级程序设计》学习笔记(1)
欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 首先,我将从<JavaScript高级程序设计>这本JavaScript学习者必看的经典教 ...
- CSV表格读取
读取CSV表格需要CSV表格的编码格式为UTF-8 ,这个脚本中有些是为了方便使用封装的dll 不过都是一些简单的实现,自己实现也很容易,可做参考. /// <summary> /// 构 ...
- 快速卸载VS2015的办法
1.以管理员身份运行CMD: 2.在CMD里输入F:\>vs_enterprise.exe /uninstall /force
- XML文件中CDATA的作用
操作XML文件时,如果允许用户输入内容,例如∶"< ".">"."/".""等,当生成XML时,会破坏了XM ...