android中设置控件获得焦点


android中,要使控件获得焦点,需要先setFocus,再requestFocus。

以Button为例:

btn.setFocusable(true);
btn.setFocusableInTouchMode(true);

btn.requestFocus();
btn.requestFocusFromTouch();

或者在xml文件里设置:

 <RadioGroup
            android:id="@+id/auxilTabBarId"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            <-- 如下,缺一不可-->
            android:focusable="true"
            android:focusableInTouchMode="true">

获得失去焦点的监听器:

btn.setOnFocusChangeListener(new OnFocusChangeListener() {
  
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
   // TODO Auto-generated method stub
   if (hasFocus) {
    btn_box.setBackgroundResource(R.drawable.book_green);
   }else {
    btn_box.setBackgroundResource(R.drawable.book);
   }   
  }
 });


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM