I try to use ternary operator to change the textcolor of button. Something like that: here is the xml.
leaves :: Tree a -> (Int, [a])
leaves''' Nil = (1, [])
leaves''' (Leaf a) = (1, [a])
leaves''' (Br l r) = let (n1, resl) = leaves''' l
(n2, resr) = leaves''' r
in (n1 + n2 + 1, resl ++ resr)
But the colors are not set correctly. I get some wierd purple colors instead.
I tried
<Button
android:id="@+id/actionButton"
android:layout_width="113dp"
android:layout_height="30dp"
android:background="@drawable/button"
android:backgroundTint="@{selected ? R.color.white : R.color.turquoise}"
android:text="@{selected ? "Selected " : "Select "}"
android:textColor="@{selected ? @color/white : @color/turquoise}"
android:onClick="@{(view) -> handler.selectClick(view)}"/>
with same result.
How should I do it?
答案 0 :(得分:0)
你的第一个变种应该可以正常工作。您可以参考this doc的“资源”一章。 这是一个完整的工作示例。
<强> colors.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="foo">#fff</color>
<color name="bar">#000</color>
</resources>
<强> main_activity.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="selected" type="boolean" />
<variable name="button2" type="String" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_a"
android:onClick="switchColor"
android:text="Click me"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_b"
android:textColor="@{selected ? @color/foo : @color/bar}"
android:text="@{button2}"/>
</LinearLayout>
</layout>
<强> ActivityMain.class 强>
public class ActivityMain extends AppCompatActivity {
public static final String TAG = "MainActivity";
MainActivityBinding mBinding;
boolean mSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = DataBindingUtil.setContentView(this, R.layout.main_activity);
mBinding.setButton2("Don't click me please!");
}
public void switchColor(View view) {
mBinding.setSelected(mSelected = !mSelected);
}
}