gpt4 book ai didi

java - Wrap_Content 带有服装单选按钮?

转载 作者:行者123 更新时间:2023-12-02 01:17:58 25 4
gpt4 key购买 nike

我制作了一个测验应用程序,但我遇到了单选按钮的问题。我创建了一个圆形背景并将其设置为我的单选按钮背景。但是,背景不会随着单选按钮内的文本量而放大。

这是一张图片

我无法发布信誉度低于 10 的图片。 /image/sVrfd.jpg

我在这里发表的第一篇文章,因此请原谅我的任何错误格式。预先感谢您的建议!!

这是我的圆形按钮和 quiz.xml 的 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>

测验.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 代码### 有点乱,但正如这里所要求的,这是完整的代码。如果我放入碎片,那么将找不到解决方案。

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();
}
}

最佳答案

检查一下:根据需要更换项目。

  <?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>

这是屏幕截图: enter image description here

关于java - Wrap_Content 带有服装单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com