Android ListView CheckedTextView 闪烁

发布于 2024-10-07 01:00:25 字数 1367 浏览 0 评论 0原文

CheckedTextView 有问题,我似乎找不到解决方案。我什至不完全确定发生了什么。

我有一个自定义 ListView,其行包含 TextView 和 CheckedTextView。

row.xml

<CheckedTextView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:id="@+id/title"
    android:text="Name" 
    android:gravity="center_vertical"
    android:paddingRight="6dip"
    android:typeface="sans" 
    android:checkMark="?android:attr/textCheckMark"
    android:textSize="16sp" 
    android:textStyle="bold"/>

MyAdapterView.java

public class RuleAdapterView extends LinearLayout
{   
    private CheckedTextView title;

    ...

    title = (CheckedTextView)v.findViewById(R.id.title);
    title.setText(entry.getName());
    title.setChecked(entry.isActive());

    // setup a listener for the checkbox
    title.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();
        }
    });
}

在主 XML 文件中,我将 ListView 设置为 android:choiceMode="multipleChoice"

所以我想要的是 ListView 行是长的和短的可点击,并且 CheckedTextView 是单独的点击执行。这适用于 CheckedTextView 的文本部分。每当按下 CheckedTextView 时,文本就会“闪烁”。我慢慢地弄清楚到底发生了什么。当您按下 CheckTextView 时,白色文本会消失或切换为黑色(可能反转?),当您松开时,文本会重新出现并且复选标记会切换。按下 ListView 时不会出现“闪烁”效果。

对这里发生的事情有什么想法吗?

谢谢

Having an issue with CheckedTextView that I can't seem to find a solution. I'm not even entirely sure what's happening.

I have a custom ListView whose rows contain TextViews and a CheckedTextView.

row.xml

<CheckedTextView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:id="@+id/title"
    android:text="Name" 
    android:gravity="center_vertical"
    android:paddingRight="6dip"
    android:typeface="sans" 
    android:checkMark="?android:attr/textCheckMark"
    android:textSize="16sp" 
    android:textStyle="bold"/>

MyAdapterView.java

public class RuleAdapterView extends LinearLayout
{   
    private CheckedTextView title;

    ...

    title = (CheckedTextView)v.findViewById(R.id.title);
    title.setText(entry.getName());
    title.setChecked(entry.isActive());

    // setup a listener for the checkbox
    title.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();
        }
    });
}

And in the main XML file I set the ListView to android:choiceMode="multipleChoice".

So what I want is for the ListView rows to be long and short clickable and for the CheckedTextView to be separate click execution. This works with the exception of the text part of the CheckedTextView. Whenever either the CheckedTextView is pressed, the text 'flickers'. I did it slowly to figure out exactly what was going on. When you press down on the CheckTextView, the white text either disappears or toggles black (possibly inverts?) and when you release, the text reappears and the checkmark toggles. There is no 'flicker' effect when the ListView is pressed.

Any ideas on what's going on here?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-10-14 01:00:25

您应该尝试使用 OnTouchListener 而不是使用 OnClickListener

ckToggle.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                CheckedTextView ck = ((CheckedTextView) v);
                ck.toggle();
                return false;
            }
        });

Instead of using OnClickListener you should try using OnTouchListener

ckToggle.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                CheckedTextView ck = ((CheckedTextView) v);
                ck.toggle();
                return false;
            }
        });
别低头,皇冠会掉 2024-10-14 01:00:25

单击某个项目时,ListView 使用自己的方案(突出显示背景)。您可能想查看此解决方案 Android 如何制作单击时查看突出显示?

ListView uses its own scheme when an item is clicked (highlighting the background). You might want to check out this solution Android how to make View highlight when clicked?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文