【问题标题】:Drawables next to buttons?按钮旁边的可绘制对象?
【发布时间】:2017-01-13 07:52:51
【问题描述】:

我一直在开发一个简单的应用程序,该应用程序具有一个功能,您可以通过回答多项选择题来测试您的知识。选择答案后(通过单选按钮),如果答案正确,我想在所选答案的右侧显示一个复选标记,或者如果答案不正确,我想显示一个“X”。出于某种原因,我的代码在第一次尝试时运行良好,然后在选择其他答案时没有任何改变。

这里是代码(它是适配器的一部分 - 子视图)

 /**
 * Creates the View of group(parent) in the ExpandableListView
 *
 * @return the View of each parent in the List
 */
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, final ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View parentView = inflater.inflate(R.layout.custom_list_parent, parent, false);
    Question myQuestion = myList.get(groupPosition);
    //setting the text of each parent
    TextView singleTitle = (TextView) parentView.findViewById(R.id.title);
    singleTitle.setText(myQuestion.getQuestion());
    //handling the indicator
    ImageView indicator = (ImageView) parentView.findViewById(R.id.indicator);
    //if the list is expanded use arrow up, if not then use arrow down
    int imageResourceId = isExpanded ? android.R.drawable.arrow_up_float : android.R.drawable.arrow_down_float;
    //initial arrow
    indicator.setImageResource(imageResourceId);
    indicator.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            /**
             * Will expand and collapse groups depending on previous state
             * @param v the View from which the ClickListener was triggered
             */
            if (isExpanded) ((ExpandableListView) parent).collapseGroup(groupPosition);
            else ((ExpandableListView) parent).expandGroup(groupPosition, true);

        }
    });

    return parentView;


}

/**
 * Creates the View of child in the ExpandableListView
 * @return the View of each child in the List
 */
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    final View childView = inflater.inflate(R.layout.custom_list_child_questions, parent, false);
    handleRadioButtons(childView,groupPosition);
    return childView;
}

/**
 * Checks if the child can be selected/pressed as well
 *
 * @return whether the child is selectable or not
 */
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return false;
}


public void handleRadioButtons(View childView, int groupPosition) {
    final Question myQuestion = myList.get(groupPosition);
    RadioGroup group = (RadioGroup) childView.findViewById(R.id.buttonGroup);
    initializeButtons(childView);
    setButtonText(myQuestion);
    //if one of the buttons is pressed
    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        /**
         * Will expand and collapse groups depending on previous state
         * @param group the button group this ActionListener will operate on
         * @param checkedId the ID of the individual button that was pressed
         */
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //get the index of the correct answer
            int indexAnswer = Integer.parseInt(myQuestion.getAnswer()) - 1;
            for (int i = 0; i < possibleAnswers.length; i++) {
                //if the button at position i was pressed
                if (checkedId == possibleAnswers[i].getId()) {
                    //if its the correct answer
                    if (i == indexAnswer)
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
                    else
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.wrong, 0);

                } else {
                    possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                }
            }
        }
    });

}

/**
 * Sets the text of each radio button as a possible answer
 * @param myQuestion the Question corresponding to the possible answers
 */
public void setButtonText(Question myQuestion) {
    for (int i = 0; i < possibleAnswers.length; i++) {
        possibleAnswers[i].setText(myQuestion.getPossibleAnswers()[i]);
    }
}

/**
 * Initializes the RadioButton array of possible answers
 * @param childView the View in which the radio buttons are defined
 */
public void initializeButtons(View childView) {
    //array of radioButtons, each button will correspond to a possible answer
    possibleAnswers[0] = (RadioButton) childView.findViewById(R.id.answer1);
    possibleAnswers[1] = (RadioButton) childView.findViewById(R.id.answer2);
    possibleAnswers[2] = (RadioButton) childView.findViewById(R.id.answer3);
    possibleAnswers[3] = (RadioButton) childView.findViewById(R.id.answer4);
}

}

和xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#6495ED"
              android:layout_width="match_parent"
              android:layout_height="135dp">

    <RadioGroup
            android:id="@+id/buttonGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="choice1"
                    android:drawablePadding="6dp"
                    android:textColor="#ffffff"
                    android:textSize="12dp"
                    android:id="@+id/answer1"/>


            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="choice 2"
                    android:drawablePadding="6dp"
                    android:textColor="#ffffff"
                    android:textSize="12dp"
                    android:id="@+id/answer2"/>


            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="choice 3"
                    android:drawablePadding="6dp"
                    android:textColor="#ffffff"
                    android:textSize="12dp"
                    android:id="@+id/answer3"/>

            <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="choice 4"
                    android:drawablePadding="6dp"
                    android:textColor="#ffffff"
                    android:textSize="12dp"
                    android:id="@+id/answer4"
            />

    </RadioGroup>

这就是问题在屏幕上的样子: problem

有什么想法吗?请帮忙!

编辑:

这就是我现在正在做的事情,试图玩弄可见性:

public void handleRadioButtons(final View childView, int groupPosition) {
    final Question myQuestion = myList.get(groupPosition);
    RadioGroup group = (RadioGroup) childView.findViewById(R.id.buttonGroup);
    initializeButtons(childView, myQuestion);
    setButtonText(myQuestion);
    //if one of the buttons is pressed
    group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        /**
         * Will expand and collapse groups depending on previous state
         *
         * @param group     the button group this ActionListener will operate on
         * @param checkedId the ID of the individual button that was pressed
         */
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //get the index of the correct answer
            for (int i = 0; i < possibleAnswers.length; i++) {
                //if the button at position i was pressed
                if (checkedId == possibleAnswers[i].getId()) {
                    possibleAnswers[i].getCompoundDrawables()[2].setVisible(true,true);
                }
            }
        }
    });
}



 /**
 * Initializes the RadioButton array of possible answers
 * @param childView the View in which the radio buttons are defined
 */
public void initializeButtons(View childView,Question myQuestion) {
    //array of radioButtons, each button will correspond to a possible answer
    possibleAnswers[0] = (RadioButton) childView.findViewById(R.id.answer1);
    possibleAnswers[1] = (RadioButton) childView.findViewById(R.id.answer2);
    possibleAnswers[2] = (RadioButton) childView.findViewById(R.id.answer3);
    possibleAnswers[3] = (RadioButton) childView.findViewById(R.id.answer4);
    for(int i=0;i<possibleAnswers.length;i++){
        if(Integer.parseInt(myQuestion.getAnswer()) - 1 == i){
            possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
            possibleAnswers[i].getCompoundDrawables()[2].setVisible(false,true);
        }
        else{
            possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.wrong, 0);
            possibleAnswers[i].getCompoundDrawables()[2].setVisible(false,true);

        }

    }
}

【问题讨论】:

    标签: java android radio-button expandablelistview drawable


    【解决方案1】:

    您可以在每个单选按钮旁边添加图像,在正确答案旁边添加绿色检查,在错误答案旁边添加红色 X,以编程方式将它们全部使用 setVisibility(GONE);,当您想显示正确答案时,只需 setVisibility(VISIBLE) 仅适用于正确答案和选择的答案

    【讨论】:

    • 如何设置drawable的可见性?我知道您可以使用 setVisible 来显示单选按钮,但我希​​望它们始终显示并仅隐藏可绘制对象。你认为我应该在每个按钮旁边添加一个图像视图?
    • 你必须为drawables设置Visibility,drawables是ImageView,在java代码中处理它,为ImageView设置Visibility(GONE),并使用setVisibility(VISIBLE)显示它们
    • 非常感谢您的帮助。请检查我的编辑,我仍然对如何做到这一点感到困惑。
    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 2016-03-21
    • 2023-03-28
    相关资源
    最近更新 更多