用sp保存实现记住密码

SharedPreferences的用法:

由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的,用法见下面代码:

Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例

name:是指文件名称,不需要加后缀.xml,系统会自动为我们添加上。

mode:是指定读写方式,其值有三种,分别为:
Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写

Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写

Context.MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序读写。

1.Mainactivity.class

package com.mr.tengyu.spdemo;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar tool;
    private EditText usename;
    private EditText psw;
    private CheckBox cb;
    private Button bt;

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

        initView ( );
    }

    private void initView() {
        tool = (Toolbar) findViewById ( R.id.tool );
        setSupportActionBar ( tool );
        tool.setTitle ( "登录" );
        usename = (EditText) findViewById ( R.id.usename );
        psw = (EditText) findViewById ( R.id.psw );
        cb = (CheckBox) findViewById ( R.id.cb );
        bt = (Button) findViewById ( R.id.bt );
        SharedPreferences sp = this.getSharedPreferences ( "use", MODE_PRIVATE );
        if (sp.getBoolean ( "ISCHECK", false )  ){
            String name = sp.getString ( "name", "" );
            String pswa = sp.getString ( "psw", "" );
            usename.setText ( name );
            psw.setText ( pswa );

            Intent intent = new Intent ( this, WebActivity.class );
            startActivity ( intent );
        }
        bt.setOnClickListener ( this );

    }

    private void submit() {
        // validate
        String usenameString = usename.getText ( ).toString ( ).trim ( );
        if (TextUtils.isEmpty ( usenameString )) {
            Toast.makeText ( this, "  账号", Toast.LENGTH_SHORT ).show ( );
            return;
        }

        String pswString = psw.getText ( ).toString ( ).trim ( );
        if (TextUtils.isEmpty ( pswString )) {
            Toast.makeText ( this, "  密码", Toast.LENGTH_SHORT ).show ( );
            return;
        }
        SharedPreferences sp = getSharedPreferences ( "use", MODE_PRIVATE );
        SharedPreferences.Editor edit = sp.edit ( );
        if (cb.isChecked ()){
        edit.putString ( "name",usenameString );
        edit.putString ( "psw" ,pswString);
        edit.putBoolean ( "ISCHECK",true );
        edit.commit ();
        }
        // TODO validate success, do something


    }

    @Override
    public void onClick(View v) {
        switch (v.getId ( )) {
            case R.id.bt:
                submit();
                Intent intent = new Intent ( this, WebActivity.class );
                startActivity ( intent );
                Toast.makeText ( this, "恭喜您登陆成功", Toast.LENGTH_SHORT ).show ( );
                break;
        }
    }
}

2.layout_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:id="@+id/tool"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_margin="15dp"
        android:layout_height="match_parent"
        >
        <EditText
            android:layout_marginTop="80dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="  账号"
            android:background="@drawable/yuanjiao"
            android:id="@+id/usename"
            />
        <EditText
            android:layout_below="@id/usename"
            android:layout_marginTop="40dp"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="  密码"
            android:id="@+id/psw"
            android:background="@drawable/yuanjiao"
            />

        <CheckBox
            android:id="@+id/cb"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/psw"
            android:layout_marginTop="0dp"
            android:text="记住密码" />
        <LinearLayout
            android:layout_below="@id/cb"
            android:layout_width="match_parent"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content">
        <Button
            android:id="@+id/bt"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginTop="37dp"
            android:background="@drawable/yuanjiao"
            android:hint="登录" />

        </LinearLayout>
    </RelativeLayout>

</LinearLayout>


2.Webactivity.class

package com.mr.tengyu.spdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class WebActivity extends AppCompatActivity {

    private WebView web;

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

    }

    private void initView() {
        web = (WebView) findViewById ( R.id.web );
        web.getSettings().setJavaScriptEnabled(true);
        web.setWebChromeClient(new WebChromeClient ());
        web.loadUrl("http://www.aa2626.com/");
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".WebActivity">
    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/web"
        />

</LinearLayout>

5.yuanjiao.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"

    >
    <corners android:radius="2dp"/>
    <stroke android:color="@color/colorAccent" android:width="1dp"/>

</shape>

6.清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mr.tengyu.spdemo">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/NoTheme">
        <meta-data
            android:name="com.google.android.actions"
            android:resource="@xml/network_security_config" />

        <activity android:name=".WebActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值