android之前對于滑鼠光标事件的監控非常少,4.0之後多了一個hover的元件,此元件可以監控滑鼠光标在view上的變化。
代碼如下:
1. public class HoverDemoActivity extends Activity {
2. private Button btnBottom;
3. @Override
4. public void onCreate(Bundle savedInstanceState) {
5. super.onCreate(savedInstanceState);
6. setContentView(R.layout.main);
7. btnBottom = (Button) findViewById(R.id.btn_bottom);
8.
9. new OnHoverListener() {
10. @Override
11. public boolean onHover(View v, MotionEvent event) {
12. int what = event.getAction();
13. switch(what){
14. case MotionEvent.ACTION_HOVER_ENTER: //滑鼠進入view
15. "bottom ACTION_HOVER_ENTER");
16. break;
17. case MotionEvent.ACTION_HOVER_MOVE: //滑鼠在view上
18. "bottom ACTION_HOVER_MOVE");
19. break;
20. case MotionEvent.ACTION_HOVER_EXIT: //滑鼠離開view
21. "bottom ACTION_HOVER_EXIT");
22. break;
23. }
24. return false;
25. }
26. });
27. }
28. }