简体   繁体   English

Wrap_Content与服装单选按钮?

[英]Wrap_Content with costume radio button?

I have made a quiz app but I am facing problems with the radio buttons. 我做了一个测验应用程序,但单选按钮却遇到了问题。 I created a rounded background and set it as my radio button background. 我创建了一个圆形的背景,并将其设置为我的单选按钮背景。 However, the background doesn't enlarge with the amount of text inside the radio button. 但是,背景不会随着单选按钮内的文本量而扩大。

Here is an image 这是一张图片

I cannot post an image with less than 10 reputation. 我不能发布信誉低于10的图像。 https://i.imgur.com/5M9Y6cH.jpg https://i.imgur.com/5M9Y6cH.jpg

My first post here so forgives me for any bad formatting. 我在这里的第一篇文章原谅任何格式错误。 Thanks in advance for the advice!! 在此先感谢您的建议!

Here is my XML code for a rounded button and for quiz.xml 这是我的圆形按钮和quiz.xml的XML代码

rounded_option.xml rounded_option.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
            <solid android:color="@android:color/holo_orange_light" />
            <corners android:radius="150dp" />
        </shape>
    </item>
</selector>

quiz.xml quiz.xml

<RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view_question"
        tools:ignore="RelativeOverlap">


        <RadioButton
            android:id="@+id/radio_button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:background="@drawable/rounded_option"
            android:padding="5dp"
            android:text="Option 1 \n option1 \n option"
            android:textAlignment="gravity"
            android:textSize="20dp"
            tools:ignore="HardcodedText" />

Activity Code### A bit messy but as asked here is the full code. 活动代码###有点混乱,但是这里要求的是完整代码。 If I put fragments then the solution will not be found. 如果我放碎片,那么将找不到解决方案。

import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.ColorStateList;
import android.graphics.Color;


import android.graphics.drawable.Drawable;
import android.os.Bundle;


import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;


import static android.graphics.Color.GREEN;
import static android.graphics.Color.RED;



public class QuizActivity extends AppCompatActivity {

    public static final String EXTRA_SCORE = "extraScore";
    private TextView textViewQuestion;
    private TextView textViewScore;
    private TextView textViewQuestionCount;
    private TextView textViewCorrectAnswer;
    private RadioGroup rbGroup;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private RadioButton rb4;
    private Button buttonConfirmNext;
    private long backPressedTime;

    private ColorStateList textColorDefaultRb;
    private Drawable textColorDefaultButton;
    private Drawable textColorDefaultAnswer;

    private List<Question> questionList;
    private int questionCounter;
    private int questionCountTotal;
    private Question currentQuestion;

    private int score;
    private boolean answered;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        textViewQuestion = findViewById(R.id.text_view_question);
        textViewScore = findViewById(R.id.text_view_score);
        textViewCorrectAnswer = findViewById(R.id.text_view_correctAnswer);
        textViewQuestionCount = findViewById(R.id.text_view_question_count);
        rbGroup = findViewById(R.id.radio_group);
        rb1 = findViewById(R.id.radio_button1);
        rb2 = findViewById(R.id.radio_button2);
        rb3 = findViewById(R.id.radio_button3);
        rb4 = findViewById(R.id.radio_button4);

        buttonConfirmNext = findViewById(R.id.button_confirm_next);

        textColorDefaultRb = rb1.getTextColors();
        textColorDefaultButton = rb1.getBackground();


        QuizDbHelper dbHelper = new QuizDbHelper(this);
        questionList = dbHelper.getAllQuestions();
        questionCountTotal = 20;
        Collections.shuffle(questionList);

        showNextQuestion();

        buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!answered) {
                    if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked() || rb4.isChecked()) {
                        checkAnswer();
                    } else {
                        Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    showNextQuestion();
                    textViewCorrectAnswer.setText("");
                    textViewCorrectAnswer.setBackground(textColorDefaultAnswer);
                }
            }
        });
    }


    private void showNextQuestion() {
        rb1.setTextColor(textColorDefaultRb);
        rb2.setTextColor(textColorDefaultRb);
        rb3.setTextColor(textColorDefaultRb);
        rb4.setTextColor(textColorDefaultRb);


        rb1.setBackground(textColorDefaultButton);
        rb2.setBackground(textColorDefaultButton);
        rb3.setBackground(textColorDefaultButton);
        rb4.setBackground(textColorDefaultButton);


        rbGroup.clearCheck();

        if (questionCounter < questionCountTotal) {
            currentQuestion = questionList.get(questionCounter);

            textViewQuestion.setText(currentQuestion.getQuestion());
            rb1.setText(currentQuestion.getOption1());

            rb2.setText(currentQuestion.getOption2());
            rb3.setText(currentQuestion.getOption3());
            rb4.setText(currentQuestion.getOption4());

            questionCounter++;
            textViewQuestionCount.setText("Question: " + questionCounter + "/" + questionCountTotal);
            answered = false;
            buttonConfirmNext.setText("Confirm");
        } else {
            finishQuiz();
        }
    }

    private void checkAnswer() {
        answered = true;

        RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
        int answerNr = rbGroup.indexOfChild(rbSelected) + 1;

        if (answerNr == currentQuestion.getAnswerNr()) {
            score++;
            textViewScore.setText("Score: " + score);
            textViewCorrectAnswer.setText("Your Answer is Correct");
            textViewCorrectAnswer.setTextColor(GREEN);

        }
        else {
            textViewCorrectAnswer.setText("Your Answer is Incorrect");
            textViewCorrectAnswer.setTextColor(RED);
        }
        showSolution();
    }

    private void showSolution() {

        rb1.setBackgroundColor(Color.RED);
        rb2.setBackgroundColor(Color.RED);
        rb3.setBackgroundColor(Color.RED);
        rb4.setBackgroundColor(Color.RED);


        switch (currentQuestion.getAnswerNr()) {
            case 1:
                rb1.setBackgroundColor(GREEN);

                break;
            case 2:
                rb2.setBackgroundColor(GREEN);


                break;
            case 3:
                rb3.setBackgroundColor(GREEN);


                break;
            case 4:

                rb4.setBackgroundColor(GREEN);


        }

        if (questionCounter < questionCountTotal) {
            buttonConfirmNext.setText("Next");

        } else {
            buttonConfirmNext.setText("Finish");

        }

    }

    private void finishQuiz() {
        Intent resultIntent = new Intent();
        resultIntent.putExtra(EXTRA_SCORE, score);
        setResult(RESULT_OK, resultIntent);
        finish();
    }

    @Override
    public void onBackPressed() {
        if (backPressedTime + 2000 > System.currentTimeMillis()) {
            finishQuiz();
        } else {
            Toast.makeText(this, "Press back again to finish", Toast.LENGTH_SHORT).show();
        }

        backPressedTime = System.currentTimeMillis();
    }
}

check this out : replace items as you need . 检查一下:根据需要更换物品。

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="250dp">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="RelativeOverlap">


        <RadioButton
            android:background="@drawable/rounded_option"
            android:id="@+id/radio_button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:padding="5dp"
            android:text="afeeeeeeeeeeeeeeeeeeeesdfsfsdfsdfsfsdsfddddddddddddddddddddddsfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdffsdffsdfsdfdsf"
            android:textAlignment="gravity"
            android:textSize="20dp"
            tools:ignore="HardcodedText" />
    </RadioGroup>

</RelativeLayout>

Here is the screenshot : 这是屏幕截图: 在此处输入图片说明

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

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