• sharedPreferences界面登录信息存储


    制作一个登录界面,以SP方式存储用户名。用户下次登录时自动显示上次填写的用户名。
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.wang.xuexi.TestActivity5"
    11     android:orientation="vertical">
    12 
    13     <EditText
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:hint="请输入用户名"
    17         android:textSize="20dp"
    18         android:id="@+id/et_1"/>
    19     <EditText
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:hint="请输入密码"
    23         android:textSize="20dp"
    24         android:inputType="textPassword"
    25         android:id="@+id/et_2"/>
    26     <LinearLayout
    27         android:layout_width="match_parent"
    28         android:layout_height="wrap_content">
    29 
    30         <Button
    31             android:layout_width="0dp"
    32             android:layout_weight="1"
    33             android:layout_height="wrap_content"
    34             android:text="注册"/>
    35 
    36         <Button
    37             android:layout_width="0dp"
    38             android:layout_weight="1"
    39             android:layout_height="wrap_content"
    40             android:text="登录"
    41             android:onClick="bt_OnClick"/>
    42 
    43     </LinearLayout>
    44 </LinearLayout>
    .xml
     1 package com.example.wang.xuexi;
     2 
     3 import android.content.SharedPreferences;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.os.Bundle;
     6 import android.view.View;
     7 import android.widget.EditText;
     8 import android.widget.Toast;
     9 
    10 public class TestActivity5 extends AppCompatActivity {
    11 
    12     EditText et_1;
    13     EditText et_2;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_test5);
    19 
    20         et_1=(EditText)findViewById(R.id.et_1);
    21         et_2=(EditText)findViewById(R.id.et_2);
    22 
    23         SharedPreferences sp=getSharedPreferences("yhm", MODE_PRIVATE);
    24 
    25         String str=sp.getString("save",null);
    26 
    27         et_1.setText(str);
    28 
    29         String str1=sp.getString("save1",null);
    30 
    31         et_2.setText(str1);
    32     }
    33 
    34     public void bt_OnClick(View v)
    35     {
    36         EditText et_1=(EditText)findViewById(R.id.et_1);
    37         et_2=(EditText)findViewById(R.id.et_2);
    38 
    39         String string=et_1.getText().toString();
    40         String string1=et_2.getText().toString();
    41 
    42         SharedPreferences sharedPreferences=getSharedPreferences("yhm",MODE_PRIVATE);
    43 
    44         SharedPreferences.Editor editor=sharedPreferences.edit();
    45 
    46         editor.putString("save",string);
    47         editor.putString("save1",string1);
    48 
    49         editor.commit();
    50 
    51     }
    52 }
    .java

     
     
     
  • 相关阅读:
    Ubuntu20.04本地安装Redash中文版
    ubuntu设置root密码
    qmake设置生成文件分类
    QML对象的构造函数和析构函数
    QString使用split按照某字符进行分解
    Qt的qDebug直接打印不添加头文件
    C++宏定义中的#
    Qt设置生成的文件路径
    QWidget禁止最大化
    js-去掉回车和空格
  • 原文地址:https://www.cnblogs.com/arxk/p/5521988.html
Copyright © 2020-2023  润新知