android界面响应事件


很多人都想在activity加载完毕的同时处理一下事件,如弹出一个popWindow或者其他,但在onCreate方法中处理这个时间的话,程序会报错:大意是activity还没有加载完,不能处理弹窗事件,其实不光是弹窗,只要用到activity.this的事件,在activity加载完之前都不能处理,原因很明显——popWindow或者是dialog的显示都要依附在一个parent Activity上,所以在Activity加载完之间,他们都不能显示。

不过万幸的是android给开发者提供了一个方法,可以解决这个问题,这个方法就是onWindowFocusChanged()

 

其说明如下:

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so should always be called.

Note that this provides information about global focus state, which is managed independently of activity lifecycles. As such, while focus changes will generally have some relation to lifecycle changes (an activity that is stopped will not generally get window focus), you should not rely on any particular order between the callbacks here and those in the other lifecycle methods such as onResume().

As a general rule, however, a resumed activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take window input focus without pausing the foreground activity.

Java代码   收藏代码
  1. @Override  
  2.     public void onWindowFocusChanged(boolean hasFocus)  
  3.     {  
  4.         if (hasFocus)  
  5.         {  
  6.         }  
  7.     }  
 


View组件将事件分为六种:
(1)OnClickListener接口:
          回调方法为 public void onClick(View v):v为单击发生事件的组件
(2)OnLongClickListener接口:长按事件
          回调方法为 public boolean onLongClick(View v):返回true表示事件已经处理完毕,返回false表示事件还未处理完毕
(3)OnFocusChangeListener接口:组件焦点改变事件
          回调方法为  public void onFocusChange(View v,boolean hasFocus):hasFocus表示事件源的状态,是否获得焦点
(4)OnKeyListener接口:手机键盘事件的监听
          回调方法为 public boolean onKey(View v,int keyCode,KeyEvent event):keyCode为键盘码,event为键盘事件封装的对象
(5)OnTouchListener接口:处理手机屏幕事件  Viev触摸、按下、抬起、滑动都会触发该事件
          回调方法为 public boolean onTouch(View v,MotionEvent)
(6)OnCreateContextMenuListener接口:处理上下文菜单被创建的事件
          回调方法为 public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info):menu 为上下文的菜单,info为该对象封装了有关上下文菜单其他信息 。创建上下文菜单使用的是:registerForContextMenu(View v)本质是为View 组件v注册该接口,并实现了相应的回调方法。
 
当事件没有被任意的View处理时,会在Activity中调用响应的回调方法(Android SDK提供5个默认的回调方法)
(1)public boolean onKeyDown(int keyCode,KeyEvent event)方法是接口KeyEvent.Callback的抽象方法,返回true表示已经完成该事件,返回false表示该事件还可以被其他的监听器处理
(2)public boolean onKeyUp(int keyCode,KeyEvent event)
(3)public boolean onTouchEvent(MotionEvent event)方法在View中定义
        屏幕按下MotionEvent.getAction()值为Motion.ACTION_DOWN;
        离开屏幕时值为Motion.ACTION_UP;
        滑动时值为Motion.ACTION_MOVE
        返回true表示事件完成,false表示未完成
(4)public boolean onTrackballEvent(MotionEvent event):处理手机中轨迹球的的相关事件,true完成,false为未完成
(5)protected void onFocusChanged(boolean gainFocus,int direction,Rect previouslyFocusRect)该方法只能在View中重写
        direction表示焦点移动的方向 previouslyFocusRect表示触发事件的View的坐标事件中前一个获得交掉的矩形区域
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值