2017-07-08 4 views
2

私は以下のような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); 
       } 
      }); 
+0

このアクティビティに参加するときは、選択したラジオボタンを事前に選択できます。あなたがこのアクティビティに戻ってきたら、このユーザをあなたのDB/Shared Prefに保存してください。 また、ユーザーが任意の選択肢を選択すると、ラジオグループを無効にすることができます。 –

+0

は 'RadioGroup'と' setOnCheckedChangeListener'を使います。 –

答えて

1

あなたは、あなたのアプリケーションへのエントリポイントとして、このような何か「ナビゲーター」Activityを作成することができます。これは、ユーザーの前の選択に応じて4つの活動の一つにナビゲートします

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を楽しみにナビゲートします。

これは、ご自分のニーズに合わせて調整していただくための非常に基本的な例です。

関連する問題