【问题标题】:How to change the layout on button click based on the radio button?如何根据单选按钮更改按钮单击的布局?
【发布时间】:2016-02-28 10:17:46
【问题描述】:

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



    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>

【问题讨论】:

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


    【解决方案1】:

    你必须改变点击的代码是这样的:-

    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,然后必须执行相应的操作。

    【讨论】:

    • 感谢您的回复。我复制了相同的代码。但仍然无法更改布局。抱歉。
    • 我没有看到任何错误..甚至我的日志语句也没有打印...你能建议如何检查错误..对不起,我对 android 很陌生,这是我的第一个应用程序
    • 您可以编辑问题并发布您的 xml 吗?这将有助于识别问题
    • int selectedId = radioGroup.getCheckedRadioButtonId(); if (selectedId==R.id.radioButton){ Intent intentMain = new Intent(Join_AS_A.this,Student_Signup.class); Join_AS_A.this.startActivity(intentMain); Log.i("内容", "主布局"); }
    • 请按此更改onclick方法代码。它会起作用的。
    猜你喜欢
    • 2021-04-02
    • 2017-05-08
    • 2012-03-17
    • 2023-04-03
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多