继续介绍android中触摸事件,这在android游戏开发中肯定会用到的。

1.工程文件如下。TouchDemo
2.布局文件。
<?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.运行效果。


更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android文件下载使用Http协议
  4. android 截屏
  5. android 的那些触摸效果
  6. [Android]ListView中分割线的设置
  7. Android学习笔记(2)——搭建Android开发平台
  8. android 内置默认输入法
  9. Android(安卓)Studio下项目编译出错could not find com.android.

随机推荐

  1. Android实现app内部更改系统时间(需要手机
  2. 【Android】图片(文件)上传的请求分析结构
  3. [置顶] Android学习路线:如何成长为高级工
  4. ANDROID音频系统散记之二 Android上的res
  5. Android中的多进程开发以及多进程的使用
  6. Android核心分析28篇,强烈推荐android初学
  7. Android 中图片压缩分析(上)
  8. Android Market 等于鸡肋,想做Android开
  9. Android(安卓)studio中 RelativeLayout
  10. Android中如何将原生程序向stdout和stder