简单的switch
语句,最后一个按钮为下一个按钮。我想要它,因此用户必须在继续之前在switch语句中选择一个图像。我不知道如何写一些东西来阻止他们,用户只需单击next按钮并转到下一个活动,而无需从switch语句中进行选择。下面的代码。
public void onClick(View v) {
SharedPreferences.Editor editorWorkout = workoutPref.edit();
// get the constraint layout from its ID.
ConstraintLayout mConstraintLayout = getView().findViewById(R.id.FragmentWorkoutMood1);
switch (v.getId()) {
case R.id.excitedFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_excited);
editorWorkout.putInt("excitedkey", EXCITED.getId());
editorWorkout.commit();
break;
case R.id.happyFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_happy);
editorWorkout.putInt("happykey", HAPPY.getId());
editorWorkout.commit();
break;
case R.id.fineFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_fine);
editorWorkout.putInt("finekey", FINE.getId());
editorWorkout.commit();
break;
case R.id.nextBtnMoodPage:
Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class);
startActivity(intent);
}
答案 0 :(得分:0)
您需要禁用nextBtnMoodPage
所执行的操作,直到用户选择了其他任何选项。使用简单的布尔值执行此操作。见下文:
// Inside the class itself
private boolean hasSelected = false;
public void onClick(View v) {
SharedPreferences.Editor editorWorkout = workoutPref.edit();
// get the constraint layout from its ID.
ConstraintLayout mConstraintLayout = getView().findViewById(R.id.FragmentWorkoutMood1);
switch (v.getId()) {
case R.id.excitedFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_excited);
editorWorkout.putInt("excitedkey", EXCITED.getId());
editorWorkout.commit();
hasSelected = true;
break;
case R.id.happyFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_happy);
editorWorkout.putInt("happykey", HAPPY.getId());
editorWorkout.commit();
hasSelected = true;
break;
case R.id.fineFace:
mConstraintLayout.setBackgroundResource(R.mipmap.background_clouds_fine);
editorWorkout.putInt("finekey", FINE.getId());
editorWorkout.commit();
hasSelected = true;
break;
case R.id.nextBtnMoodPage:
if(hasSelected){
Intent intent = new Intent(getActivity(), WorkoutAutomaticThoughtActivity.class);
startActivity(intent);
}
}
}
简而言之,只要用户选择了一个选项,您要做的就是翻转一个标志。如果该标记未翻转,请对下一个按钮进行操作,不执行任何操作。
如果该按钮不执行任何操作,则将其变灰或将其显示为禁用也是一个好主意,但是由于此问题不在我的范围内,因此我将不解释如何执行该操作。
祝你好运!
答案 1 :(得分:0)
您可以从switch语句外部开始活动 只是写
按钮按钮下一步。 = findViewById ..
buttonNext.SetOnClickListener ...