Android对数据进行存储和读取

本文介绍了一个简单的Android应用程序,该程序使用SharedPreferences来存储和读取用户的登录信息,包括用户名和密码。通过两个按钮实现数据的写入和读取功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android对数据进行存储和读取

1127006-20170509230922051-547888603.png1127006-20170509230929816-437432892.png

activity_main.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="com.example.conserve.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:id="@+id/name"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:digits="0123456789"
        android:id="@+id/password"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="写入"
        android:id="@+id/wright"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="读取"
            android:id="@+id/read"/>
</LinearLayout>
</LinearLayout>

MainActivity:

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText Name;
    private EditText password;
    private Button wright;
    private Button read;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name=(EditText)findViewById(R.id.name);
        password=(EditText)findViewById(R.id.password);
        wright=(Button)findViewById(R.id.wright);
        read=(Button)findViewById(R.id.read);
        //获得对象preferences,创建文件及其属性(这里设置为可追加的属性)
        preferences=getSharedPreferences("Save",MODE_APPEND);
        //编辑器
        editor=preferences.edit();
        //分别对两个按钮进行监听
        wright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wright.isClickable()) {
                //从两个输入框内提取相应的数据
                    editor.putString("Name",Name.getText().toString());
                    editor.putString("password",password.getText().toString());
                    editor.commit();
                    editor.clear();
                    Toast.makeText(MainActivity.this,"已保存",Toast.LENGTH_SHORT).show();
                }
            }
        });
        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (read.isClickable()) {
                //提取文件内的数据
                    SharedPreferences data = getSharedPreferences("Save",MODE_APPEND);
                    String name = data.getString("Name", "");
                    String password = data.getString("password", "");
                    Toast.makeText(MainActivity.this,"姓名为"+name+"密码为"+password,Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

转载于:https://www.cnblogs.com/lmy0323/p/6833263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值