[英]How to change border color of pressed button in android
I trying to change the border color of the button when it's in pressed state programmatically. 我试图以编程方式更改按钮处于按下状态时的边框颜色。 The reason that I'm doing this is because I made the dynamic theme.
我这样做的原因是因为我制作了动态主题。 Below you can see all my research but it doesn't really help me.
您可以在下面看到我的所有研究,但并不能真正帮助我。
I found how to change it using XML from this example Change border color when edittext is focused 我从该示例中找到了如何使用XML进行更改的方法当聚焦edittext时更改边框颜色
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true">
<shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" android:color="#ffa0a496" />
<corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
</shape>
</item>
Below code changes the background color based on the button which I got from here Android create selector programmatically 下面的代码根据我从此处获得的按钮更改背景颜色Android以编程方式创建选择器
public static StateListDrawable makeSelector(int color) {
StateListDrawable res = new StateListDrawable();
res.setExitFadeDuration(400);
res.setAlpha(45);
res.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(color));
res.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
return res;
}
view.setBackground(makeSelector(Color.RED));
And this code changes the border color programmatically but without specifying the state which I got from here Android Button or TextView Border programmatically without using setBackgroundDrawable method 并且此代码以编程方式更改边框颜色,但未指定我从此处获得的状态,即通过Android Button或TextView Border进行了编程,而未使用setBackgroundDrawable方法
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setBackgroundDrawable(gd);
onClick
of button
as programmatic
onClick
button
添加为programmatic
xml
in drawable folder
drawable folder
添加此xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorBlack"/>
<stroke android:width="2dp"
android:color="@color/colorwhite" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"
/>
<corners android:bottomRightRadius="@dimen/_30dp"
android:bottomLeftRadius="@dimen/_30dp"
android:topLeftRadius="@dimen/_30dp"
android:topRightRadius="@dimen/_30dp"/>
</shape>
//here we set background
btn.setBackgroundResource(R.drawable.btn_border);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.