HeJ小鼠! 我真的很感激一些帮助。我已经研究过论坛,但似乎无法找到解决方案。我想在选中单选按钮时转到另一个活动(页面),然后单击“提交”。如果选中了另一个RadioButton,那么我想转到另一个活动页面。我被困住了,我无法找到出路。
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="com.example.android.dietchallenge.second">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/write_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_weight="1"
android:layout_margin="20dp"/>
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="20dp"/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/vegan" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/vege"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/gluten"/>
<RadioButton
android:id="@+id/radio4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all"
android:onClick="onRadioButtonClicked"/>
<Button
android:id="@+id/summary1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:text="@string/question1_submit"
android:onClick="enter"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
&#13;
package com.example.android.dietchallenge;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.Arrays;
public class second extends AppCompatActivity {
TextView myMessage;
TextView summary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String message = getIntent().getStringExtra("message_key");
myMessage = (TextView) findViewById(R.id.write_name);
myMessage.setText("Hello " + message + "!");
summary = (TextView) findViewById(R.id.summary);
summary.setText("Let's start by figuering out what you like. This is your first question." + "\n" + "Which is your favourite diet?");
}
public void enter(View view) {
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio1:
if (checked) {
Intent page = new Intent(second.this, three.class);
startActivity(page);}
// Pirates are the best
break;
case R.id.radio2:
if (checked)
{
Intent page = new Intent(second.this, four.class);
startActivity(page);}
// Ninjas rule
break;
}
}
}
&#13;
非常感谢!
答案 0 :(得分:0)
您尚未在Button
上添加点击监听器。
您可以执行以下操作:
在onCreate()
内添加点击监听器,如下所示。
findViewById(R.id.summary1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton veganRadioButton = (RadioButton) findViewById(R.id.radio1);
RadioButton vegeRadioButton = (RadioButton) findViewById(R.id.radio2);
if(veganRadioButton.isChecked()) {
Intent page = new Intent(second.this, three.class);
startActivity(page);
} else if(vegeRadioButton.isChecked()) {
Intent page = new Intent(second.this, four.class);
startActivity(page);
} else {
//neither radio1 nor radio2 is selected.
}
}
});