android添加触摸事件
16lz
2021-12-04
android触摸事件
package com.example.touch_1;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity implements OnTouchListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); ////1111RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.myLayout);///222myLayout.setOnTouchListener(this);/////3333这三句话的顺序很重要}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "发生触摸", Toast.LENGTH_SHORT).show();return false;}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/myLayout"////这里要加id android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
更多相关文章
- android 电容屏(一):电容屏基本原理篇
- 【Android每周专题】触摸屏手势
- android 电容屏(三):驱动调试之驱动程序分析篇
- android 电容屏(一):电容屏基本原理篇
- android GestureDetector应用
- android跑马灯有时候不跑
- Android(安卓)Gesture Detector
- Android(安卓)(layout_toLeftOf,layout_toRightOf,layout_above,
- 箭头图标不显示