【问题标题】:Touch (not click) listener触摸(非点击)监听器
【发布时间】:2012-08-08 14:57:39
【问题描述】:

前两天我尝试在 Android API7 中编写简单的拖放功能,但一直遇到问题。现在有了触摸监听器。我使用 onTouchListener 但 onTouch 方法只有当我在 UI 元素上按下屏幕时才会调用它。当我在另一个地方按下屏幕,然后我将手指移到指定 onTouchListener 的元素上方时,什么也没发生。为什么? 是否有任何适用于 android 的侦听器可以在不点击 UI 元素所在位置的屏幕的情况下捕获触摸事件?感谢您的帮助,因为我今天要疯了;)

【问题讨论】:

    标签: ontouchlistener


    【解决方案1】:

    onTouchListener 仅用于视图。你可以将它用于任何布局(例如,LinearLayout)。如果您的布局带有“fill_parent”参数,它将适用于您的所有屏幕。您还需要控制您的 ACTION_DOWN、UP 和 MOVE 参数。也许这个样本可以帮助你:

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.LinearLayout;
    
    public class main extends Activity implements OnTouchListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll =(LinearLayout)this.findViewById(R.id.ll);
        ll.setOnTouchListener(this);
    }
    
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        int action=event.getAction();
        StringBuilder str=new StringBuilder();
        str.append("\nActrion type: ");
    
        switch(action)
        {
            case MotionEvent.ACTION_DOWN: str.append(«ACTION_DOWN\n»);break;
            case MotionEvent.ACTION_MOVE: str.append(«ACTION_MOVE\n»);break;
            case MotionEvent.ACTION_UP: str.append(«ACTION_UP\n»);break;
        }
    
        Log.v(«Mytag», str.toString());
        return true;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多