簡體   English   中英

如何在switch語句中實現驗證?

[英]How to implement validation onto a switch statement?

簡單的switch語句,下一個按鈕作為最后一個案例。 我想要它,所以用戶必須在繼續之前在switch語句中選擇一個圖像。 我不知道如何編寫一些內容來阻止用戶只需單擊下一個按鈕並轉到下一個活動而無需從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);
    }

您需要禁用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);
            }
    }
}

簡而言之,您需要做的就是在用戶選擇了一個選項后翻轉標記。 如果未翻轉該標志,請對下一個按鈕執行操作。

如果沒有動作,將按鈕變灰或以其他方式顯示為禁用也是一個好主意,但我不會解釋如何執行此操作,因為它超出了此問題的范圍。

祝好運!

你可以從switch語句外部啟動actvity只是寫

按鈕按鈕下一步。 = findViewById ..

buttonNext.SetOnClickListener ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM