简体   繁体   English

如何根据第一活动数据(单击单选按钮)从第二活动转到第三活动或第四活动?

[英]How to go from second activity to third activity or fourth activity based on first activity data (Radio button clicked)?

I have four activities say java files - activity1.java, activity2.java, activity3.java, activity4.java and xml files - activity_1.xml, activity_2.xml, activity_3.xml, activity_4.xml 我有四个活动,例如Java文件-activity1.java,activity2.java,activity3.java,activity4.java和xml文件-activity_1.xml,activity_2.xml,activity_3.xml,activity_4.xml

Now in activity1 - I have a radio group with two radio buttons and a button to go to activity2 现在处于activity1中-我有一个带有两个单选按钮的单选按钮,还有一个转到activity2的按钮

Now in activity2 - I have a button to go to activity3 or activity4 based on which radio button is clicked in activity1. 现在在活动2中-我有一个按钮可以转到活动3或活动4,这取决于在活动1中单击了哪个单选按钮。

I can delete activity2 and use if condition in activity1 to go to activity3 or 4 But I definitely need activity2 我可以删除活动2并使用如果活动1中的条件转到活动3或4,但我绝对需要活动2

I am not familiar with bundles, shared preferences 我对捆绑包,共享的首选项不熟悉

How? 怎么样? Any help in this regard 这方面的任何帮助

You can just use an intent and pass on values between activities. 您可以只使用意图并在活动之间传递值。 In first activity: 在第一个活动中:

 Intent intent = new Intent(this, SecondActivity.class);
 intent.putExtra("someName", valueOfRadioButton);
 startActivity(intent);

In the onCreate of the next activity you can retrieve the value you passed like this (for an Integer): 在下一个活动的onCreate中,您可以检索通过以下方式传递的值(对于Integer):

 getIntent().getIntExtra("someName", someDefaultValue);

Now you have a variable to query. 现在,您有一个要查询的变量。 Depending on the value, you launch whatever activity you like. 根据值,您可以启动任何您喜欢的活动。

Use 采用

Intent startSecondActivityIntent = new Intent(FirstActivity.this,SecondActivity.java);
Intent.putExtra("nameOfValue",value);
startActivity(startSecondActivityIntent);

Then you can get this value: 然后,您可以获得以下值:

getIntent().getIntExtra("nameOfValue",defaultValue);

Or you can use SharedPreferences 或者您可以使用SharedPreferences

SharedPreferences sp = getSharedPreferences("nameOfPreferences",MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString("nameOfValue",value);
    editor.commit();

And get this value in other activity: 并在其他活动中获得此值:

SharedPreferences sp = getSharedPreferences("nameOfPreferences",MODE_PRIVATE);
String value = sp.getString("nameOfValue",defaultValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从第二个活动传递到第三个活动 - Passing data from second activity to third activity 将数据从第二个活动移到第一个活动 - Move Data from Second Activity to first Activity 单击第三个活动中的按钮时,第一个和第二个活动是否可能不会再次显示? - Is it possible that First and Second activity will not show again when clicked the button in third activity? 避免第三项活动回到第一项活动 - Avoid third activity go back to first activity 如何将第一个Activity中的数据保存到第二个Activity中? - How to save the data in my ListView from the first Activity to the second Activity? 将数据从第三个活动发送到第一个活动 - 正确的方法? - Sending data from Third Activity to First Activity - correct method? 如何使第二个活动中的按钮打开第三个活动? - How do you make a button from second activity open a third activity? 显示有关第二个活动中设置的第一个活动的数据 - Displaying data on first activity that is set in the second activity 将数据从第三个活动转移到第一个活动 - Transferring data from the third activity to the first 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM