gpt4 book ai didi

android - 如何根据条件选择启动Activity

转载 作者:行者123 更新时间:2023-11-29 01:08:44 25 4
gpt4 key购买 nike

我有 4 个单选按钮,如下所示,我只想从任何 4 个选项中选择 1 个,并且在该用户无法返回更改他填写的选项后也有一次......并且当应用程序打开用户应该被引导到他选择的 Activity 。我该怎么做?谢谢。

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);
}
});

最佳答案

您可以创建一个“导航器”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;
}
}

这将根据用户之前的选择导航到四个 Activity 之一。如果用户尚未选择,它将导航到 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

这只是一个非常基本的示例,您可以根据自己的需要进行调整。

关于android - 如何根据条件选择启动Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44984200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com