0

我试图在操作栏中使用复选框,我正在使用 actionbarsherlock。我已经非常努力地让复选框工作,现在我已经制作了 UI(使用 setCustomView 方法),但我被困在捕捉复选框的检查事件中,我对一些类似的问题进行了一些研究但是得到“复选框不能在操作栏中使用,它只能在 submemus 等中使用”的答案,我对此表示怀疑并想知道是否有办法让它工作......这是我的用户界面: 在此处输入图像描述

这是我的 CustomView xml 文件:

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="right"
    android:gravity="center_vertical" >

    <CheckBox
        android:id="@+id/action_anoni_check"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:checked="false"
        android:gravity="center"
        android:text="@string/anonymity" />

</LinearLayout>

这是我在我的 ui 中添加它的方式:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.write_actionbar_top); 
4

2 回答 2

1

我知道这是一个老问题,但我想我会插话,因为我无法让 Craig 的解决方案正常工作。这样做的“预期”方法是:

    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(R.layout.write_actionbar_top);

    CheckBox mCheckbox = (CheckBox) getActionBar().getCustomView().findViewById(R.id.action_anoni_check);
    mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                //DO YOUR on CHECK ACTION IN HERE
            }
        }
    });
于 2013-08-26T11:18:58.793 回答
0

您可能想要创建一个类实例变量并扩充您的 write_actionbar_top 视图,以便您可以引用它,然后将 onCheckListener 添加到您的复选框:

private CheckBox mCheckbox;

...
...

mCheckbox = getLayoutInflater().inflate(R.layout.write_actionbar_top, null,false);

mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if(isChecked){
            //DO YOUR on CHECK ACTION IN HERE
        }

    }
}

ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(mCheckbox); 

此代码尚未经过测试,但您应该明白这一点。

还有其他一些方法可以在此处的 CheckBox 视图上获取单击或检查操作 -> Android:复选框侦听器

于 2013-05-03T14:12:18.697 回答