安卓保存数据的三种方式--Rom、SD、sharepreference

下面我将讲一下初学者应该掌握的安卓保存数据的三种方式

要求:做一个登录的界面,用户可以输入账号和密码,有确定的按钮,有一个记住密码的勾选框,若用户在选择记住密码后,下次登录时账号密码仍然存在,不需要用户再次输入。界面如下
 
恩,布局十分的简单,我就不废话了,直接贴出代码,来看下activity_main.xml的实现
<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:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user" />

    <EditText
        android:id="@+id/ed_user"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你的账号"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password" />

    <EditText
        android:id="@+id/ed_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入你的密码"
        android:inputType="textPassword" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remember_password" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="@string/yes" />
    </RelativeLayout>

    <RadioGroup
        android:id="@+id/rd_findlocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rd_rom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/rom" />

        <RadioButton
            android:id="@+id/rd_sd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Android实现四种数据存储方式的介绍: 1. SharePreference存储数据 ```java // 获取SharePreference对象 SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE); // 获取Editor对象 SharedPreferences.Editor editor = sharedPreferences.edit(); // 存储数据 editor.putString("name", "Tom"); editor.putInt("age", 18); // 提交数据 editor.commit(); ``` 2. File存储数据 ```java // 获取FileOutputStream对象 FileOutputStream fos = openFileOutput("data.txt", MODE_PRIVATE); // 写入数据 fos.write("Hello World".getBytes());// 关闭流 fos.close(); ``` 3. SQLite数据库存储数据 ```java // 创建数据库 SQLiteDatabase db = openOrCreateDatabase("test.db", MODE_PRIVATE, null); // 创建表 db.execSQL("CREATE TABLE IF NOT EXISTS person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age INTEGER)"); // 插入数据 db.execSQL("INSERT INTO person (name, age) VALUES ('Tom', 18)"); // 查询数据 Cursor cursor = db.rawQuery("SELECT * FROM person", null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.d("TAG", "name: " + name + ", age: " + age); } // 关闭游标和数据库 cursor.close(); db.close(); ``` 4. ContentProvider存储数据 ```java // 创建ContentValues对象 ContentValues values = new ContentValues(); values.put("name", "Tom"); values.put("age", 18); // 插入数据 Uri uri = getContentResolver().insert(Uri.parse("content://com.example.provider/person"), values); // 查询数据 Cursor cursor = getContentResolver().query(uri, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.d("TAG", "name: " + name + ", age: " + age); } // 关闭游标 cursor.close(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值