atultiwari

SettingsActivity.java

Jul 16th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. // ---- SettingsActivity.java ----//
  2. package com.atultiwari.medicalquiz;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.Button;
  13. import android.widget.RadioButton;
  14.  
  15. /**
  16.  * @author robert.hinds
  17.  *
  18.  */
  19. public class SettingsActivity extends Activity implements OnClickListener{
  20.  
  21.  
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.settings);
  26.        
  27.         /**
  28.          * set listener on update button
  29.          */
  30.         Button updateBtn = (Button) findViewById(R.id.nextBtn);
  31.         updateBtn.setOnClickListener(this);
  32.        
  33.         /**
  34.          * Set selected button if saved
  35.          */
  36.         updateButtonWithPreferences();
  37.        
  38.     }
  39.  
  40.  
  41.     /**
  42.      * Method to update default check box
  43.      */
  44.     private void updateButtonWithPreferences() {
  45.         RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
  46.         RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
  47.         RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);
  48.        
  49.         SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  50.         int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
  51.        
  52.         switch (diff)
  53.         {
  54.         case Constants.EASY :
  55.             c1.toggle();
  56.             break;
  57.            
  58.         case Constants.MEDIUM :
  59.             c2.toggle();
  60.             break;
  61.            
  62.         case Constants.EXTREME :
  63.             c3.toggle();
  64.             break; 
  65.         }
  66.     }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.     @Override
  73.     public void onClick(View arg0) {
  74.         /**
  75.          * check which settings set and return to menu
  76.          */
  77.         if (!checkSelected())
  78.         {
  79.             return;
  80.         }
  81.         else
  82.         {
  83.             SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
  84.             Editor e = settings.edit();
  85.             e.putInt(Constants.DIFFICULTY, getSelectedSetting());
  86.             e.commit();
  87.             finish();
  88.         }
  89.        
  90.     }
  91.  
  92.  
  93.     /**
  94.      * Method to check that a checkbox is selected
  95.      *
  96.      * @return boolean
  97.      */
  98.     private boolean checkSelected() {
  99.         RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
  100.         RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
  101.         RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);
  102.         return (c1.isChecked() || c2.isChecked() || c3.isChecked());
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Get the selected setting
  108.      */
  109.     private int getSelectedSetting() {
  110.         RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
  111.         RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
  112.         if (c1.isChecked())
  113.         {
  114.             return Constants.EASY;
  115.         }
  116.         if (c2.isChecked())
  117.         {
  118.             return Constants.MEDIUM;
  119.         }
  120.        
  121.         return Constants.EXTREME;
  122.     }
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment