我一直在寻找可以帮助我实现自定义复选框的帖子。我有一个复选框,用于确定它是否是最喜欢的,并使用两个不同的自定义图标来显示状态。我使用了小int作为数据类型( 0或1)确定是否喜欢。我尝试过使用onclicklistener以及oncheckchangelistener,但没有运气。
这是我的可绘制的
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_action_important" android:state_checked="false"/>
<item android:drawable="@drawable/ic_action_important_holo_dark" android:state_checked="true"/>
<item android:drawable="@drawable/ic_action_important_holo_dark" android:state_pressed="true"/>
<item android:state_focused="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
<!-- <item android:drawable="@drawable/ic_action_important"/> -->
</selector>
这是复选框代码..
<CheckBox
android:id="@+id/is_favourite_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/email_img_button"
android:layout_marginLeft="12dp"
android:clickable="true"
android:layout_toRightOf="@+id/email_img_button"
android:button="@drawable/checkbox_selector" />
这是片段中实现的代码。
is_Favourite.setOnCheckedChangeListener(this);
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (((CheckBox) buttonView).isChecked())
{
int result=mContactsManager.updateInfo(info.getId(), 1);
Log.d("Test", "Result is "+result +" ie is favourite");
} else {
int result=mContactsManager.updateInfo(info.getId(), 0);
Log.d("Test", "Result is "+result +" ie not a favourite");
}
}
当它的1我尝试将它设置为chkbox.setchecked(true),如果0到chkbox.setchecked(false)。但这似乎没有工作。我的逻辑中的任何错误? Plz做帮助。谢谢。
答案 0 :(得分:1)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_action_important_holo_dark" android:state_checked="true"/>
<item android:drawable="@drawable/ic_action_important" android:state_checked="false"/>
<item android:drawable="@drawable/ic_action_important"/>
</selector>
将此drawable设置为复选框按钮并尝试。让我知道您的问题是否已解决。
答案 1 :(得分:1)
试试这个选择器。
如果我将 drawable 错误,请更改它。
它在我的应用中工作。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
<item android:state_checked="false" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="true" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
<item android:state_checked="false" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
</selector>
答案 2 :(得分:0)
使用此 -
<CheckBox
...
android:button="@drawable/your_drawable_xml
/>
答案 3 :(得分:0)
几个月前我遇到了同样的问题,但最后我发现了一个简单而又甜蜜的解决方案,你必须使用两个不同的可绘制图像,一个用于表示已选中,另一个用于表示未选中!
<强> CustomCheckboxActivity.java 强>
package com.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class CustomCheckboxActivity extends Activity {
private CheckBox checkBox1;
private CheckBox checkBox2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox1=(CheckBox)findViewById(R.id.checkBox1);
checkBox2=(CheckBox)findViewById(R.id.checkBox2);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(checkBox1.isChecked()==true && checkBox2.isChecked()){
Toast.makeText(getApplicationContext(), checkBox1.getText()+" "+checkBox2.getText(), Toast.LENGTH_LONG).show();
}
else if (checkBox1.isChecked()==true){
Toast.makeText(getApplicationContext(), checkBox1.getText(), Toast.LENGTH_LONG).show();
}
else if(checkBox2.isChecked()==true){
Toast.makeText(getApplicationContext(), checkBox2.getText(), Toast.LENGTH_LONG).show();
}
}
});
}
}
<强> main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox
android:text=" OptionOne"
android:textColor="#000000"
android:button="@drawable/custom_checkbox"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<CheckBox
android:text=" OptionTwo"
android:textColor="#000000"
android:button="@drawable/custom_checkbox"
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<Button
android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
要自定义复选框,请在可绘制文件夹中创建xml文件 的 RES /抽拉/ custom_checkbox.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="@drawable/checkbox_checked"/>
<item
android:state_checked="false"
android:drawable="@drawable/checkbox_unchecked" />
答案 4 :(得分:0)
添加到CheckBox android:button="@drawable/your_drawable_xml"