package com.TestSingleTouch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestSingleTouch extends Activity {
TextView textEvent, textX, textY;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout MainLayout = (LinearLayout)findViewById(R.id.mainlayout);
textEvent = (TextView)findViewById(R.id.event);
textX = (TextView)findViewById(R.id.x);
textY = (TextView)findViewById(R.id.y);
MainLayout.setOnTouchListener(OnTouchListener);
}
private View.OnTouchListener OnTouchListener
= new View.OnTouchListener(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// TODO Auto-generated method stub
textX.setText("x: " + String.valueOf(motionEvent.getX()));
textY.setText("y: " + String.valueOf(motionEvent.getY()));
int action = motionEvent.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
textEvent.setText("ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
textEvent.setText("ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
textEvent.setText("ACTION_UP");
break;
case MotionEvent.ACTION_CANCEL:
textEvent.setText("ACTION_CANCEL");
break;
default:
textEvent.setText("Unknown!");
}
return true; //means event have been processed
}
};
}
<?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"
android:id="@+id/mainlayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/event"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/x"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/y"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Related Post:
- Detect Touch Event, test on Custom View
- Detect Multi-Touch Event
- Detect Multi-Touch Event, test on Custom View
No comments:
Post a Comment