[英]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.