作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个测验应用程序,但我遇到了单选按钮的问题。我创建了一个圆形背景并将其设置为我的单选按钮背景。但是,背景不会随着单选按钮内的文本量而放大。
我无法发布信誉度低于 10 的图片。 /image/sVrfd.jpg
我在这里发表的第一篇文章,因此请原谅我的任何错误格式。预先感谢您的建议!!
这是我的圆形按钮和 quiz.xml 的 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>
<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" />
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>
关于java - Wrap_Content 带有服装单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57646538/
我在 IntelliJ 中创建了一个 gradle java 项目来处理从文件中写入和读取 JSON 对象。它还包含 2 个服装异常类。我的 libgdx 游戏项目需要这个用于当前正在开发的 andr
我想用这种结构进行 Activity : 这个想法是在顶部放置一些文本,在顶部放置一个横幅,在横幅上方放置按钮,并将我的 Canvas 放在 View 的中心,使用所有可能的空间。 我对 View 进
我正在开发一个 ASP.Net 电子商务应用程序。我使用 ASP.Net Web 表单。当我完成整个应用程序时,我考虑缓存一些数据,以便提高应用程序性能。 (坏主意......我必须从一开始就考虑到这
我是一名优秀的程序员,十分优秀!