Chinaunix
标题:
看代码学Android :如何处理Touch?
[打印本页]
作者:
zdwang
时间:
2011-12-23 02:09
标题:
看代码学Android :如何处理Touch?
public
class
test2
extends
Activity
implements
OnTouchListener
,
OnClickListener
{
/** Called when the activity is first created. */
GestureDetector mGestureDetector
;
@
Override
public
void
onCreate
(
Bundle savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
main
)
;
Button
btn
=
(
Button
)
this
.
findViewById
(
R
.
id
.
button1
)
;
btn
.
setOnTouchListener
(
this
)
;
btn
.
setOnClickListener
(
this
)
;
//创建一个手势dectector
mGestureDetector
=
new
GestureDetector
(
this
,
new
CalendarGestureListener
(
)
)
;
}
@
Override
public
boolean
onTouch
(
View
v
,
MotionEvent
event
)
{
if
(
mGestureDetector
.
onTouchEvent
(
event
)
)
{
return
true
;
}
return
super
.
onTouchEvent
(
event
)
;
}
class
CalendarGestureListener
extends
GestureDetector
.
SimpleOnGestureListener
{
/* (non-Javadoc)
* @see android.view.GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@
Override
public
boolean
onFling
(
MotionEvent e1
,
MotionEvent e2
,
float
velocityX
,
float
velocityY
)
{
Log
.
v
(
"touch"
,
"x="
+
Float
.
toString
(
e1
.
getX
(
)
)
+
" x2="
+
Float
.
toString
(
e2
.
getX
(
)
)
)
;
// TODO Auto-generated method stub
return
super
.
onFling
(
e1
,
e2
,
velocityX
,
velocityY
)
;
}
}
@
Override
public
void
onClick
(
View
arg0
)
{
// TODO Auto-generated method stub
Log
.
v
(
"touch"
,
"Hello, Click Me"
)
;
}
}
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2