我有4个单选按钮,如下所示,我想在任何4个选项中只选择1个,也有一次用户无法返回更改他填充的选项.......当app打开用户应该被引导到他选择的活动。我该怎么做? 谢谢。
RadioButton FourthGrade = (RadioButton) findViewById(R.id.grade_4th);
FourthGrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CourseSelectionActivity.this, Home.class);
startActivity(intent);
}
});
答案 0 :(得分:1)
您可以创建一个“导航器”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
。
这只是一个非常基本的例子,可以根据您的需要定制。