event

来源:互联网 发布:冰与火之歌结局 知乎 编辑:程序博客网 时间:2024/06/06 23:16

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn_main_test1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击启动MotionEvent测试"         android:textSize="20dp"/>    <Button        android:id="@+id/btn_main_test2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="长按启动KeyEvent测试"         android:textSize="20dp"/></LinearLayout>

activity_touch.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:id="@+id/ll_touch_root">    <com.example.move.MyImageView        android:id="@+id/iv_touch_test"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/atguigu"         android:layout_margin="10dp"/></LinearLayout>

MainActivity.java

package com.example.move;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//添加点击事件findViewById(R.id.btn_main_test1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, MotionEventTestActivity.class));}});//添加长按事件findViewById(R.id.btn_main_test2).setOnLongClickListener(new View.OnLongClickListener() {@Overridepublic boolean onLongClick(View v) {startActivity(new Intent(MainActivity.this, KeyEventTestActivity.class));//true if the callback consumed the long click, false otherwise.???????????????//返回值为true的话,说明这个点击事件会被长点击独占//默认值为falsereturn false;}});}}

MyImageView.java


package com.example.move;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.ImageView;/** * 自定义ImageView *  */public class MyImageView extends ImageView {public MyImageView(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onTouchEvent(MotionEvent event) {  //handleMessage()// 得到事件类型int action = event.getAction();Log.e("TAG", "IV onTouchEvent() " + Utils.getActionName(action));if(action==MotionEvent.ACTION_DOWN) {return false;}return super.onTouchEvent(event);}}

MotionEventTestActivity.java

package com.example.move;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MotionEventTestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_touch);// 设置触摸监听findViewById(R.id.iv_touch_test).setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// 得到事件类型int action = event.getAction();Log.e("TAG","IV TouchListener onTouch() "+ Utils.getActionName(action));return false;// 只有down可以进来// return true; //down, move, up都会进来}});//设置触摸监听findViewById(R.id.ll_touch_root).setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// 得到事件类型int action = event.getAction();Log.i("TAG","LL TouchListener onTouch() "+ Utils.getActionName(action));//只有downif (action == MotionEvent.ACTION_DOWN) {return true;}//move和up不做处理return false;}});}// down事件, 最先调用,然后分发找到用户操作点位//并且每一样down,move,up事件都会调用下@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {// 得到事件类型int action = event.getAction();Log.e("TAG","Activity dispatchTouchEvent() " + Utils.getActionName(action));return super.dispatchTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// 得到事件类型int action = event.getAction();Log.e("TAG", "Activity onTouchEvent() " + Utils.getActionName(action));return super.onTouchEvent(event);}} Activity dispatchTouchEvent() DOWN IV TouchListener onTouch() DOWN IV onTouchEvent() DOWN LL TouchListener onTouch() DOWN Activity dispatchTouchEvent() MOVE LL TouchListener onTouch() MOVE Activity onTouchEvent() MOVE Activity dispatchTouchEvent() UP LL TouchListener onTouch() UP Activity onTouchEvent() UP
---------------------------

activity_key.xml

<?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" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="测试KeyEvent"         android:textSize="20dp"/></LinearLayout>

KeyEventTestActivity.java

package com.example.move;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.KeyEvent;import android.widget.Toast;/** * 实现连续点击两次back键退出应用 */public class KeyEventTestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_key);}//初始化为falseprivate boolean exit = false;private Handler handler = new Handler(new Handler.Callback() {@Overridepublic boolean handleMessage(Message msg) {if(msg.what==1) {exit = false;//让上一次的点击back失效}//???return false,这个函数默认返回falsereturn false;}});@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {//判断是否是back键if(keyCode==KeyEvent.KEYCODE_BACK) {if(exit) {//退出finish();} else {//第一次按back键,由于exit是false,所以执行else里代码Toast.makeText(KeyEventTestActivity.this, "再按一次退出应用", 0).show();//第二次按back键,改变状态标识,执行if代码块直接退出exit = true;//发送消息handler.sendEmptyMessageDelayed(1, 2000);//???return true;}}//默认就是退出return super.onKeyUp(keyCode, event);}}




0 0
原创粉丝点击