我有一个带有LinearLayout的Activity,包含TextView,CheckBox和Button。 当我检查CheckBox时,没有任何可见的情况发生,但它的行为正确。但是,当我单击Button时,CheckBox会在Activity关闭之前正确显示(应该如此)。此外,该选项设置正确,因此CheckBox的状态确实发生了变化,但它并未显示更改。这是我的代码:
activity_setup_hint.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<LinearLayout
android:background="@drawable/info_box_inset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:padding="20dp">
<de.myProject.util.FontTextView
android:id="@+id/calint_txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30sp"
android:text="@string/setup_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<de.myProject.FontCheckBox
android:id="@+id/dont_show_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="-25dp"
android:button="@null"
android:checked="false"
android:drawableLeft="@drawable/my_checkbox_button"
android:drawablePadding="50dp"
android:text="@string/show_hint"
android:textSize="22sp"
/>
<de.myProject.util.FontButton
android:id="@+id/start_but_calibrate"
style="@style/myButton"
android:layout_gravity="center"
android:onClick="onFinishClick"
android:text="@string/ready" />
</LinearLayout>
SetupHintActivity.java
public class SetupHintActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup_hint);
}
public void onFinishClick(View view) {
CheckBox cb = (CheckBox) findViewById(R.id.dont_show_hint);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = sharedPref.edit();
editor.putBoolean("dont_show_hint", cb.isChecked());
editor.apply();
finish();
}
}
my_checkbox_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/my_button_checked" android:state_checked="true"/>
<item android:drawable="@drawable/my_button_unchecked" android:state_checked="false"/>
<item android:drawable="@drawable/my_button_unchecked"/>
</selector>
答案 0 :(得分:0)
设置CheckBox
的自定义图片不会因drawable
属性而改变..
使用 android:button
属性更改按钮状态..并在那里使用Drawable文件...
为CheckBox
定义自定义xml。
android:button="@drawable/my_checkbox_button"
您的XML代码..
<de.myProject.FontCheckBox
android:id="@+id/dont_show_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:button="@drawable/my_checkbox_button"
android:checked="false"/>
更好的例子:Create Custom Checkbox With The Use Of Selectors And Shapes
您也可以通过以下方式设置您的Drawables图像......
CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
// TODO Auto-generated method stub
if (buttonView.isChecked())
{
cb.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_button_checked,null,null,null); // in order to left ,top , right, bottom
}
else
{
cb.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_button_unchecked,null,null,null);
}
}
});
这可能会帮助你...
答案 1 :(得分:0)
试试这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:padding="20dp" >
<TextView
android:id="@+id/calint_txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
</LinearLayout>
<CheckBox
android:id="@+id/dont_show_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="5dp"
android:button="@drawable/my_checkbox_button"
android:checked="false"
android:text="CheckBox"
android:textSize="22sp" />
<Button
android:id="@+id/start_but_calibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="onFinishClick"
android:text="Button" />