Java 单击按钮时使用ToggleButton将数据发送到另一个活动

Java 单击按钮时使用ToggleButton将数据发送到另一个活动,java,android,android-layout,Java,Android,Android Layout,我正在做一个项目,我想要一组切换按钮将数据发送到android中的另一个活动 我希望切换按钮根据单击的活动向另一个活动发送数据。以下是xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_start2" android:layout_widt

我正在做一个项目,我想要一组切换按钮将数据发送到android中的另一个活动

我希望切换按钮根据单击的活动向另一个活动发送数据。以下是xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_start2"
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"
tools:context="com.brillica.vapid.Start2">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Example"
    android:textSize="20sp"
    android:textColor="#000"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="19dp"
    android:id="@+id/question" />

<ToggleButton
    android:textOff="Sports"
    android:textOn="Sports"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="88dp"
    android:id="@+id/sports"
    android:layout_below="@+id/question"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#fff"
    android:background="#DDA388"/>

<ToggleButton
    android:textOff="Science"
    android:textOn="Science"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/science"
    android:textColor="#fff"
    android:background="#DDA388"
    android:layout_alignBaseline="@+id/sports"
    android:layout_alignBottom="@+id/sports"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ToggleButton
    android:textOff="Art"
    android:textOn="Art"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/art"
    android:textColor="#fff"
    android:background="#DDA388"
    android:layout_alignBaseline="@+id/science"
    android:layout_alignBottom="@+id/science"
    android:layout_centerHorizontal="true" />

<ToggleButton
    android:textOff="Entertainment"
    android:textOn="Entertainment"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:id="@+id/entertainment"
    android:textColor="#fff"
    android:background="#DDA388"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ToggleButton
    android:textOff="Crafting"
    android:textOn="Crafting"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:id="@+id/reading"
    android:textColor="#fff"
    android:background="#DDA388"
    android:layout_alignBaseline="@+id/entertainment"
    android:layout_alignBottom="@+id/entertainment"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<ToggleButton
    android:textOff="Farming"
    android:textOn="Farming"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/farming"
    android:textColor="#fff"
    android:background="#DDA388"
    android:layout_alignBaseline="@+id/reading"
    android:layout_alignBottom="@+id/reading"
    android:layout_alignLeft="@+id/art"
    android:layout_alignStart="@+id/art" />


<Button
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="65dp"
    android:id="@+id/next"
    android:background="#DDA388"
    android:text="Continue"
    android:textColor="#fff"
    android:onClick="next"
    android:layout_below="@+id/entertainment"
    android:layout_centerHorizontal="true" />

例如,如果单击了togglebutton science和togglebutton art,我希望在单击“下一步按钮”后,另一个活动中的文本更改为“艺术和科学”;如果单独单击艺术,我希望单击“下一步按钮”后,TextView显示艺术

我是编程新手,所以我对代码没有什么经验


有什么想法吗

您应该有目的地调用新活动

您可以在活动之间传递数据的意图中添加额外内容

mIntent.putExtra("sciencebutton", scbutton.isChecked());
您可以通过执行以下操作以编程方式获取按钮:

ToggleButton scbutton = findViewById(R.id.science);
在第二项活动中:

boolean isScienceButtonChecked = getIntent().getBooleanExtra("sciencebutton");

将返回您在第一个活动中传递的值(在本例中,无论是否选中了“科学”按钮)

您应该有目的地调用新活动

您可以在活动之间传递数据的意图中添加额外内容

mIntent.putExtra("sciencebutton", scbutton.isChecked());
您可以通过执行以下操作以编程方式获取按钮:

ToggleButton scbutton = findViewById(R.id.science);
在第二项活动中:

boolean isScienceButtonChecked = getIntent().getBooleanExtra("sciencebutton");

将返回您在第一个活动中传递的值(在本例中,无论是否选中“科学”按钮)

这是有意义的,但您能否给出一个在textView中访问它的实例?不确定“在textView中访问它”是什么意思。您的意思是在活动之间发送textView中的内容吗?或者,您的意思是根据在第一个活动中选中的按钮在第二个活动中填充textView?我希望textView指示已选中toggleButton,例如,如果在第二个活动中选中了scbtn.isChecked{tv.setText(“Hello”)},您已经有了正确的想法。只需使用从getIntent()返回的布尔值<代码>如果(IsScienceButtonched){tv.setText(“Hello”)}你的答案很有道理,但我不知道一旦意图被传递,如何将TextView设置为science。这很有道理,但你能给出一个在TextView中访问它的实例吗?不确定“在TextView中访问它”是什么意思。您的意思是在活动之间发送textView中的内容吗?或者,您的意思是根据在第一个活动中选中的按钮在第二个活动中填充textView?我希望textView指示已选中toggleButton,例如,如果在第二个活动中选中了scbtn.isChecked{tv.setText(“Hello”)},您已经有了正确的想法。只需使用从getIntent()返回的布尔值
if(issciencebuttonched){tv.setText(“Hello”)}
你的答案很有道理,但我不知道一旦意图通过,如何将TextView设置为science。