2015-02-04 48 views
0

我正嘗試使用sharedpreference保存單選按鈕的狀態,我能夠爲活動中的其他項保存狀態,但我不知道該怎麼辦這是因爲我對此很陌生。我試圖使用這個代碼,並意圖使用打開登錄活動,我可以從這個活動輸入用戶名和密碼,並驗證它是否爲真。我正在嘗試使用sharedpreference保存單選按鈕的狀態

Here is my code: 
package com.relativelayouttask; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.Toast; 

public class SignUpActivity extends Activity { 
    EditText EtFirstName, EtLastName, EtEmailAddress, EtEnter_Password, 
      EtConfirm_Password, EtDOB; 
    Button BtnSubmit; 
    RadioButton RbMale, RbFemale; 
    RadioGroup RgGender; 

    private int year; 
    private int month; 
    private int day; 
    public static final String MyPREFERENCES = "MyPrefs"; 
    public static final String FIRSTNAME = "firstnamekey"; 
    public static final String LASTNAME = "lastnamekey"; 
    public static final String EMAIL = "emailkey"; 
    public static final String ENTER_PASSWORD = "enterpasswordkey"; 
    public static final String CONFIRM_PASSWORD = "confirmpasswordkey"; 
    public static final String DOB = "dobkey"; 
    public static final String GENDER = "genderkey"; 

    SharedPreferences sharedpreferences; 

    static final int DATE_DIALOG_ID = 999; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signup_layout); 
     EtFirstName = (EditText) findViewById(R.id.First_Name_Et); 
     EtLastName = (EditText) findViewById(R.id.Last_Name_Et); 
     EtEmailAddress = (EditText) findViewById(R.id.User_Name_Et); 
     EtEnter_Password = (EditText) findViewById(R.id.Password_Et); 
     EtConfirm_Password = (EditText) findViewById(R.id.Confirm_Pass_Et); 
     RgGender = (RadioGroup) findViewById(R.id.rgroup_Gender); 
     RgGender.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      public void onCheckedChanged(RadioGroup group, int checkedId) { 

       RbMale = (RadioButton) findViewById(R.id.Male_Rbtn); 
       if (RbMale.isChecked()) { 
        Toast.makeText(getBaseContext(), "You Selected Male", 
          Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(getBaseContext(), "You Selected Female", 
          Toast.LENGTH_LONG).show(); 

       } 

      } 
     }); 

     EtDOB = (EditText) findViewById(R.id.Birth_Day_Et); 
     BtnSubmit = (Button) findViewById(R.id.Submit_Btn); 
     RbMale = (RadioButton) findViewById(R.id.Male_Rbtn); 
     RbFemale = (RadioButton) findViewById(R.id.Female_Rbtn); 

     sharedpreferences = getSharedPreferences(MyPREFERENCES, 
       Context.MODE_PRIVATE); 

     EtFirstName.setText(sharedpreferences.getString(FIRSTNAME, "")); 
     EtLastName.setText(sharedpreferences.getString(LASTNAME, "")); 

     if (sharedpreferences.contains(EMAIL)) { 
      EtEmailAddress.setText(sharedpreferences.getString(EMAIL, "")); 

     } 
     if (sharedpreferences.contains(ENTER_PASSWORD)) { 
      EtEnter_Password.setText(sharedpreferences.getString(
        ENTER_PASSWORD, "")); 

     } 
     if (sharedpreferences.contains(CONFIRM_PASSWORD)) { 
      EtConfirm_Password.setText(sharedpreferences.getString(
        CONFIRM_PASSWORD, "")); 

     } 
     if (sharedpreferences.contains(DOB)) { 
      EtDOB.setText(sharedpreferences.getString(DOB, "")); 

     } 
     run(); 
    } 

    private void run() { 
     BtnSubmit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.e("Submit", "Status"); 
       SubmitClick(); 
      } 

      @SuppressLint("NewApi") 
      public void SubmitClick() { 
       String firstName = EtFirstName.getText().toString(); 
       if (firstName.length() <= 0) { 
        EtFirstName.setError("Invalid"); 

       } else 
        EtFirstName.setError(null); 

       String lastName = EtLastName.getText().toString().trim(); 
       if (lastName.length() < 1) { 
        EtLastName.setError("Invalid"); 

       } else { 
        EtLastName.setError(null); 
       } 

       String dateofbirth = EtDOB.getText().toString().trim(); 
       if (dateofbirth.isEmpty()) { 
        EtDOB.setError("Please select your date of Birth"); 

       } else { 
        EtDOB.setError(null); 
       } 

       String email = EtEmailAddress.getText().toString().trim(); 
       if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email) 
         .matches()) { 
        EtEmailAddress.setError("Invalid"); 

       } else { 
        EtEmailAddress.setError(null); 
       } 
       String password = EtEnter_Password.getText().toString().trim(); 
       if (password.length() < 8) { 
        EtEnter_Password.setError("Invalid"); 

       } else { 
        EtEnter_Password.setError(null); 
       } 

       String confirmPwd = EtConfirm_Password.getText().toString() 
         .trim(); 
       if (password.length() < 8 || !confirmPwd.equals(password)) { 
        EtConfirm_Password.setError("Invalid"); 

       } else { 
        EtConfirm_Password.setError(null); 
       } 

       /* 
       * String country = TVcountry.getText().toString().trim(); if 
       * (country.isEmpty()) { 
       * TVcountry.setError("Please select your country"); 
       * 
       * } else { TVcountry.setError(null); } 
       */ 

       String fn = EtFirstName.getText().toString(); 
       String ln = EtLastName.getText().toString(); 

       String em = EtEmailAddress.getText().toString(); 
       String ep = EtEnter_Password.getText().toString(); 
       String cp = EtConfirm_Password.getText().toString(); 
       String d = EtDOB.getText().toString(); 
       Editor editor = sharedpreferences.edit(); 
       editor.putString(FIRSTNAME, fn); 
       editor.putString(LASTNAME, ln); 
       editor.putString(EMAIL, em); 
       editor.putString(ENTER_PASSWORD, ep); 
       editor.putString(CONFIRM_PASSWORD, cp); 
       editor.putString(DOB, d); 

       editor.commit(); 

       /* 
       * Intent intent = new Intent(SignUpActivity.this, 
       * LoginActivity.class); 
       * 
       * startActivity(intent); 
       */ 

      } 
     }); 

     EtDOB.setOnClickListener(new OnClickListener() { 

      @SuppressWarnings("deprecation") 
      @Override 
      public void onClick(View v) { 
       showDialog(DATE_DIALOG_ID); 

      } 
     }); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      // set date picker as current date 
      return new DatePickerDialog(this, datePickerListener, year, month, 
        day); 
     } 
     return null; 
    } 

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 

     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear, 
       int selectedMonth, int selectedDay) { 
      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 

      // set selected date into textview 
      EtDOB.setText(new StringBuilder().append(month + 1).append("-") 
        .append(day).append("-").append(year).append(" ")); 

      // set selected date into datepicker also 

     } 
    }; 

} 
+0

此處擴展信息 - http://stackoverflow.com/a/151940/2074990 – bofredo

回答

0

您可以通過在單選按鈕對象上使用方法isChecked()保存布爾值來保存SharedPrefs中單選按鈕的狀態。

SharedPreferences.putBoolean(RADIO_BUTTON, radioButton.isChecked()); 

然後,當您要驗證它,你可以做一個類似的檢查:

if (SharedPreferences.getBoolean(RADIO_BUTTON) == radioButton.isChecked()) { 
    setError(null); 
} else { 
    setError("Invalid"); 
} 
0

保存它像這樣

Editor editor = sharedpreferences.edit(); 
int selectedId = radioSexGroup.getCheckedRadioButtonId(); // will give you the id of currently selected radio button in the radio group 
if(selectedId == 0) 
    editor.putBoolean("isMale", true); // assuming 0th radio button is for male 
else 
    editor.putBoolean("isMale", false); 

editor.commit(); 

集又回來了這樣

boolean isMale = sharedpreferences.getBoolean("isMale", true); 
if(isMale) 
    radioSexGroup.check(0); 
else 
    radioSexGroup.check(1); 

否te而不是保存布爾值,你可以簡單地保存int(所選複選框的ID)。這取決於你。