问题描述:

I want to read touch position, While on the touch is moved, and released.

But all of this in LinearLayout class and i have no idea how to do this. OnTouchListener doesn`t work like this :/

网友答案:

Below is the code for handling touch event in linear layout of 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);
       }
      }
   });
 }
相关阅读:
Top