【问题标题】:RadioButton Intent activity - androidRadioButton Intent 活动 - android
【发布时间】:2018-02-05 23:44:07
【问题描述】:

嘿嘿! 我真的很感激一些帮助。我已经研究了论坛,但似乎无法找到解决方案。 I want to move to another activity (page) when a radio button is checked and I click submit.如果选中了另一个 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>

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

非常感谢!

【问题讨论】:

标签: java android radio-button


【解决方案1】:

您尚未在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.
        }
    }
});

【讨论】:

  • 天哪,非常感谢@KalpeshPatel!
  • Em ...是否也可以知道如何做到这一点,所以不能同时点击所有?
  • 将您所有的RadioButton 嵌入RadioGroup
  • 好的,谢谢。确实是“偶然”弄明白的 :D :D 但谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 2012-06-17
相关资源
最近更新 更多