Android复习练习七(使用SharedPreferences)

需求:写一个简单的应用,实现提取输入框的输入内容,通过点击按钮调用sharedpreferences保存内容,然后通过点击另一个按键显示出保存的内容


这个练习旨在复习SharedPreferences工具的使用,注意点如下:1.SharedPreferences默认保存目录是/data/data/包名/shared_prefs/;2.SharedPreferences保存信息的格式为xml文件,比如,保存一个(key,value)对这样,如果value是String类型的,那么在保存的xml文件中会是这种标签的形式<string name=key>value</string>

Activity代码如下:

package com.alexchen.sharedpreferencesdemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText etSp;
	private TextView tvShow;
	private String content;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		etSp = (EditText) findViewById(R.id.et_sp);
		tvShow = (TextView) findViewById(R.id.tv_show);

	}

	public void save(View v) {
		try {
			sp = getSharedPreferences("info", MODE_PRIVATE);
			Editor edit = sp.edit();
			content = etSp.getText().toString().trim();
			edit.putString("content", content);
			edit.commit();
			Toast.makeText(this, "保存成功", 0).show();
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "保存失败", 0).show();
		}
	}

	public void show(View v) {
		try {
			sp = getSharedPreferences("info", MODE_APPEND);
			String content = sp.getString("content", "");// 第一个参数是key值,第二个参数是默认值,如果对应key值的value不存在,则返回这个默认值
			tvShow.setText(content);
			Toast.makeText(this, "读取成功", 0).show();
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "读取失败", 0).show();
		}

	}
}

布局文件代码:

<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="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/et_sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:hint="请输入要存入SharedPreferences的内容" 
        android:textSize="15sp"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:onClick="save"
            android:text="保存" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:onClick="show"
            android:text="显示" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

效果如图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值