简体   繁体   中英

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. I am stuck and I just cant find a way out.

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 .

you can do following:

Inside your onCreate() add click listener on button like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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