您的位置:首页 > 其它

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

2016-05-24 14:51 351 查看
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"/>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: