解决自定义ActionBar布局中,无法为CheckBox添加监听器

自定义了一个布局用来取代系统默认的ActionBar布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
     >
 
    <Spinner 
      android:layout_centerVertical="true"
      android:id="@+id/location"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="10dp"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
     /> 
  
     <RadioGroup 
      android:id="@+id/mid"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:layout_toRightOf="@id/location"
      android:layout_centerVertical="true"
      android:gravity="center"
      android:orientation="horizontal"
      >
         <RadioButton 
           android:button="@null"
           android:background="@drawable/actionbar_mid_r_bg"
           android:id="@+id/btn_left"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_weight="1"
           android:text="@string/mid_left_btn"
           android:textColor="@color/black"
           android:padding="5dp"
          />   
          
          <RadioButton 
           android:button="@null"
           android:background="@drawable/actionbar_mid_r_bg"
           android:id="@+id/btn_right"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_weight="1"
           android:text="@string/mid_right_btn"
           android:textColor="@color/black"
           android:padding="5dp"
          />
     </RadioGroup>
  
     <CheckBox 
       android:id="@+id/actionbar_check"
       android:layout_centerVertical="true"
       android:button="@null"
       android:background="@drawable/actionbar_right_check"
       android:layout_alignParentRight="true"
       android:layout_marginRight="10dp"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
      />
 </RelativeLayout>

然后,我尝试给这个CheckBox添加一个OnCheckedChangeListener,但是却不能进入事件监听。

LayoutInflater inflater=LayoutInflater.from(this);
View view=inflater.inflate(R.layout.film_actionbar,null);
checkbox=(CheckBox)view.findViewById(R.id.actionbar_check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
            
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
           if (isChecked) {  
              Log.v("@_@",""+isChecked);      }else {  
              
             Log.v("@_@",""+isChecked);                }  
    }
           });  


后来,发现不能用这种方式获取ActionBar布局里组件的view,应该改为

View view=getActionBar().getCustomView();
checkbox=(CheckBox)view.findViewById(R.id.actionbar_check);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {  
            
            @Override  
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
           if (isChecked) {  
              Log.v("@_@",""+isChecked);      }else {  
              
             Log.v("@_@",""+isChecked);                }  
    }
           });  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值