android添加触摸事件
16lz
2021-01-23
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应用程序键盘事件机制
- Java事件模型与Android事件模型的比较
- Android菜鸟的成长笔记(11)——Android中的事件处理
- Android事件处理
- Android之各种事件触发方案
- 「Android 进阶」Android 事件分发机制
- checkbox 与listview 点击事件冲突
- AndroidAndroid程序提示和消息button响应事件
- android监听软键盘退格(删除)事件