【English】Android -> Training -> Adding the Action Bar -> Saving Data -> sharedPreferences【1——004】

step 1 : Get a Handle to a SharedPreferences

Context context = getActivity(); 
SharedPreferences sharedPref = context.getSharedPreferences( 
        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

step 2 : Write to Shared Preferences

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
editor.putInt(getString(R.string.saved_high_score), newHighScore); 
editor.commit();

step 3 : Read from Shared Preferences

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 
int defaultValue = getResources().getInteger(R.string.saved_high_score_default); 
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);


Practice:

step 1 : create layout xml for MainActivity

<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" >

    <TextView
        android:id="@+id/oldcontent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:hint="@string/hello_world"
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <Button 
        android:id="@+id/btnSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnText"/>

</LinearLayout>


 

step 2 : modify MainActivity.java

package com.example.sharedpreferences;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btn_save;
	private EditText input;
	private TextView oldContent;
	private SharedPreferences sharedPreferences = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sharedPreferences = getSharedPreferences(this.getApplication().getPackageName(),
				Context.MODE_PRIVATE);
		setContentView(R.layout.activity_main);
		initComponents();
	}

	private void initComponents() {
		btn_save = (Button) findViewById(R.id.btnSave);
		btn_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Editor editor = sharedPreferences.edit();
				editor.putString(getString(R.id.oldcontent), input.getText()
						.toString());
				editor.commit();
				oldContent.setText(input.getText());
			}
		});
		input = (EditText) findViewById(R.id.editText);
		oldContent = (TextView) findViewById(R.id.oldcontent);
		String oldText = sharedPreferences.getString(
				getString(R.id.oldcontent), "default");
		oldContent.setText(oldText);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值