我尝试更改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 pressed
TextView1:
<TextView
android:id="@+id/TextView1"
android:layout_width="50dp"
android:layout_height="50dp"/>
有人知道我在想什么吗?我是乞gg。
对不起,这是我在代码中的错误。正确的代码是:
var textview1 = findViewById(R.id.TextView1) as TextView
textview1.setOnClicklistener {
//val t = TextView(this)
textview1.setBackgroundResource(R.drawable.text)