1 android android-xml android-button android-selector
我是android的新手,我创建了简单的应用程序.当点击按钮textview应该改变该功能工作正常但按钮图像按下时不会改变.
这是我在fragment_main.xml中的按钮
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/plus"
android:onClick="changeText" />
Run Code Online (Sandbox Code Playgroud)
这里是drawable文件夹中custom.xml文件的代码.(没有默认的drawable文件夹,我创建了它)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/plus_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/plus_highlighted" android:state_focused="true"/>
<item android:drawable="@drawable/plus"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
plus_selected.png,plus_highlighted.png和plus.png文件位于drawable-hdpi文件夹中
这是我的java代码
public void changeText(View v) {
TextView tvText = (TextView) findViewById(R.id.tvDisplay);
tvText.setText("you cliked the button");
}
Run Code Online (Sandbox Code Playgroud)
当单击按钮textview更改为"您单击按钮"但plus.png图像始终作为按钮图像存在.按下时它不会改变
我该如何解决这个问题?
你应该改变
android:background="@drawable/plus"
Run Code Online (Sandbox Code Playgroud)
至
android:background="@drawable/custom"
Run Code Online (Sandbox Code Playgroud)
设置背景为selector.custom.xml在你的情况下是选择器.