Android 将按钮背景更改为单击
当你在Android中按下一个按钮时,它会改变颜色。我希望它保持这样。我是说我想要这样的东西:Android 将按钮背景更改为单击,android,button,background,Android,Button,Background,当你在Android中按下一个按钮时,它会改变颜色。我希望它保持这样。我是说我想要这样的东西: Button btn = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { btn.setBackGroundColor(R.drawable.clicked /
Button btn = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btn.setBackGroundColor(R.drawable.clicked /*clicked style*/ );
}
});
例如:
这是一个按下的按钮。我希望在我停止按它之后它保持这样。有没有这样简单的方法:
android.R.drawable.btn_default//这是默认的按钮样式,我想要按下的按钮:D您可以尝试这种方法,按下时将图像设置为黑色或彩色 drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/buttondark"/>
<item android:state_pressed="true" android:drawable="@drawable/buttondark" />
<item android:drawable="@drawable/button" />
</selector>
您可以尝试这种方式,按下时将图像设置为黑色,也可以设置颜色 drawable/selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@drawable/buttondark"/>
<item android:state_pressed="true" android:drawable="@drawable/buttondark" />
<item android:drawable="@drawable/button" />
</selector>
在java代码中写入
按钮。setPressed(true)代码>。它将在按下模式下设置按钮。当您想在java代码写入按钮中将参数设置为按下和未按下时,可以将参数更改为true和false…代码>。它将在按下模式下设置按钮。当您想将参数设置为按下和未按下时,您可以将参数更改为true和false…那么您想让它开始不按下,然后在被单击后保持按下?
尝试使用OnTouchListener,而不是添加btn.setPressed(true)代码>:
所以你想让它开始不被按下,然后在被点击后保持按下状态?
尝试使用OnTouchListener,而不是添加btn.setPressed(true)代码>:
查看StateSelector图像
您可以为按钮/视图的不同状态设置不同的资源。此示例设置了不同的颜色:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/branded_content_pressed_color" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@color/branded_content_pressed_color" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@color/branded_content_background_color"/>
</selector>
查看StateSelector图像
您可以为按钮/视图的不同状态设置不同的资源。此示例设置了不同的颜色:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/branded_content_pressed_color" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@color/branded_content_pressed_color" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@color/branded_content_background_color"/>
</selector>
尝试使用选择器:
在drawable文件夹中定义selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>
并将按钮的背景设置为android:background=“@drawable/selector”
,然后在按钮点击事件的代码中设置btn.setpressed(true)代码>尝试使用选择器:
在drawable文件夹中定义selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>
并将按钮的背景设置为android:background=“@drawable/selector”
,然后在按钮点击事件的代码中设置btn.setpressed(true)代码>查看此博客这将帮助您。如果答案有效,请接受。查看此博客这将帮助您。如果答案有效,请接受。这似乎是一个很好的解决方案。但在selector.xml中,它给出了错误:“错误:未找到与给定名称匹配的资源(在'drawable'处,值为'@drawable/buttondark')”和“错误:未找到与给定名称匹配的资源(在'drawable'处,值为'@drawable/button')。”仅仅因为图像不在资源文件夹中,所以将图像或颜色替换为buttondarkIt似乎是一个很好的解决方案。但在selector.xml中,它给出了错误:“错误:未找到与给定名称匹配的资源(在'drawable'处,值为'@drawable/buttondark')”和“错误:未找到与给定名称匹配的资源(在'drawable'处,值为'@drawable/button')。”只是因为资源文件夹中没有图像,所以将图像或颜色替换为ButtonDarks。当我在其他方法中使用setPressed时,单击buttons onClick,就可以了。但当我在onClick中使用它时,它不起作用。我认为当onClick事件完成时,它会自动将setPressed更改为false。当我在其他方法中使用setPressed时,按钮onClick就会起作用。但当我在onClick中使用它时,它不起作用。我认为当onClick事件完成时,它会自动将setPressed更改为false。