Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我正在尝试使用SharedReference保存单选按钮的状态_Android - Fatal编程技术网

Android 我正在尝试使用SharedReference保存单选按钮的状态

Android 我正在尝试使用SharedReference保存单选按钮的状态,android,Android,我正在尝试使用SharedReference保存单选按钮的状态,我可以为活动中的其他项目保存状态,但我不知道如何执行此操作,因为我是新手。我正在尝试使用此代码,并有意使用“打开登录”活动,在此活动中我可以输入用户名和密码,并验证其是否为真 Here is my code: package com.relativelayouttask; import android.annotation.SuppressLint; import android.app.Activity; import andr

我正在尝试使用SharedReference保存单选按钮的状态,我可以为活动中的其他项目保存状态,但我不知道如何执行此操作,因为我是新手。我正在尝试使用此代码,并有意使用“打开登录”活动,在此活动中我可以输入用户名和密码,并验证其是否为真

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

        }
    };

}

通过使用单选按钮对象上的isChecked方法保存布尔值,可以保存SharedPrefs中单选按钮的状态

SharedPreferences.putBoolean(RADIO_BUTTON, radioButton.isChecked());
然后,在验证时,可以执行类似于以下内容的检查:

if (SharedPreferences.getBoolean(RADIO_BUTTON) == radioButton.isChecked()) {
    setError(null);
} else {
    setError("Invalid");
}
这样保存吧

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);
布景是这样回来的

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);
注意,您可以只保存选中复选框的int-id,而不是保存布尔值。这取决于您。

此处的扩展信息-