简体   繁体   中英

Button Onclick Select and unselect state in android

i am developing/designing a quiz application in android, on the testActivity i have

a TextView which loads the questions and four answer(alternatives) buttons.

btnAnswer1, btnAnswer2, btnAnswer3, and btnAnswer4.

Problem:When i click on btnAnser1 as my answer, it should remain in selected state, however it should also be possible to unselect(normal state) it if i doubt the answer. and lastly if its selected, and i decide to go for btnAnswer2 whiles btnAnswer1 is selected, immediately i select btnAnswer2, btnAnswer1 should be unselected(normal) whiles btnAnswer2 is selected.

i hope my question is clear and it sounds quiet easy but am still new in android programming so will appreciate the help.

thanks.

 optionone = (Button) findViewById(R.id.optionone);
    optionone.setTag(1);
    optionone.setId(i);
    //optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));


    optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
    optionone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            v.setSelected(true);
            checkAnswer();
            final int change = (Integer) v.getTag();
            if (change == 1) {
                optionone.setBackgroundColor(Color.parseColor("#B2B2B2"));
                back.setVisibility(View.VISIBLE);
                next.setVisibility(View.VISIBLE);
                v.setTag(0);
            } else {
                optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
                back.setVisibility(View.GONE);
                next.setVisibility(View.GONE);
                v.setTag(1);

            }

        }
    });

尝试使用切换按钮而不是按钮,或者仅在按下按钮时更改按钮的背景颜色,而不是状态。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM