数据库存储-读取资源信息

本文介绍了一个简单的Android应用程序示例,展示了如何使用SharedPreferences组件来读取之前保存的姓名和年龄信息,并将这些信息显示在TextView组件中。此方法适用于保存应用的基本配置信息。


 

在main.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <TextView

        android:id="@+id/nameinfo"

      android:gravity="center_horizontal"

      android:layout_marginTop="8dp"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

    <TextView

        android:id="@+id/ageinfo"

      android:gravity="center_horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

 

</LinearLayout>

 

 

 

 

 

在MySharedPreferencesDemo.java程序中

package com.li.sharedpreferences;

 

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.widget.TextView;

 

public class MySharedPreferencesDemo extends Activity {

  private static final String FILENAME = "BaoCunDeWenJianMingCheng"; //保存的文件名称

 

  private TextView nameinfo = null;

  private TextView ageinfo = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

       

        this.nameinfo = (TextView)super.findViewById(R.id.nameinfo);

        this.ageinfo = (TextView)super.findViewById(R.id.ageinfo);

       

        SharedPreferences share = super.getSharedPreferences(FILENAME

           , Activity.MODE_PRIVATE);

        this.nameinfo.setText("姓名:" + share.getString("name", "没有作者信息!"));

        this.ageinfo.setText("年龄:" + share.getInt("age", 0));

      

    }

}


 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值