gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:48:49 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com