• sp存储和读取,保存登录信息练习


    package com.hanqi.testapp111;
    
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class denglu extends AppCompatActivity {
        EditText et_1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_denglu);
            et_1=(EditText)findViewById(R.id.et_1);
            SharedPreferences sp=getSharedPreferences("a",MODE_PRIVATE);
            String str =sp.getString("a",null) ;
            et_1.setText(str);
    
        }
        public void bt_OnClick(View v)
        {
            String s1=et_1.getText().toString();
            SharedPreferences sharedPreferences=getSharedPreferences("baocun",MODE_APPEND);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putString("a",s1);
            editor.commit();
            Toast.makeText(denglu.this, "保存数据成功", Toast.LENGTH_SHORT).show();
    
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        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="com.hanqi.testapp111.denglu"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="请输入用户名"/>
        <EditText
                android:layout_width="match_parent"
               android:layout_height="wrap_content"
                android:textSize="20dp"
                android:id="@+id/et_1"/>
        </LinearLayout>
               <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="登录"
                    android:onClick="bt_OnClick"/>

  • 相关阅读:
    【洛谷 1536】村村通
    【洛谷 1551】亲戚
    【UOJ 300】感冒病毒
    【洛谷 2299】Mzc和体委的争夺战
    【洛谷 3371】模板_单源最短路径(弱化版)
    【洛谷 2910】寻宝之路
    【洛谷 1359】租用游艇
    【UOJ 275】最短路径问题
    【UOJ 38】 股票经济人通信网络
    【UOJ 276】无向图最小环
  • 原文地址:https://www.cnblogs.com/jiang2538406936/p/5523391.html
Copyright © 2020-2023  润新知