Onclick和onTouch在android中无法正常工作

时间:2012-03-30 05:36:29

标签: android ontouchlistener buttonclick

我创建了一个在touch listener和onclick listener上实现的活动。 两者都没有合作。如果ontouch工作点击不起作用,如果onclick工作ontouch不起作用。 这是我的代码

 public class QuickStart extends Activity implements OnTouchListener 
    {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    private ViewFlipper viewFlipper;
    int page = 0;
    private Button btn_menu;
    private LinearLayout layout_menu;
    Developer developer = new Developer();
    private ArrayList<SideMenuItem> arrayList = new ArrayList<SideMenuItem>();
    private SideMenuEntryAdapter adapter;
    private ListView list_side;
    private Context context;
    private int flag = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.quickstart);
        context = this;
        gestureDetector = new GestureDetector(new MyGestureDetector());
        setupUI();
        arrayList = developer.setupList();
        adapter = new SideMenuEntryAdapter(this, arrayList);
        list_side.setAdapter(adapter);
        list_side.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                System.out.println(position);
                Developer.Menu(context, position,Developer.quick_start);
                if (flag == 0) {
                    flag = 1;
                    layout_menu.setVisibility(LinearLayout.VISIBLE);
                } else {
                    flag = 0;
                    layout_menu.setVisibility(LinearLayout.GONE);
                }
            }
        });
    }
    private void setupUI() {
        viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
        viewFlipper.setOnTouchListener(this);
        btn_menu = (Button) findViewById(R.id.btn_menu);
        layout_menu = (LinearLayout) findViewById(R.id.layout_menu);
        list_side = (ListView) findViewById(R.id.listview_side);
        btn_menu.setClickable(true);
        layout_menu.setClickable(true);
        list_side.setClickable(true);
        btn_menu.setFocusable(true);
        btn_menu.setFocusableInTouchMode(true);
        layout_menu.setFocusable(true);
        layout_menu.setFocusableInTouchMode(true);
        list_side.setFocusable(true);
        list_side.setFocusableInTouchMode(true);
        btn_menu.setEnabled(true);
        layout_menu.setEnabled(true);
        list_side.setEnabled(true);
        btn_menu.setOnClickListener(new Button.OnClickListener() {
        @Override
            public void onClick(View v) {
                if (v == btn_menu) {
                    System.out.println("Menu");
                    if (flag == 0) {
                    flag = 1;
                    layout_menu.setVisibility(LinearLayout.VISIBLE);
                    } else {
                        flag = 0;
                    layout_menu.setVisibility(LinearLayout.GONE);
                    }
                }
            }
        });
    } 
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {

                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    if (page < 7) {
                        page++;
                        viewFlipper.showNext();
                    }
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                    if (page > 0) {
                        page--;
                    viewFlipper.showPrevious();
                    }
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
             if (gestureDetector.onTouchEvent(event)) {
                 return false;
                } else {
                 return true;
                }
    }

3 个答案:

答案 0 :(得分:1)

public boolean onTouch(View v, MotionEvent event)
{
  int Action=event.getAction();
  switch(Action)
  {
    case MotionEvent.ACTION_DOWN: Log.d(tag , "down"); break;
    case MotionEvent.ACTION_MOVE: Log.d(tag , "move"); break;
    case MotionEvent.ACTION_UP: Log.d(tag , "up"); break;
  }

  return true;
}

尝试此代码用于动作事件。我希望这会有所帮助。

答案 1 :(得分:1)

让您的活动实现onGestureListener。

然后在你的活动中添加

private GestureDetector gestureScanner; 
gestureScanner = new GestureDetector(this); 
private static final int SWIPE_MIN_DISTANCE = 100;  
private static final int SWIPE_THRESHOLD_VELOCITY = 100; 

然后覆盖onTouchEvent(MotionEvent事件),如下所示:

@Override    
 public boolean onTouchEvent(MotionEvent event) {   
      return gestureScanner.onTouchEvent(event);    
 } 

最后,您需要覆盖onFling方法,例如:

@Override     
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        /* on scroll to the next page */         
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY            ) {
             //Ur code goes here         }        
 /* on scroll to the previous page  */ 
 else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE  && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY                 ) {        
     //ur code goes here.        

 }         return false;     } 

答案 2 :(得分:0)

你必须在听之前注册听众setOnTouchListener和setOnClickListener。