繁体   English   中英

TextView 按下时如何更改背景颜色?

[英]How to change TextView background color while it's pressed down?

我尝试在按下 TextView1 时更改它的背景。 这是我在drawable文件夹中的text.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@android:color/white" />
    <item android:drawable="@android:color/black"/>
</selector>

这是我的 Kotlin 文件:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_app)

        var textview1 = findViewById(R.id.TextView1) as TextView

        textview1.setOnClickListener{
            val t = TextView(this)
            t.setBackgroundResource(R.drawable.text)
        }
    }

我是根据这个回答的答案这样做的: Programatically make TextView background color change while press

文本视图1:

<TextView
        android:id="@+id/TextView1"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

有人知道我错过了什么吗? 我是初学者。


对不起,这是我的代码错误。 正确的代码是:

 var textview1 = findViewById(R.id.TextView1) as TextView

        textview1.setOnClickListener {
            //val t = TextView(this)
            textview1.setBackgroundResource(R.drawable.text)

您需要在 onClick 事件之前而不是之后设置 textview 的背景。 现在,在处理了单击事件后,颜色将应用于新的 Textview ( t )。 Textview 不是在屏幕上呈现的视图( textview1

使用以下方法之一:

<TextView
        android:id="@+id/TextView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/text"/>

或者

textview1.setBackgroundResource(R.drawable.text)
textview1.setOnClickListener{
             //Handle click
        }

此外,如果您将代码更改为:

textview1.setOnClickListener{
            textview1.setBackgroundResource(R.drawable.text)
        }

您会注意到 state 列表确实在第一次单击得到应用

仅当您想要点击 animation 的涟漪效果时使用

android:background="?android:attr/selectableItemBackground"

正如@Saurabh 所说,它在点击监听器中不起作用,只需将其放在外面或在 XML 文件中声明即可。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_training)

        var textview1 = findViewById(R.id.TextView1) as TextView

        val t = TextView(this)
        t.setBackgroundResource(R.drawable.text)

    }

Although as stated above you can do this programmatically, the correct and easy way to do this is to go to your styles file and add a customized style for your TextView, in the style you can add colors based on different states and control everything the TextView必须提供,然后只需 go 到您的.xml文件并添加样式属性并加载您刚刚创建的样式,这很容易并且可以在整个应用程序上重复使用,而不是每次都复制粘贴所有代码你需要做这样的事情。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_training)

        var textview1 = findViewById(R.id.TextView1) as TextView

        textview1.setOnClickListener{
            val t = TextView(this)
            t.setBackground(R.drawable.text)
        }
    }

对不起,这是我的代码错误。 正确的代码是:

 var textview1 = findViewById(R.id.TextView1) as TextView

        textview1.setOnClickListener {
            //val t = TextView(this)
            textview1.setBackgroundResource(R.drawable.text)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM