0

我已经在 android 中制作了一个简单的自定义复选框程序,因为我根据用户操作将两个图像用于“故障”和“检查”状态,

在此处输入图像描述

我的代码是:

final ImageView chekbx =(ImageView)dialog.findViewById(R.id.chk_login);
            if(chekbx.isSelected()){
                System.out.println("checkbox check");
                chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            }else{
                chekbx.setBackgroundResource(R.drawable.checkbox);
            }
4

4 回答 4

2

为此目的使用选择器。

这是你的支票:

          <CheckBox
            android:id="@+id/remb_ckh_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_box_selector" />

及其选择器:

<?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_selected" />

<item android:state_checked="false" android:drawable="@drawable/checkbox_unselected" />

</selector>
于 2013-06-28T05:50:46.897 回答
1

尝试使用此代码:

已编辑

final ImageView chekbx =(ImageView)dialog.findViewById(R.id.chk_login);
boolean flag =false; //TAKE AS A PUBLIC VAR
chekbx.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(flag)
        {
            chekbx.setBackgroundResource(R.drawable.checkbox);
            flag=false;
        }
        else
        {
            System.out.println("checkbox check");
            chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            flag = true;
        }
    }
});

希望这会帮助你。

于 2013-06-28T05:51:23.677 回答
0

Android 已经有 CheckBox 视图,你不需要自己构建一个。

复选框 API:http: //developer.android.com/reference/android/widget/CheckBox.html

这个复选框也可以有自定义图像,使用选择器: How to change default images of CheckBox

于 2013-06-28T05:51:04.430 回答
0
    void onClick Login(View v)
    {
        if(checkbx.isSelected(){
            chekbx.setBackgroundResource(R.drawable.checkbox_ticked);
            <Set your new database with login details and phone ID to remember>
            //check your database for login details here with registered details
            }
        else{
            chekbx.setBackgroundResource(R.drawable.checkbox);
            //check your database for login details here with registered details
        }
    }   

这个逻辑应该可以帮助你。谢谢。

于 2013-06-28T06:02:28.593 回答