首页 > 解决方案 > 图像灰色直到点击 - 改变颜色并记住点击的图像

问题描述

当我的用户第一次登陆主要活动时,我希望图块(图像视图)变灰且不可点击。只有第一个图块应该是彩色的并且可以点击。在用户访问第一个图块并返回到主要活动后,下一个图块将变为彩色且可点击,依此类推。

这个想法是在第一次使用该应用程序时引导用户完成序列。我猜这些信息需要存储在共享首选项中,因为我只希望第一次发生这种情况。随后,所有瓷砖都应该是彩色的并且可以点击。

有人可以帮我实现这一目标吗?目前我有一个 switch 语句,一个布尔和彩色/灰色版本的图像块。

 @Override
public void onClick(View v) {

    SharedPreferences.Editor introEditor = introPref.edit();

    switch (v.getId()) {

        case R.id.cbt_button_grey:
            Intent mainIntent = new Intent(getActivity(), IntroActivity2.class);
            startActivity(mainIntent);
            hasSelected = true;
            introEditor.putBoolean("hasSelected", hasSelected);
            introEditor.commit();
            break;
        case R.id.twisted_thinking_button_grey:
            hasSelected = true;
            Intent distortedIntent = new Intent(getActivity(), TwelveTypesDistortedThinkingSliderActivity.class);
            startActivity(distortedIntent);
            break;
        case R.id.workout_button_grey:
            hasSelected = true;
            Fragment workoutFragment = new WorkoutFragment();
            FragmentTransaction workoutTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            workoutTransaction.replace(R.id.fragment_container, workoutFragment);
            workoutTransaction.commit();
            break;
        case R.id.workout_log_button_grey:
            hasSelected = true;
            Fragment workoutLogFragment = new WorkoutLogFragment();
            FragmentTransaction workoutLogTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            workoutLogTransaction.replace(R.id.fragment_container, workoutLogFragment);
            workoutLogTransaction.commit();
            break;
        case R.id.mood_log_button:
            hasSelected = true;
            Fragment moodLogFragment = new MoodLogFragment();
            FragmentTransaction moodLogTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            moodLogTransaction.replace(R.id.fragment_container, moodLogFragment);
            moodLogTransaction.commit();
            break;
        case R.id.activities_button_grey:
            hasSelected = true;
            Fragment activitiesFragment = new ActivitiesFragment();
            FragmentTransaction activitiesTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            activitiesTransaction.replace(R.id.fragment_container, activitiesFragment);
            activitiesTransaction.commit();
            break;
    }
}

标签: javaandroid

解决方案


在 MainActivity 类中使​​用 startActivityForResult() 怎么样。您可以覆盖 onActivityResult() 以启用/禁用图像。这只是一个建议。


推荐阅读