android4.0鼠标

android之前对于鼠标光标事件的监控非常少,4.0之后多了一个hover的组件,此组件可以监控鼠标光标在view上的变化。

     代码如下:

public class HoverDemoActivity extends Activity {
	private Button btnBottom;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnBottom = (Button) findViewById(R.id.btn_bottom);
        
        btnBottom.setOnHoverListener(new OnHoverListener() {
			@Override
			public boolean onHover(View v, MotionEvent event) {
				int what = event.getAction();
				switch(what){
				 case MotionEvent.ACTION_HOVER_ENTER:  //鼠标进入view
					 System.out.println("bottom ACTION_HOVER_ENTER");
                     break;
                 case MotionEvent.ACTION_HOVER_MOVE:  //鼠标在view上
                	 System.out.println("bottom ACTION_HOVER_MOVE");
                     break;
                 case MotionEvent.ACTION_HOVER_EXIT:  //鼠标离开view
                	 System.out.println("bottom ACTION_HOVER_EXIT");
                     break;
				}
				return false;
			}
		});
    }
}

你可能感兴趣的:(android,Class,action,button)