将一个可绘制对象放置在按钮的右侧

3
我正在开发一款生成多项选择题的应用程序。当用户选择一个按钮(单选按钮)时,它会在所选答案旁边放置一个X的图像(如果错误)或V的图像(如果正确)。我创建了包含所有按钮的RadioGroup,并使用了OnCheckedChanged Listener。我跟踪并放置打印语句以确保它起作用,它确实起作用。但是,除了第一次用户按下之外,它根本不显示可绘制图像。

换句话说,用户将按下其中一个按钮,将看到反馈(V或X),但然后按下不同的按钮,就看不到任何东西,尽管逻辑确实起作用(我在if内部使用了打印语句)。我使用SetCompoundDrawableWithIntrinsicBounds,但它就是不起作用。有人有什么想法如何解决这个问题吗?

我已经花了过去8个小时在这上面工作,非常沮丧。

监听器代码:

 @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) {
                        Log.i("MyAcitvity", "thats correct");
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
                    }
                    else {
                        Log.i("MyActivity", "thats wrong");
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.wrong, 0);
                    }

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

感谢大家,祝你们有愉快的一天。
1个回答

0

我认为最好先获取单选按钮,然后像这样进行更改。

View radioButton = group.findViewById(i);
radioButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);

为了得到更好的答案,请添加所有与您的单选组相关的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接