我想根据字符串值在android Button View中显示不同样式的按钮,我能以编程方式做到这一点吗?
即使我将setTextAppearance设置为不反映在视图中,也可以通过编程方式使用set样式条件吗?
item = "first, "
splitItems = item.split(', ')
splitItems[0] // Works, the contents are 'first'
splitItems[1] // Works, the contents are empty
答案 0 :(得分:1)
已弃用您使用的setTextAppearance()
这种形式。将您的代码更改为:
if (Build.VERSION.SDK_INT < 23) {
dynakey[ID].setTextAppearance(this, R.style.LeftDisableDynakeyButton);
} else {
dynakey[ID].setTextAppearance(R.style.LeftDisableDynakeyButton);
}
因为引入了API 23 setTextAppearance(styleId)
。
尝试一下。
为您的代码编辑:
(1)这行:
private Button dynakey[]= new Button[9];
创建一个由9个按钮组成的数组,而不是8个作为注释状态。
(2)您将更改应用到try
块内(我看不到catch
块),因此不会传播任何错误。可能是按钮的ID不正确吗?
(3)发布您的样式和xml
答案 1 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_button_gray" android:state_enabled="false" />
<item android:drawable="@drawable/shape_button_colored" android:state_enabled="true" />
</selector>
现在将此选择器添加到按钮作为背景
<Button
android:id = "@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_button"
android:textColor="@android:color/white" />
现在输入代码
if(sButtonString.trim().length() == 0) {
dynakey[ID].setEnabled(true)
} else {
dynakey[ID].setEnabled(false)
}