• SharedPreferences(保存偏好参数)


    初次接触SharedPreferences,借助code-pig的博客写的小demo。实现每次打开app自动填充上次推出时输入的名字和年龄。


    工具类SharedService

    package com.example.sharedpreference;
    
    import java.util.HashMap;
    import java.util.Map;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.widget.Toast;
    
    public class SharedService {
    
        private Context context ;
        public SharedService(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context ;
        }
        
        public void save(String name,String age) {
            SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ;
            Editor edit = sp.edit() ;
            edit.putString("name", name) ;
            edit.putString("age", age) ;
            edit.commit() ;
            Toast.makeText(context, "信息已经成功写入", Toast.LENGTH_LONG) ;
        }
        
        public Map<String,String> read() {
            Map<String,String> map = new HashMap<String, String>() ;
            SharedPreferences sp = context.getSharedPreferences("base", Context.MODE_WORLD_READABLE) ;
            map.put("name", sp.getString("name", "")) ;
            map.put("age", sp.getString("age", "")) ;
            return map ;
        }
    
    }
    
    
    

    主activity

    package com.example.sharedpreference;
    
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;  
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends ActionBarActivity {
    
    	private EditText name ;
    	private EditText age ;
    	private Button ok ;
    	private SharedService ss ;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		this.name = (EditText) findViewById(R.id.name) ;
    		this.age = (EditText)findViewById(R.id.age) ;
    		this.ok = (Button) findViewById(R.id.ok) ;
    		ss = new SharedService(this) ;
    		this.name.setText(ss.read().get("name"));
    		this.age.setText(ss.read().get("age"));
    		
    		this.ok.setOnClickListener(new OnClickListener() {
    			
    			@Override
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				ss.save(name.getText().toString(), age.getText().toString());
    				
    			}
    		});
    		
    	}
    
    }
    


    布局代码:

    <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="com.example.sharedpreference.MainActivity" >
    
       <EditText 
           android:id="@+id/name"
           android:layout_width="fill_parent"
           android:hint="输入姓名"
           android:layout_height="wrap_content"/>
       
       <EditText 
           android:id="@+id/age"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:hint="输入年龄"/>
       
       <Button 
           android:id="@+id/ok"
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"
           android:text="确定"/>
    
    </LinearLayout>
    


  • 相关阅读:
    使用textarea标签按Enter键后web页面中成换行 vue
    关于json数组对象和对象数组遇到的一些问题
    vue.js实现checkbox的全选和反选
    关于arcgis js 中要素更新的问题
    C# 图片上传问题
    arcgis js 几种拓扑关系详解
    ISS部署网站--HTTP 错误 404.17
    ADODB.Stream在进行文件上传时报错
    window.open 打开Excel或者Word 无权限问题
    Aspose.cell生成表格
  • 原文地址:https://www.cnblogs.com/emoji/p/4436829.html
Copyright © 2020-2023  润新知