简体   繁体   English

我在Android中具有复选框,图像视图和文本视图,我想在单击关联的文本视图时检查复选框

[英]I have checkbox, imageview and textview in android, I want to check my checkbox when i click the associated textview

Here is my code. 这是我的代码。

    if (view == null) {         
        view = lInflater.inflate(
                R.layout.list_item_multiple_choice, null);
        vHolderMultiple = new ViewHolder();
        vHolderMultiple.tv = (TextView) view.findViewById(R.id.tvMultipleChoiceListItem);
        vHolderMultiple.cb = (CheckBox) view.findViewById(R.id.cbMultipleListItem);
        vHolderMultiple.iv = (ImageView) view.findViewById(R.id.ivMultiChoiceItem);
        view.setTag(vHolderMultiple);
    } 
    else {
        vHolderMultiple = (ViewHolder) view.getTag();
    }
    vHolderMultiple.tv.setText(optionArray.get(position).getOptionText());
    vHolderMultiple.cb.setChecked(lvAnswers.isItemChecked(position));
    if (optionArrayCopy.get(position).getImageUrl() != null && optionArrayCopy.get(position).getImageUrl().length() > 0) {
        final Bitmap bmp = loader.loadImageBitmap(optionArray.get(position).getImageUrl(), resDir);
        vHolderMultiple.iv.setVisibility(VISIBLE);
        if (bmp != null) {
            vHolderMultiple.iv.setImageBitmap(bmp);
        }
    } else
        vHolderMultiple.iv.setImageBitmap(null);

Same issue is occurred with checkbox with listview. 带有listview的复选框发生相同的问题。

Use CheckedTextView UI component 使用CheckedTextView UI组件

OR 要么

You need to code that manually. 您需要手动进行编码。 Add the setonclicklistner to Textview & pass the checkbox object to it. 将setonclicklistner添加到Textview并将复选框对象传递给它。 On net lots of code sample available. 大量的代码示例可用。

Simply add a OnClickListener to the TextView and check/uncheck the CheckBox inside it. 只需将OnClickListener添加到TextView并选中/取消选中其中的CheckBox。 Something like this: 像这样:

vHolderMultiple.tv.setOnClickListener(new OnClickListener(){

    public void onClick(View v){

        if(vHolderMultiple.cb.isChecked())
            vHolderMultiple.cb.setChecked(false);
        else
            vHolderMultiple.cb.setChecked(true);
    }
});

Note that you must declare cd as final in order to be able to change its state from an OnClick method. 请注意,必须将cd声明为final才能从OnClick方法更改其状态。

It seems like you are using ListView to show some texts with image and checkbox. 似乎您正在使用ListView来显示带有图像和复选框的某些文本。

It's better to use a CheckedTextView instead, which is a TextView combined with a CheckBox. 最好改用CheckedTextView,它是结合了CheckBox的TextView。 And use TextView.setCompoundDrawablesWithIntrinsicBounds() to replace the ImageView. 并使用TextView.setCompoundDrawablesWithIntrinsicBounds()替换ImageView。

Then you can enable multiple choice mode by ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE). 然后,可以通过ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)启用多选模式。 And ListView will helps to manage the check state of each row. ListView将有助于管理每一行的检查状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在android中单击textView时更改cardview的textView的文本 - I want to change the text of textView of cardview on click of textView in android 我想要在单击checkbox1时,然后检查它,如果checkbox2在之前被选中,则将其强制为未选中状态(对于android) - I want when click on check box1, then it chekecd and if checkbox2 is checked before , force it to no checked state(for android) 在 Android 中,如何仅当 textView 中有文本时才启用复选框? - In Android, how can I enable a checkBox only if there is text in a textView? Android:如何在复选框中将textview和imageview分组 - Android: How to group a textview and imageview in a checkbox 单击关联的textview时如何启用复选框 - How to enable checkbox when associated textview is clicked 我有一个带有ImageView和TextView的自定义ListView吗? - I have a Custom ListView with an ImageView and TextView? 当我尝试将TextView放在ImageView前面时,TextView消失了 - TextView disappearing when I try to put it in front of my ImageView Android在需要时不更改我的TextView - Android not changing my TextView when I need it to 当我点击数量时,我想在图像下方,数量会自动出现在我的编辑文本中,数量是一个文本视图 - I want to below the image when i click on amount the amount will automatically come on my edit text amount is a textview 我在列表视图中自定义了某些项目,但我希望在文本视图中的所有项目中使用不同的文本视图 - I have custom certain items in my listview but i want different textview in all the item in textview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM