Android基础——物理按键,长按,触摸事件及其监听器

宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述Android基础——物理按键,长按,触摸事件及其监听器相关的知识,希望能为你提供帮助。
布局文件

< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relationLayout" tools:context=".MainActivity"> < ImageButton android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="长按按钮" /> < /RelativeLayout>

java代码:长按这块还有点问题
package com.example.myeventi; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.view.ContextMenu; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.2:把长按事件注册到菜单中,并打开菜单,在onCreate中执行 ImageButton button1 = (ImageButton)findViewById(R.id.button1); button1.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { registerForContextMenu(v); //把长按事件注册到ContextMenu中 openContextMenu(v); //打开菜单 return false; } }); //3.2:创建并实例化TouchView类的对象,并为TouchView添加触摸事件监听器, //在重写的方法中根据触摸的位置重绘TouchView final TouchView touchView = new TouchView(MainActivity.this); touchView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { touchView.bitmapX = event.getX()-80; //设定坐标; touchView.bitmapY = event.getY()-80; touchView.invalidate(); //重新绘制 return true; } }); //3.3:把TouchView更新到布局管理器中 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relationLayout); relativeLayout.addView(touchView); }/* 1:基本按钮连续按两次返回键退出应用 */ private static long lastTime = 0; //1.1:重写onKeyDown()方法拦截用户单击后退按钮事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK){ exit(); return true; } return super.onKeyDown(keyCode, event); } //1.2:创建退出方法exit(),需要判断两次按下返回键的时间差 public void exit(){ if(System.currentTimeMillis()-lastTime > 2000){//只按了一次 Toast.makeText( MainActivity.this,"再按一次退出程序",Toast.LENGTH_SHORT ).show(); lastTime = System.currentTimeMillis(); } else {//连按两次就退出 finish(); System.exit(0); } }/* * 2:长按事件监听器 * */ //2.1:在MainActivity中重写onCreateContextMenu菜单,为菜单添加选项值 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add("收藏"); menu.add("举报"); } //2.2:把长按事件注册到菜单中,并打开菜单,在onCreate中执行/* * 3.触摸事件监听器 * */ //3.1:新建TouchView类呈现触摸所在地的图像 //3.2:创建并实例化TouchView类的对象,并为TouchView添加触摸事件监听器, //在重写的方法中根据触摸的位置重绘TouchView //3.3:把TouchView更新到布局管理器中 }

【Android基础——物理按键,长按,触摸事件及其监听器】 

    推荐阅读