android - 我可以在 android 的线性布局中声明这种类型的动态复选框吗?

标签 android checkbox

enter image description here我想为 linearlayout 声明这种类型的复选框,并获取每个复选框 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);

                           } 

最佳答案

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 中的值。

关于android - 我可以在 android 的线性布局中声明这种类型的动态复选框吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15002766/

相关文章:

android - 如何在android中以编程方式创建表格行和列边框

Java android - CookieHandler - 如何在关闭应用程序后保留 cookie?

html - 复选框组可以识别重复区域结果吗?

javascript - 产品过滤器 - 根据选中/取消选中的多个复选框显示结果

Android studio - 很多口味(> 70)时同步gradle花费的时间太长

javascript - 在 Phonegap Android 中存储值的最简单方法

java - 在 Android 中使用 Listview 从 Firebase 搜索数据

java - edittext可见意味着我如何检查android中的if条件

Java PDF Stamper 在复选框下方写入(文本被复选框覆盖)

javascript - Angular 形式复选框最初未包含在模型中