一个字符串中的三个复选框值

时间:2013-02-01 04:49:06

标签: java android

我有三个复选框,如下所示

Facebook CheckBox1  GooglePlus CheckBox2  Twitter CheckBox3

我曾尝试过以下代码:

if (facebookChackbox.isChecked()) 
    {
        facebook_checkbox = true;

        AppUtil.facebookNetworkList_EditPost ="Facebook";
        list.add("facebook");
        Intent i = new Intent();
        i.putExtra("facebook_boolean", true);
        Log.e("facebook checkbox", "facebook checkbox" + facebook_checkbox);
    }
    else
    {
        AppUtil.facebookNetworkList_EditPost ="";
    }
    if (twitterChackbox.isChecked()) 
    {
        twitter_checkbox = true;
        AppUtil.twitterNetworkList_EditPost ="Twitter";
        list.add("twitter");
        Intent i = new Intent();
        i.putExtra("twitter_boolean", true);
        Log.e("twitter  checkbox", "twitter  checkbox" + twitter_checkbox);

    }
    else
    {
        AppUtil.twitterNetworkList_EditPost ="";
    }
    if (googleChackbox.isChecked()) 
    {
        AppUtil.gpNetworkList_EditPost ="Google Plus";
        googleplus_checkbox = true;
        list.add("gplus");
        Intent i = new Intent();
        i.putExtra("google_plus_boolean", true);
        Log.e("google plus checkbox", "google plus checkbox" + googleplus_checkbox);

    }
    else
    {
        AppUtil.gpNetworkList_EditPost ="";
    }

    AppUtil.SocialNetworkingList_EditPost = AppUtil.facebookNetworkList_EditPost+""+AppUtil.twitterNetworkList_EditPost+""+AppUtil.gpNetworkList_EditPost;

我在APPUTIL类中声明了所有这些静态字符串。

当我检查了所有复选框,或者一两个复选框时,它给我null,null,null取决于检查chechk框。

我想要选中复选框的复选框值。

private void processCheckboxes() {
    // TODO Auto-generated method stub

    list = new ArrayList<String>();
    sb = new StringBuilder();

    if (facebookChackbox.isChecked()) 
    {
        facebook_checkbox = true;

        AppUtil.facebookNetworkList_EditPost ="Facebook";
        list.add("facebook");
        Intent i = new Intent();
        i.putExtra("facebook_boolean", true);
        Log.e("facebook checkbox", "facebook checkbox" + facebook_checkbox);
    }
    else
    {
        AppUtil.facebookNetworkList_EditPost ="";
    }
    if (twitterChackbox.isChecked()) 
    {
        twitter_checkbox = true;
        AppUtil.twitterNetworkList_EditPost ="Twitter";
        list.add("twitter");
        Intent i = new Intent();
        i.putExtra("twitter_boolean", true);
        Log.e("twitter  checkbox", "twitter  checkbox" + twitter_checkbox);

    }
    else
    {
        AppUtil.twitterNetworkList_EditPost ="";
    }
    if (googleChackbox.isChecked()) 
    {
        AppUtil.gpNetworkList_EditPost ="Google Plus";
        googleplus_checkbox = true;
        list.add("gplus");
        Intent i = new Intent();
        i.putExtra("google_plus_boolean", true);
        Log.e("google plus checkbox", "google plus checkbox" + googleplus_checkbox);

    }
    else
    {
        AppUtil.gpNetworkList_EditPost ="";
    }

    AppUtil.SocialNetworkingList_EditPost = AppUtil.facebookNetworkList_EditPost+""+AppUtil.twitterNetworkList_EditPost+""+AppUtil.gpNetworkList_EditPost;


    for (int zz = 0; zz < list.size(); zz++) {

        network = list.get(zz);

        if (zz < list.size() - 1) 
        {

            Log.i("list.size()",""+list.size());
            Log.i("list.size() - 1",""+list.size());
            Log.i("network",""+network);

            sb.append(network + "," + network + "," + network);

            Log.i("sb",""+sb);
        } 
        else
        {
            Log.e("log", "List----" + zz + network);
            network_list = network;

            network_list = network + "," + network + "," + network;
            // network_list=network+""+network+"&gplus="+network+"&twitter"+
            // "" +network;

            sb.append(network + "," + network + "," + network);
        }
    }
}

上面是我现在调用的方法,我想将检查的值存储在字符串中,并希望使用它来设置文本。但是我得到了facebookfacebookfacebook,意味着重复字符串正在发生。

1 个答案:

答案 0 :(得分:0)

yourcheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {             

                if (isChecked) {
                    //do something when checkbox is checked
                }
                else {          
                    //do something when checkbox is unchecked
                }
            }});

内置复选框已更改的侦听器使用这些可以轻松通知所有复选框的更改

为代码中的所有三个复选框创建checkedchangelistener .Friend