android 触摸事件
16lz
2021-12-04
继续介绍android中触摸事件,这在android游戏开发中肯定会用到的。
1.工程文件如下。TouchDemo2.布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
3.核心代码。 package org.touch.cn;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView) this.findViewById(R.id.txt);
txt.setOnTouchListener(new OnTouchListenerimp());
}
private class OnTouchListenerimp implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
MainActivity.this.txt.setText("X = "+event.getX()+",Y = "+event.getY());
System.out.println("+++++++++"); //设置文本
return false;
}
}
}
4.运行效果。
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android文件下载使用Http协议
- android 截屏
- android 的那些触摸效果
- [Android]ListView中分割线的设置
- Android学习笔记(2)——搭建Android开发平台
- android 内置默认输入法
- Android(安卓)Studio下项目编译出错could not find com.android.