如何使用共享首选项复选框 - Android Studio

时间:2017-06-08 10:40:40

标签: android

我创建了一个连接到mysql数据库的登录页面。 在activity_user_login.xml中,我输入了2个editText用于电子邮件和密码以及登录Botton,我为已保存的数据添加了一个复选框。 我想保存电子邮件和密码 但是,我无法添加sharedPreference。 下面的完整代码..你可以帮帮我吗?谢谢

activity_user_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_user_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.UserLoginActivity"
    android:background="#FF5722"
    android:padding="20dp">

    <TextView
        android:text="User Login Form"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:gravity="center"
        android:textSize="20dp"
        android:textColor="#fff"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#fbfefd"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/email"
        android:hint="Enter Your Email Here"
        android:gravity="center"
        />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#fbfefd"
        android:inputType="textPassword"
        android:ems="10"
        android:layout_below="@+id/email"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/password"
        android:hint="Enter Your Password Here"
        android:gravity="center"/>

    <Button
        android:text="Log In Here"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/Login" />

    <CheckBox
        android:id="@+id/saveLoginCheckBox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editTextPassword"
        android:text="Save Login?"
        android:textColor="#FFF" />

</RelativeLayout>  

UserLoginActivity.java

package com.myapp.myappcode;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.HashMap;


public class UserLoginActivity extends AppCompatActivity {

    EditText Email, Password;
    Button LogIn ;
    String PasswordHolder, EmailHolder;
    String finalResult ;
    String HttpURL = "http://mysite/User/UserLogin.php";
    Boolean CheckEditText ;
    ProgressDialog progressDialog;
    HashMap<String,String> hashMap = new HashMap<>();
    HttpParse httpParse = new HttpParse();
    public static final String UserEmail = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);

        Email = (EditText)findViewById(R.id.email);
        Password = (EditText)findViewById(R.id.password);
        LogIn = (Button)findViewById(R.id.Login);

        LogIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                CheckEditTextIsEmptyOrNot();

                if(CheckEditText){

                    UserLoginFunction(EmailHolder, PasswordHolder);

                }
                else {

                    Toast.makeText(UserLoginActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

                }

            }
        });
    }
    public void CheckEditTextIsEmptyOrNot(){

        EmailHolder = Email.getText().toString();
        PasswordHolder = Password.getText().toString();

        if(TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))
        {
            CheckEditText = false;
        }
        else {

            CheckEditText = true ;
        }
    }

    public void UserLoginFunction(final String email, final String password){

        class UserLoginClass extends AsyncTask<String,Void,String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                progressDialog = ProgressDialog.show(UserLoginActivity.this,"Loading Data",null,true,true);
            }

            @Override
            protected void onPostExecute(String httpResponseMsg) {

                super.onPostExecute(httpResponseMsg);

                progressDialog.dismiss();

                if(httpResponseMsg.equalsIgnoreCase("Data Matched")){

                    finish();

                    Intent intent = new Intent(UserLoginActivity.this, DashboardActivity.class);

                    intent.putExtra(UserEmail,email);

                    startActivity(intent);

                }
                else{

                    Toast.makeText(UserLoginActivity.this,httpResponseMsg,Toast.LENGTH_LONG).show();
                }

            }

            @Override
            protected String doInBackground(String... params) {

                hashMap.put("email",params[0]);

                hashMap.put("password",params[1]);

                finalResult = httpParse.postRequest(hashMap, HttpURL);

                return finalResult;
            }
        }

        UserLoginClass userLoginClass = new UserLoginClass();

        userLoginClass.execute(email,password);
    }

}

2 个答案:

答案 0 :(得分:0)

  sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();

// get e mail and pass on button click and save it like this
            editor.putString(pass, ps);
            editor.putString(Email, e);
            editor.commit();

并将其取回:

String n  = ed1.getText().toString();
            String pass  = ed2.getText().toString();
            String e  = ed3.getText().toString();

这是示例link.

答案 1 :(得分:0)

选中

复选框,然后将值放入共享首选项

将值设置为“共享首选项”

SharedPreferences pref=getSharedPreferences("values",Context.MODE_PRIVATE);
pref.edit().putString("email", Email.getText().toString());
pref.edit().putString("pass", Password.getText().toString());
pref.apply();

从共享首选项中获取值

SharedPreferences pref=getSharedPreferences("values", Context.MODE_PRIVATE);
String useremail=pref.getString("email","def_val");
String password=pref.getString("pass","def_val");

def_val替换为空或任何