实验 logincheck 菜鸟级别的我

来源:互联网 发布:淘宝文案风格好的推荐 编辑:程序博客网 时间:2024/06/03 09:35
今天写一个实验,是之前做过的一个,并没有登陆之后的,现在加入了登陆之后的一些,算是比较完善了吧。


sharedpreferences是andriod平台上一个轻量级的存储类,主要用于存储一些应用程序的配置参数,例如用户名、密码、自定义参数的设置等。sharedPreferences中存储的数据是以key/value键值对的形式保存在XML文件中,该文件位于data/data/(packagename)/shared-prefs文件夹中。需要主要的是,sharedPreferences中的value值只能是float、int、long、boolean、String、StringSet类型数据。
使用sharedPreferences类存储数据时,首先需要通过context。getsharedPreferences(String name,int mode)获取sharedPreferences的实例对象。

MODE-PRIVATE:指定该sharedPreferences中的数据只能被本应用程序读写。
MODE-APPEND:该文件的内容可以追加。
MODE-WORLD-READABLE:指定该sharedPreferences中的数据可以被其他应用程序读。
MODE-WORLD-WRITEABLE:指定sharedPreferences中的数据可以被其他应用程序读写。
sharedPreferences对象本身只能获取数据,并不指定数据的存储和修改。数据的存储和修改需要通过sharedPreferences.Editor()对象实现,要想获取Editor实例对象,需要调用sharedPreferences.Editor edit()方法。
使用sharedPreferences存储数据时,需要先获取sharedPreferences对象,通过该对象获取到Editor对象,然后通过Editor对象的相关方法存储数据。
sharedPreferences获取数据时比较简单,只需要创建sharedPreferences对象,然后使用该对象获取相应key的值即可。
sharedPreferences删除数据时与存储数据类似,同样需要先获取到Editor对象,然后通过该对象删除数据,最后提交。




LoginActivity.java

package cn.edu.bzu.case_login;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.ContextThemeWrapper;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etPassword;
    private CheckBox cbIsRememberPass;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        sharedPreferences=getSharedPreferences("rememberpassword", Context.MODE_PRIVATE);
        boolean isRemember=sharedPreferences.getBoolean("rememberpassword",false);
        if(isRemember){
            String name=sharedPreferences.getString("name","");
            String password=sharedPreferences.getString("password","");
            etName.setText(name);
            etPassword.setText(password);
            cbIsRememberPass.setChecked(true);
        }
    }
    public void initViews(){
        etName=(EditText) findViewById(R.id.etName);
        etPassword=(EditText) findViewById(R.id.etPassword);
        cbIsRememberPass=(CheckBox) findViewById(R.id.cbIsRememberPass);
    }
    public void login(View view){
        String name=etName.getText().toString();
        String password=etPassword.getText().toString();
        if("admin".equals(name)&&"123456".equals(password)){
            SharedPreferences.Editor editor=sharedPreferences.edit();
            if(cbIsRememberPass.isChecked()){
                editor.putBoolean("rememberpassword",true);
                editor.putString("name",name);
                editor.putString("password",password);
            }else {
                editor.clear();
            }
            editor.commit();
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();
        }
    }
}




activity_main.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_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.case_login.MainActivity">
    <TextView
        android:text="Welcome you"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="40sp"
        android:layout_marginTop="263dp"
        android:id="@+id/textView" />
</RelativeLayout>



MainActivity.java
package cn.edu.bzu.case_login;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}




activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/loginbg"
    tools:context="cn.edu.bzu.case_login.LoginActivity">
    <include layout="@layout/login_top"></include>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/deer"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="18dp"
        android:layout_marginEnd="18dp"
        android:layout_marginBottom="10dp"
        android:id="@+id/imageView" />
</RelativeLayout>




login_top.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/activity_horizontal_margin"
    android:background="@drawable/logintop_roundbag">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_user"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/etName">
        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etName"
        android:background="@android:drawable/edit_text"
        android:drawableLeft="@drawable/icon_pass"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="@string/etPassword"
        android:inputType="textPassword">
        <requestFocus />
    </EditText>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etPassword">
        <CheckBox
            android:text="记住密码"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/cbIsRememberPass"
            android:textSize="20sp"
            android:layout_weight="1" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_select"
            android:onClick="login"
            android:text="@string/btnLogin" />
    </LinearLayout>
</RelativeLayout>

此时,如果将程序退出了,再重新打开会发现账号和密码仍然显示在当前EditText中,说明信息存储在SharedPreferences中。


0 0