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