zoukankan      html  css  js  c++  java
  • 保存、方法Android文件存储采用SharedPreferences保存用户偏好设置参数和读取设置参数by小雨

    在写这篇文章之前,xxx已经写过了几篇关于改保存、方法-主题的文章,想要了解的朋友可以去翻一下之前的文章

        Android文件存储--采取SharedPreferences保存用户偏好置设数参和读取置设数参

        

        Android SDK持支那些文件存储术技?

        1. 应用SharedPreferences保存key-value类型的据数

        2. 流文件存储(应用openFileOutput和openFileInput方法,或FileInputStream和FileOutputStream)

        3. XML半结构化存储

        4. 用JSON保存组数和对象

        5.用据数库保存结构化据数

        6. 用第三方的面向对象据数库直接保存Java对象。

        

        这篇博文重要绍介用SharedPreferences保存key-value对的步调和读取置设数参的方法

        1. 应用Context.getSharedPreferences方法取获SharedPreferences对象,其中存储key-value的文件的名称有getSharedPreferences方法第一个数参指定。

        2. 应用SharedPreference.edit方法取获SharedPreferences.Editor对象。

        3. 通过SharedPreference.Editor接口的putXxx方法保存key-value对。

        4. 通过SharedPreference.Editor.commit方法提交要保存的key-value对。

        

        

        例实:SharedPreferences

        

        

        MainActivity.java

        

    package com.wwj.setting;
    
    import java.util.Map;
    
    import com.wwj.service.PreferencesService;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    	private EditText nameText;		//姓名框
    	private EditText ageText;		//春秋框
    	private RadioGroup radioGroup;	//单选框组
    	
    	//业务逻辑类
    	private PreferencesService service;
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            nameText = (EditText)findViewById(R.id.nameText);
            ageText = (EditText)findViewById(R.id.ageText);
            radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
            service = new PreferencesService(this);
            
            Map<String, String> params = service.getPerferences();
            nameText.setText(params.get("name"));
            ageText.setText(params.get("age"));
            radioGroup.check(Integer.valueOf(params.get("sex")));	//置设选择的单选钮按
            
        }
        
        /**
         * 在布局中钮按控件指定的onClick方法
         * @param v
         */
        public void save(View v) {
        	String name = nameText.getText().toString();
        	String age = ageText.getText().toString();
        	Integer sex = radioGroup.getCheckedRadioButtonId();
        	service.save(name, Integer.valueOf(age), sex);
        	Toast.makeText(getApplicationContext(), R.string.success, 1).show();
        }
        
    }

        PreferencesService.java

        

    package com.wwj.service;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    
    public class PreferencesService {
    	private Context context;
    
    	public PreferencesService(Context context) {
    		this.context = context;
    	}
    
    	/**
    	 * 保存数参
    	 * @param name	姓名
    	 * @param age	春秋	
    	 * @param sex	性别
    	 */
    	public void save(String name, Integer age, Integer sex) {
    		//得获SharedPreferences对象
    		SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
    		Editor editor = preferences.edit();
    		editor.putString("name", name);
    		editor.putInt("age", age);
    		editor.putInt("sex", sex);
    		editor.commit();
    	}
    
    	/**
    	 * 取获各项数参
    	 * @return
    	 */
    	public Map<String, String> getPerferences() {
    		Map<String, String> params = new HashMap<String, String>();
    		SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
    		params.put("name", preferences.getString("name", ""));
    		params.put("age", String.valueOf(preferences.getInt("age", 0)));
    		params.put("sex", String.valueOf(preferences.getInt("sex", 0)));
    		return params;
    	}
    	
    	
    	
    }

        布局文件activity_main.xml

        

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    	<TextView 
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    android:text="@string/name"/>
    	<EditText 
    	    android:id="@+id/nameText"
    	    android:layout_width="match_parent"
    	    android:layout_height="wrap_content"
    	    />
    	<TextView 
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    android:text="@string/age"/>
    	<EditText 
    	    android:id="@+id/ageText"
    	    android:layout_width="match_parent"
    	    android:layout_height="wrap_content"
    	    android:numeric="integer"
    	    />
    	<RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="性别" >
    
            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/male" 
                android:checked="true"/>
    
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/female" />
        </RadioGroup>
    	
    	<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="save"
            android:text="@string/saveBtn" />
    	
    </LinearLayout>

        

        

        

        

        

        

        

        

    文章结束给大家分享下程序员的一些笑话语录: 程序员打油诗   
      写字楼里写字间,写字间里程序员;
      程序人员写程序,又拿程序换酒钱。
      酒醒只在网上坐,酒醉还来网下眠;
      酒醉酒醒日复日,网上网下年复年。
      但愿老死电脑间,不愿鞠躬老板前;
      奔驰宝马贵者趣,公交自行程序员。
      别人笑我忒疯癫,我笑自己命太贱;
      不见满街漂亮妹,哪个归得程序员。

  • 相关阅读:
    [Err] 1168
    SpringAOP的运用方式——注解方式和XML配置方式
    展开运算符的理解
    find indexof findindex includes 的区别
    关于MVC MVVM的理解
    sync修饰符
    vue导航守卫
    输入网址到页面显示 经历了什么(转载)
    vue中侦听器的使用
    函数声明的三种方式
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3022990.html
Copyright © 2011-2022 走看看