Android学习笔记(三)——简单的控件和基本响应:(触屏)运动事件的响应

实例程序:

java主程序:

package com.shine.night;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity
{
    
    TextView action;
    TextView position;
    
    protected void onCreate ( Bundle b )
    {
        super.onCreate ( b );
        setContentView ( R.layout.main );
        action = ( TextView ) findViewById ( R.id.action  );
        position = ( TextView ) findViewById ( R.id.position );
    }
    
    public boolean onTouchEvent  ( MotionEvent event )
    {
        int Action = event.getAction();
        float  x = event.getX ();
        float y = event.getY ();
        action.setText ( "Action = " + Action );
        position.setText ( "Position =  + ( "+ x + "," + y +") " );
        return true;
    }
}
xml布局文件:

<?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/action"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textSize="20sp"
    />
    <TextView
        android:id="@+id/position"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</LinearLayout>




你可能感兴趣的:(android,Android开发,layout,实例,控件)