【问题标题】:How to choose starting Activity based on condition如何根据条件选择启动Activity
【发布时间】:2017-12-12 13:03:48
【问题描述】:

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

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

【问题讨论】:

  • 您可以在开始该活动时预选单选按钮。确保在返回此活动时已将此用户选择保存在数据库/共享首选项中。此外,在用户选择任何选项后,您可以禁用无线电组。
  • 使用RadioGroupsetOnCheckedChangeListener

标签: android radio-button sharedpreferences


【解决方案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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多