android复选框代码,android复选框动态

我试图动态地向LinearLayout添加复选框.我使用了以下代码,

private class ListAdapters extends ArrayAdapter {

private ArrayList items;

private int position;

public ListAdapters(Context context,int textViewResourceId,ArrayList mTitleList) {

super(context,textViewResourceId,mTitleList);

this.items = mTitleList;

}

@Override

public View getView(int position,View convertView,ViewGroup parent) {

View v = convertView;

this.position = position;

if (v == null) {

LayoutInflater inflater = (LayoutInflater) mContext

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

v = inflater.inflate(R.layout.applicationlistitem,null);

}

final ApplicationBean o = (ApplicationBean) items.get(position);

if (o != null) {

txtAppName = (TextView) v.findViewById(R.id.app_name);

txtAppName.setText("" + o.getAppName());

launchButton = (Button) v.findViewById(R.id.launch_btn);

launchButton.setTag(position);

launchButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

final PackageManager pm = mContext.getPackageManager();

Intent LaunchIntent = pm

.getLaunchIntentForPackage(items

.get(Integer.parseInt(v.getTag()

.toString())).getPname());

mContext.startActivity(LaunchIntent);

}

});

rdgPassFail = (RadioGroup) v.findViewById(R.id.status_group);

rdgPassFail.setTag(position);

RadioButton passBtn = (RadioButton) v

.findViewById(R.id.pass_btn);

passBtn.setTag(position);

RadioButton failbtn = (RadioButton) v

.findViewById(R.id.fail_btn);

failbtn.setTag(position);

rdgPassFail

.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group,int checkedId) {

ApplicationBean o = (ApplicationBean) items

.get(Integer.parseInt(group.getTag()

.toString()));

switch (checkedId) {

case R.id.fail_btn:

Log.e("Fail button","Clicked");

o.setFailState(true);

o.setPassState(false);

numOptions = 0;

Log.e("Fail button--1","Clicked");

break;

case R.id.pass_btn:

Log.e("Pass button","Clicked");

o.setFailState(false);

o.setPassState(true);

Log.e("Pass button-----1","Clicked");

break;

}

items.set(Integer.parseInt(group.getTag()

.toString()),o);

}

});

Log.i("checkBoxFlag","checkBoxFlag not true " + position);

LinearLayout featuresTable = (LinearLayout) v

.findViewById(R.id.failure_reasonslist);

for (int i = 0; i <= 5; i++) {

CheckBox feature1 = new CheckBox(this.getContext());

featuresTable.addView(feature1);

Log.i("Inside for loop","creating check Box " + position);

}

checkBoxFlag = true;

txtDescription = (EditText) v

.findViewById(R.id.description_text);

txtDescription.setTag(position);

if (txtDescription.isFocused()) {

InputMethodManager inputManager = (InputMethodManager) mContext

.getSystemService(INPUT_METHOD_SERVICE);

inputManager.restartInput(txtDescription);

}

txtDescription

.setOnFocusChangeListener(new View.OnFocusChangeListener() {

@Override

public void onFocusChange(View v,boolean hasFocus) {

if (!hasFocus) {

final EditText Caption = (EditText) v;

o.setDescription(Caption.getText()

.toString());

}

}

});

uninstallButton = (Button) v.findViewById(R.id.uninstall_btn);

uninstallButton.setTag(position);

// uninstallButton.setVisibility(View.INVISIBLE);

o.setUninstallVisible(false);

uninstallButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Uri packageUri = Uri.parse("package:"

+ items.get(

Integer.parseInt(v.getTag().toString()))

.getPname());

Intent uninstallIntent = new Intent(

Intent.ACTION_DELETE,packageUri);

uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(uninstallIntent);

mTitleList.remove(items.get((Integer) v.getTag()));

mListView.setAdapter(new ListAdapters(mContext,R.id.app_name,mTitleList));

((BaseAdapter) mListView.getAdapter())

.notifyDataSetChanged();

isUninstallclicked = true;

}

});

submitButton = (Button) v.findViewById(R.id.submit_btn);

submitButton.setTag(txtDescription);

submitButton.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

EditText tv = (EditText) v.getTag(); // get edittext

// object

txtDescription = tv;

if (txtTesterName.getText().toString().equals("")) {

showDialog("Please enter the name of tester",mContext);

} else if (numOptions == 0) {

showDialog("Please select failure reason",mContext);

} else if (tv.getText().toString().equals("")) {

showDialog("Please enter the description",mContext);

} else if (!isNetworkAvailable()) {

showDialog(

"No network connection.Report won't be submitted",mContext);

} else {

if (!o.isUninstallVisible()) {

uninstallButton.setVisibility(View.VISIBLE);

o.setUninstallVisible(true);

mListView.invalidate();

}

PostRequest p = new PostRequest(Integer.parseInt(tv

.getTag().toString()));

p.execute();

}

}

});

}

return v;

}

@Override

public int getItemViewType(int position) {

// TODO Auto-generated method stub

return position;

}

@Override

public int getViewTypeCount() {

// TODO Auto-generated method stub

return items.size();

}

}

根据代码,视图中应该有六个复选框.但当我检查了超过六个复选框时.请在下面找到日志(从logcat复制).日志也来了两次.我在列表视图中有一些其他控件.这些是在布局xml中定义的.但是我不能在设计时限制这些复选框的数量.所以我试图动态加载.有人可以帮忙吗?

10:40:23.163: I/Inside for loop(440): creating check Box 0

10:40:23.223: I/Inside for loop(440): creating check Box 0

10:40:23.223: I/Inside for loop(440): creating check Box 0

10:40:23.273: I/Inside for loop(440): creating check Box 0

10:40:23.273: I/Inside for loop(440): creating check Box 0

10:40:23.303: I/Inside for loop(440): creating check Box 0

10:40:23.393: I/c

10:40:23.393: I/Inside for loop(440): creating check Box 0

10:40:23.423: I/Inside for loop(440): creating check Box 0

10:40:23.423: I/Inside for loop(440): creating check Box 0

10:40:23.503: I/Inside for loop(440): creating check Box 0

10:40:23.503: I/Inside for loop(440): creating check Box 0

10:40:23.553: I/Inside for loop(440): creating check Box 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 复选框组件是一种可以允许用户选择一个或多个选项的 UI 控件。它通常呈现为一个小方框,里面包含一个勾选标记。用户可以通过点击方框来选择或取消选择选项。 在 Android 中,复选框组件是 CheckBox 类。除了基本的选择和取消选择之外,CheckBox 还提供了一些其他的功能,例如设置复选框的文本标签、设置复选框的状态(选中、未选中、不确定)、设置复选框的背景等。 以下是一个简单的示例,演示如何在 Android 应用程序中使用复选框组件: ```xml <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项3" /> ``` 在代码中,可以使用 CheckBox 的 isChecked() 方法来检查复选框是否被选中。例如,以下代码将显示一个 Toast 消息,指示哪些复选框被选中: ```java CheckBox checkBox1 = findViewById(R.id.checkBox1); CheckBox checkBox2 = findViewById(R.id.checkBox2); CheckBox checkBox3 = findViewById(R.id.checkBox3); String message = "选中的选项:"; if (checkBox1.isChecked()) { message += "选项1 "; } if (checkBox2.isChecked()) { message += "选项2 "; } if (checkBox3.isChecked()) { message += "选项3 "; } Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); ``` 总的来说,CheckBox 是一个非常有用的 Android UI 控件,可以让用户方便地选择多个选项。它的使用方法很简单,只需要在布局文件中添加 CheckBox,然后在代码中检查它们的状态即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值