我想读取触摸位置,触摸时移动并释放。但是所有这些都在 LinearLayout 类中,我不知道该怎么做。OnTouchListener 不能这样工作:/
问问题
2812 次
1 回答
1
下面是在android的线性布局中处理触摸事件的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/l_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
public class MainActivity extends Activity {
private LinearLayout llLayout;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
llLayout = (LinearLayout)findViewById(R.id.l_layout);
llLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE :
Toast.makeText(MainActivity.this, "Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()), Toast.LENGTH_SHORT).show();
return true;
case MotionEvent.ACTION_DOWN :
Toast.makeText(MainActivity.this, "Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()), Toast.LENGTH_SHORT).show();
return true;
default :
return super.onTouch(v,event);
}
}
});
}
于 2012-10-31T23:50:52.420 回答