我正在尝试使用共享首选项保存单选按钮的状态,我能够保存活动中其他项目的状态,但我不知道如何执行此操作,因为我是新手。我正在尝试使用此代码,并且意图使用打开登录活动,我可以从此活动输入用户名和密码,并验证它是否为真。
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 :(得分:0)
您可以使用单选按钮对象上的方法isChecked()保存布尔值,从而在SharedPrefs中保存单选按钮的状态。
SharedPreferences.putBoolean(RADIO_BUTTON, radioButton.isChecked());
然后,当您验证它时,您可以进行类似的检查:
if (SharedPreferences.getBoolean(RADIO_BUTTON) == radioButton.isChecked()) {
setError(null);
} else {
setError("Invalid");
}
答案 1 :(得分: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();
Set就像这样
boolean isMale = sharedpreferences.getBoolean("isMale", true);
if(isMale)
radioSexGroup.check(0);
else
radioSexGroup.check(1);
注意您可以直接保存int(所选复选框的ID),而不是保存布尔值。这取决于你。