Android的焦点(Focus)问题

来源:互联网 发布:银海医保软件 编辑:程序博客网 时间:2024/05/17 21:41

 

从JavaSwing平台过来的人,都会关注setFocusable()和requestFocus()方法,但是在Android的View中还有另外的两个个方法,setFocusableInTouchMode()和requestFocusFromTouch()方法。这个两个方法就是解决Android上的焦点获取问题的关键。

 

同时在View类中,还有一个isInTouchMode(),可以帮助我们在监听Focuse事件时判断是否执行click(). 代码如下:<textarea cols="50" rows="15" name="code" class="c-sharp">ImageButton.OnFocusChangeListener mFocusChangeListener = new ImageButton.OnFocusChangeListener(){ public void onFocusChange(View v, boolean hasFocus) { Log.d("FocuseChange", "Focuse has changed."); if (hasFocus) { // 如果是touchmode就执行click,否则就会只是选中。 v.setBackgroundDrawable(getResources().getDrawable(R.drawable.)); if (v.isInTouchMode()){ ((ImageButton)v).performClick(); } } else { v.setBackgroundDrawable(getResources().getDrawable(R.)); v.getBackground().setAlpha(100); } } }; </textarea> 

 

原创粉丝点击