简体   繁体   English

RadioButton Intent活动-android

[英]RadioButton Intent activity - android

Hej! I would really appreciate some help. 我真的很感谢您的帮助。 I have already researched forums but cant seem to find a solution. 我已经研究过论坛,但似乎找不到解决方案。 I want to move to another activity (page) when a radio button is checked and I click submit. 选中单选按钮并单击提交后,我想转到另一个活动(页面)。 If another RadioButton is checked, then I want to move to another activity-page. 如果选中了另一个RadioButton,那么我想转到另一个活动页面。 I am stuck and I just cant find a way out. 我被困住了,我找不到出路。

XML: 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> 

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

Thank you very much! 非常感谢你!

You have not added click listener on Button . 您尚未在Button上添加点击侦听器。

you can do following: 您可以执行以下操作:

Inside your onCreate() add click listener on button like this. 在您的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.
        }
    }
});

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

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