我想读取触摸位置,同时触摸被移动并释放。 但是所有这些都在 LinearLayout 类中,我不知道该怎么做。 OnTouchListener 不能像这样工作:/
最佳答案
下面是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);
}
}
});
}
关于Android LinearLayout 触摸移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13169000/