您可以創建一個「導航」 Activity
爲切入點,以您的應用程序,這樣的事情:
public class NavigatorActivity extends AppCompatActivity {
public static final String KEY_CHOICE = "choice";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fetch the choice of the user, or return -1 if there is no choice yet
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
int choice = prefs.getInt(KEY_CHOICE, -1);
Intent intent = createIntentBasedOnChoice(this, choice);
startActivity(intent);
finish();
}
// this method returns an Intent based on the passed choice parameter
public static Intent createIntentBasedOnChoice(Context context, int choice) {
Intent intent;
switch (choice) {
case 1: {
intent = new Intent(context, FirstActivity.class);
break;
}
case 2: {
intent = new Intent(context, SecondActivity.class);
break;
}
case 3: {
intent = new Intent(context, ThirdActivity.class);
break;
}
case 4: {
intent = new Intent(context, FourthActivity.class);
break;
}
default: {
// if there is no choice yet, start the ChoiceActivity
intent = new Intent(context, ChoiceActivity.class);
break;
}
}
return intent;
}
}
這將導航到根據用戶的前面選擇的四個活動之一。如果用戶還沒有選擇,它將導航到ChoiceActivity
。
爲ChoiceActivity
的基本佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/group_choices"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/button_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Choice 1" />
<RadioButton
android:id="@+id/button_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choice 2" />
<RadioButton
android:id="@+id/button_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choice 3" />
<RadioButton
android:id="@+id/button_choice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choice 4" />
</RadioGroup>
<Button
android:id="@+id/button_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is my choice!" />
</LinearLayout>
而且它的代碼會是這個樣子:
public class ChoiceActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choice);
final RadioGroup choiceGroup = (RadioGroup) findViewById(
R.id.group_choices);
Button submitButton = (Button) findViewById(R.id.button_submit);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int choice;
switch (choiceGroup.getCheckedRadioButtonId()) {
case R.id.button_choice1: {
choice = 1;
break;
}
case R.id.button_choice2: {
choice = 2;
break;
}
case R.id.button_choice3: {
choice = 3;
break;
}
case R.id.button_choice4: {
choice = 4;
break;
}
default: {
choice = 1;
break;
}
}
// saving the choice of the user
PreferenceManager
.getDefaultSharedPreferences(ChoiceActivity.this)
.edit()
.putInt(NavigatorActivity.KEY_CHOICE, choice)
.apply();
Intent intent = NavigatorActivity
.createIntentBasedOnChoice(ChoiceActivity.this, choice);
startActivity(intent);
finish();
}
});
}
}
這將節省用戶的選擇在SharedPreferences
和向前導航到相應的Activity
。
這只是一個非常基本的例子,可以根據您的需求量身定製。
當您參加該活動時,您可以預先選擇所選的單選按鈕。確保在回到此活動時,您已將此用戶選擇保存在您的數據庫/共享首選項中。 此外,用戶選擇任何選項後,您可以禁用收音機組。 –
使用'RadioGroup'和'setOnCheckedChangeListener'。 –