1

在此处输入图像描述我想为线性布局声明这种类型的复选框并获取每个复选框 id 以及选定和未选择的值
这种类型的复选框值得到

for(l=0;l<listCheckbox.size();l++)
                       {                    
 CheckBox edtCheckbox = (CheckBox)llremo.findViewById(l);
         boolean checked=edtCheckbox.isChecked();
                           if (checked)
                           {  
                               Log.i("was checked",edtCheckbox.getText().toString());
                               strchekBocname=edtCheckbox.getText().toString();
                               Log.v("strchekBocname checkbox post string", strchekBocname);
                               maparray.put("Value", strchekBocname);   
                               listaddalldatofform13formValue.add(strchekBocname);

                           } 
4

1 回答 1

2

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Employee Average Salary" />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/grouplayout"
        >

</LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Employee Average Qualification" />
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/grouplayout2"
        >

</LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

MainActivity.java

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{
    String checkboxtext[]={"10000","20000",">20000","Mixed"};
    String checkboxtext1[]={"Graduate","Post Graduate"};
    LinearLayout layout,layout2;
    Button button1;
    String strCheck1 = "",strCheck2 = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        layout=(LinearLayout)findViewById(R.id.grouplayout);
        layout2=(LinearLayout)findViewById(R.id.grouplayout2);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 JSONObject obj=new JSONObject();
                 try {
                    obj.put("Employee Average Salary",strCheck2);
                     obj.put("Employee Qualification",strCheck1);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                 String jsonstring=obj.toString();
                 System.out.println(jsonstring);
            }
        });
         for(int i=1;i<=checkboxtext.length;i++)
            {
            final CheckBox checkbox=new CheckBox(this);
            checkbox.setId(i);
            checkbox.setText(checkboxtext[i-1]);
            layout.addView(checkbox);
            checkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    doCheck(checkbox);
                }
            });
            }
         for(int j=1;j<=checkboxtext1.length;j++)
         {
         final CheckBox checkbox1=new CheckBox(this);
         checkbox1.setId(j);
         checkbox1.setText(checkboxtext1[j-1]);
         layout2.addView(checkbox1);
         checkbox1.setOnClickListener(new View.OnClickListener() {
             public void onClick(View arg0) {
                 doCheck2(checkbox1); 
             }
         });
         }
        super.onCreate(savedInstanceState);
    }

    protected void doCheck2(CheckBox check1) {
        try {
            Toast.makeText(this, check1.getText(), Toast.LENGTH_SHORT).show();
            for(int i=0;i<layout2.getChildCount();i++)
            {
                CheckBox checkbox=(CheckBox)layout2.getChildAt(i);
                checkbox.setChecked(false);
            }
            check1.setChecked(true);
            strCheck1 = check1.getText().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doCheck(CheckBox check) {
        try {
            Toast.makeText(this, check.getText(), Toast.LENGTH_SHORT).show();
            for(int i=0;i<layout.getChildCount();i++)
            {
                CheckBox checkbox=(CheckBox)layout.getChildAt(i);
                checkbox.setChecked(false);
            }
            check.setChecked(true);
            strCheck2 = check.getText().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

单击按钮后复制此代码并检查 logcat 中的值。

于 2013-02-22T06:19:05.947 回答