提问人:mountothrys 提问时间:6/18/2023 更新时间:6/18/2023 访问量:18
RadioGroup 不传输数据或打开下一个活动
RadioGroup doesn't transfer data or open the next Activity
问:
我正在尝试在 Android Studio 上制作一个问卷应用程序,但每当我按下第一个问题页面上的继续按钮时,该应用程序就会关闭。
我为每个问题使用了一个 RadioGroup,每个 RadioButton 都包含一个数字,该数字将转移到下一个 Activity。在问卷的末尾,它将根据从答案中计算出的数字显示结果。
这就是我所做的,但我觉得我错过了什么。谁能帮我,甚至简化代码?谢谢!
第一题课:
package com.example.hobbytest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class first_question extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_question);
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.activeButton:
if(checked){
getScore(1);
}
break;
case R.id.passiveButton:
if(checked){
getScore(9);
}
break;
case R.id.bothButton:
if(checked){
getScore(18);
}
break;
}
}
public void getScore(int first_score) {
Button proceed_button = findViewById(R.id.proceed_button);
RadioGroup first_answers = findViewById(R.id.firstAnswers);
proceed_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// When proceed button is clicked,
// Get the Radio Button which is set
// If no Radio Button is set, -1 will be returned
int selectedId = first_answers.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(first_question.this,
"No answer has been selected",
Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(first_question.this, second_question.class);
intent.putExtra("First Score", first_score);
startActivity(intent);
}
}
} );
}
}
第一次Q活动
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".first_question">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="414dp"
android:layout_height="56dp"
android:layout_marginBottom="627dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/idlestopper"
android:textColor="#616161"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintStart_toStartOf="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<!-- Right most TextView -->
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintHorizontal_bias="0.859"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/view_icon"
tools:ignore="ImageContrastCheck,ImageContrastCheck,RedundantDescriptionCheck,SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="80dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="81dp"
android:src="@drawable/triple_circles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ImageContrastCheck" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="46dp"
android:text="@string/first_question"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<RadioGroup
android:id="@+id/firstAnswers"
android:layout_width="363dp"
android:layout_height="93dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/proceed_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.397">
<RadioButton
android:id="@+id/activeButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/active"
tools:ignore="TouchTargetSizeCheck"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/passiveButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/passive"
tools:ignore="TouchTargetSizeCheck,TouchTargetSizeCheck"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/bothButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/both"
tools:ignore="TouchTargetSizeCheck"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/proceed_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="@string/proceed"
android:textColor="#5D4037"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.862" />
</androidx.constraintlayout.widget.ConstraintLayout>
第二道题:
package com.example.hobbytest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class second_question extends AppCompatActivity {
Bundle extras = getIntent().getExtras();
int first_score = Integer.parseInt(extras.getString("First Score"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_question);
}
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.shortButton:
if(checked){
getScore(1);
}
break;
case R.id.mediumButton:
if(checked){
getScore(9);
}
break;
case R.id.longButton:
if(checked){
getScore(18);
}
break;
}
}
public void getScore(int second_score) {
Button proceed_button = findViewById(R.id.proceed_button);
RadioGroup second_answers = findViewById(R.id.secondAnswers);
proceed_button.setOnClickListener(new View.OnClickListener() {
int third_score = first_score + second_score;
@Override
public void onClick(View v) {
// When proceed button is clicked,
// Get the Radio Button which is set
// If no Radio Button is set, -1 will be returned
int selectedId = second_answers.getCheckedRadioButtonId();
if (selectedId == -1) {
Toast.makeText(second_question.this,
"No answer has been selected",
Toast.LENGTH_SHORT)
.show();
} else {
Intent intent = new Intent(second_question.this, third_question.class);
intent.putExtra("Third Score", third_score);
startActivity(intent);
}
}
} );
}
}
第二个问题活动:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".second_question">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="414dp"
android:layout_height="56dp"
android:layout_marginBottom="627dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/idlestopper"
android:textColor="#616161"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintStart_toStartOf="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
<!-- Right most TextView -->
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintHorizontal_bias="0.859"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/view_icon"
tools:ignore="ImageContrastCheck,ImageContrastCheck,RedundantDescriptionCheck,SpeakableTextPresentCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="80dp"
android:layout_marginTop="150dp"
android:layout_marginEnd="81dp"
android:src="@drawable/triple_circles"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ImageContrastCheck" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
android:layout_marginTop="28dp"
android:layout_marginEnd="46dp"
android:text="@string/second_question"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<RadioGroup
android:id="@+id/secondAnswers"
android:layout_width="363dp"
android:layout_height="93dp"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/proceed_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.397">
<RadioButton
android:id="@+id/shortButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/short_ans"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/mediumButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/medium_ans"
android:onClick="onRadioButtonClicked"
/>
<RadioButton
android:id="@+id/longButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_weight="1"
android:minHeight="48dp"
android:text="@string/long_ans"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/proceed_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="@string/proceed"
android:textColor="#5D4037"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.862" />
</androidx.constraintlayout.widget.ConstraintLayout>
答: 暂无答案
评论