Custom button, onkeydown inside the replication, does not work

Source: Internet
Author: User

Li Gang's Android crazy handout is really "crazy", wasting 3 days, whether his code is a problem, or how unclear.
Problem Description: In his book, section 3.3 is based on the callback event handling propagation routines. To disguise the example program based on the callback event propagation, the source code is as follows: Mybutton.java
  
 
  1. public class MyButton extends Button
  2. {
  3. public MyButton(Context context , AttributeSet set)
  4. {
  5. super(context , set);
  6. }
  7. @Override
  8. public boolean onKeyDown(int keyCode, KeyEvent event)
  9. {
  10. super.onKeyDown(keyCode , event);
  11. Log.v("-MyButton-" , "the onKeyDown in MyButton");
  12. //返回false,表明并未完全处理该事件,该事件依然向外扩散
  13. return false;
  14. }
  15. }
Propagation.java
  
 
  1. public class Propagation extends Activity
  2. {
  3. @Override
  4. public void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. MyButton bn = (MyButton)findViewById(R.id.bn);
  9. //bn.setFocusable(true);
  10. bn.requestFocus();
  11. bn.setFocusableInTouchMode(true);
  12. //为bn绑定事件监听器
  13. bn.setOnKeyListener(new OnKeyListener()
  14. {
  15. @Override
  16. public boolean onKey(View source
  17. , int keyCode, KeyEvent event)
  18. {
  19. //只处理按下键的事件
  20. if (event.getAction() == KeyEvent.ACTION_DOWN)
  21. {
  22. Log.v("-Listener-" , "the onKeyDown in Listener");
  23. }
  24. // 返回false,表明该事件会向外传播
  25. return false;
  26. }
  27. });
  28. }
  29. //重写onKeyDown方法,该方法可监听它所包含的所有组件的按键被按下事件
  30. @Override
  31. public boolean onKeyDown(int keyCode, KeyEvent event)
  32. {
  33. super.onKeyDown(keyCode , event);
  34. Log.v("-Activity-" , "the onKeyDown in Activity");
  35. //返回false,表明并未完全处理该事件,该事件依然向外扩散
  36. return false;
  37. }
  38. }
    MyButton inherits the button and overrides the OnKeyDown method, which is important when the control has the focus and triggers the method when a key is pressed on the component ,Similar to onkeyup and onkeypress. Here comes a focus issue.    So what is the focus problem: the focus can be understood as the object we are manipulating, the focus is here, and it can be manipulated. In fact, this mybutton is in the activity, and our programwhen running the focus on the activity, which caused the inside of the mybutton can not operate, I understand so, hehe. So the procedure to add two lines is:
    Here is the reason for using requstfocuns instead of setfocusable is to set whether to allow this control to have the ability to gain focus, which is the request to get focus, and the other is to set the property, anotherone is to execute the action, so we use the latter, Setfocunsableintouchmode is the way to get the focus, we are the focus when touching. When set, focus positioningin theon this button, when you click on any key of the simulator, the program is logcat correct output.     This procedure is intended to demonstrate that if any one of the event-handling methods returns True, the event also inherits outward propagation. I do not understand the book "first move the focus to the Program Interface button" is a few meaning, confused for a long time.




From for notes (Wiz)

Custom button, onkeydown inside the replication, does not work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.