java - 当背景色调属性在按钮中为默认值时,如何更改android中单击按钮的背景颜色?
问题描述
These are the attributes I have used in this button.
I have also created one XML file button_round_corner_view.
<Button
android:id="@+id/true_button"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/button_round_corner_view"
android:padding="10dp"
android:text="True"
android:textColor="#000000"
android:textStyle="bold"
app:backgroundTint="#F8F4F4"
/>
This is the XML file attribute that I have created for a better button look.
please provide me the best answer for this problem and also tell me how to stop
默认情况下应用于按钮的背景色调属性
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="20dp"/>
<!-- This is the border color -->
<stroke
android:width="2dp"
android:color="@color/purple_700"/>
<!-- This is the background color -->
<solid
android:color="@color/white"/>
</shape>
按钮中的 BackgroundTint 属性是默认值。我想在用户单击按钮时更改按钮的背景颜色。如何实现这一点。请帮我。
解决方案
例如让我们说 button1 所以它会像
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Answer.equals("Correct")){
button1.setBackgroundColor(mContext.getResources().getColor(R.color.DarkGreen));
}else if(Answer.equals("InCorrect")){
button1.setBackgroundColor(mContext.getResources().getColor(R.color.red));
}
}
});
你可以做这样的事情。这就是改变按钮颜色的方式。
推荐阅读
- javascript - 使用列表过滤不同的 div
- variables - 如何设置队列时间变量等于预期值以在 azure 管道中执行模板
- firebase - 如何在云函数中使用 app 对象而不将其作为参数传递?
- python - Python中两条线段的交点
- java - JOOQ 与 Oracle19c:DSL 正确的 Oracle 版本
- jmc - 如何在 JMC 8 上生成 HTML JFR 报告?
- reactjs - 你怎么能在一个功能组件中做两个返回?
- c# - Websocket 连接失败
- laravel - 在 laravel 中使用选择选项作为变量
- python - Django - 查询帖子及其回复数?