android-如何具有可点击和可检查的textview [英] android - how to have a clickable AND checkable textview

查看:73
本文介绍了android-如何具有可点击和可检查的textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要以下内容:一个文本视图.)单击时更改其背景.)保持背景,直到再次单击为止.

I want to have the following: a textview that .)changes its background when clicked .)maintains that background until it is clicked again

所有这些都归结为可检查"状态,但是我无法弄清楚它是如何工作的.这是我用于背景的xml:

it all comes down to the "checkable" state, but i couldnt figure out how this exactly works. here is the xml i am using for background:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- pressed -->
<item android:drawable="@drawable/menuselected"
android:state_pressed="true" />

<!-- checked -->
<item android:drawable="@drawable/menuselected"
android:state_checked="true" />

<!-- default -->
<item android:drawable="@drawable/transpixel"/>

</selector>

更新:现在部分起作用.我采用了 http://kmansoft.com/2011/01的大部分代码/11/checkable-image-button/用于我的自定义Textview.我实际上是这样做的,我也需要单选按钮的功能.现在,我可以检查Textview,但不能取消选中它.有人知道为什么会这样吗?

Update: it partly works now. I adopted most of the code from http://kmansoft.com/2011/01/11/checkable-image-button/ for my custom Textview. I did this as actually, I need the functionality of a radio button as well. Now I can check a Textview, but I cant uncheck it. Does anybody see why that could be the case?

推荐答案

您可以将CheckedTextView与checkMark null和为您的可选背景

You can use CheckedTextView with checkMark null and background your selectable

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkMark="@null"
    android:background="@drawable/selectable"/>

您可以选择

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/selector" />
</selector>

这篇关于android-如何具有可点击和可检查的textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆