如何根据单选按钮更改按钮单击时的布局?

时间:2016-02-28 10:17:47

标签: android android-layout android-intent android-sdk-2.3

我希望在点击按钮时更改活动,并根据选中的单选按钮打开新布局。我有三个单选按钮。我想根据选中的单选按钮和下一个按钮单击更改布局。这里是我的代码



    package com.edkul.vimal.edkul;

    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    import android.app.Activity;
    import android.content.Intent;

    public class Join_AS_A extends AppCompatActivity {

        private RadioGroup radioGroup;
        private RadioButton radioButton;
        private Button btn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_join__as_);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

            radioGroup  = (RadioGroup) findViewById(R.id.joinGroup);
            radioGroup.clearCheck();
            int selectedId = radioGroup.getCheckedRadioButtonId();
            radioButton = (RadioButton) findViewById(selectedId);
            btn = (Button)findViewById(R.id.nextButton);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String selection = (String) radioButton.getText();
                    Log.i("Print here", selection);
                    if (selection.equals("Student") && view ==btn){
                        Intent intentMain = new Intent(Join_AS_A.this, Student_Signup.class);
                        Join_AS_A.this.startActivity(intentMain);
                        Log.i("Content ", " Main layout ");
                    }

                }
            });
        }

    }


这是xml代码

Here is the xml file :

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.edkul.vimal.edkul.Join_AS_A" tools:showIn="@layout/activity_join__as_"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/joinAsA" android:textSize="30sp" android:id="@+id/textView2" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <RadioGroup android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="47dp" android:layout_alignRight="@+id/textView2" android:layout_alignEnd="@+id/textView2" android:id="@+id/joinGroup"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaStudent" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton" android:checked="false" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaTeacher" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton2" android:checked="false" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinasaparent" android:textSize="20sp" android:textColor="#33b5e5" android:id="@+id/radioButton3" android:checked="false" /> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/joinNext" android:id="@+id/nextButton" android:background="@drawable/custom_button" android:layout_alignBottom="@+id/joinGroup" android:layout_centerHorizontal="true" android:layout_marginBottom="79dp" /> </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您必须更改点击的代码,例如: -

radioGroup = (RadioGroup) findViewById(R.id.joinGroup);
    btn = (Button) findViewById(R.id.nextButton);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int selectedId = radioGroup.getCheckedRadioButtonId();
            if (selectedId == R.id.radioButton) {
                Intent intentMain = new Intent(MainActivity.this,
                        Student_Signup.class);
                startActivity(intentMain);
                Log.i("Content ", " Main layout ");
            }

        }
    });

当您在oncreate()中获得已检查的单选按钮ID时,即使用户选择其他单选按钮,其引用也不会更改。因此,当用户单击下一个按钮然后必须相应地执行操作时,您必须找到所选的单选按钮ID。