嗨,我设置了一个CheckedTextView,但是我不能让onClick事件正常工作。我将onClick代码放在main.layout的onCreate中,但在第101行得到一个空指针,即chkBox.setOnClickListener(new View.OnClickListener() )。列表视图是在AsyncTask的onPostExecute中创建的。有人能帮帮忙吗?
我的CheckedTextView:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listCheckboxview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:gravity="left"
android:textColor="#0075AB" android:textStyle="bold" android:textSize="14dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"
android:focusable="true"
android:text=""
/> 我的onClick事件:
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.listCheckboxview);
chkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
((CheckedTextView) v).toggle();
}
});发布于 2011-11-04 06:33:00
我把onClick代码放在main.layout的onCreate中,但是在第101行得到了一个空指针,它是chkBox.setOnClickListener(
View.OnClickListener()
这意味着chkBox是null,这意味着安卓没有找到R.id.listCheckboxview。确保您在正确的事情上调用findViewById() (在这里,您似乎是在activity上调用它,但您的问题提到了一个ListView)。另外,尝试清理您的项目(从Eclipse主菜单中选择project > Clean,或者从命令行中选择ant clean ),因为有时R常量会失去同步。
发布于 2015-10-06 16:25:35
您可以使用具有空背景和空按钮的ToggleButton。ToggleButton组件还有另一个有趣的特性,就是将一个文本设置为On状态,另一个设置为Off状态。在下面的示例中,我还包含了文本颜色的选择器。
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:background="@null"
android:paddingLeft="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="@drawable/toggle_text"
android:textOn="My on state"
android:textOff="My off state" />toggle_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="@color/app_color" />
<item
android:color="@android:color/darker_gray" />
</selector>https://stackoverflow.com/questions/8002473
复制相似问题