zoukankan      html  css  js  c++  java
  • android 数据存储之SharedPerferences

    package com.example.phonedemo;
    
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.FrameLayout.LayoutParams;
    import android.widget.LinearLayout;
    
    public class SharePreferenceDemo extends Activity {
        private LayoutParams wrap = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        private LayoutParams match = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    
        private LinearLayout layout = null;
    
        private EditText name = null;
        private EditText school = null;
        private EditText age = null;
        private Button but = null;
        private SharedPreferences sf = null;
        private SharedPreferences.Editor edit = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.sf = super.getSharedPreferences("PhoneDemo", MODE_PRIVATE);
            this.edit = this.sf.edit();
            this.layout = new LinearLayout(this);
            this.layout.setOrientation(LinearLayout.VERTICAL);
    
            this.name = new EditText(this);
            this.name.setText(getValue("name"));
            this.layout.addView(this.name, wrap);
    
            this.school = new EditText(this);
            this.school.setText(getValue("school"));
            this.layout.addView(this.school, wrap);
    
            this.age = new EditText(this);
            this.age.setText(getValue("age"));
            this.layout.addView(this.age, wrap);
    
            this.but = new Button(this);
            this.but.setText("save");
            this.but.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    System.out.println("name: "
                            + SharePreferenceDemo.this.name.getText().toString());
                    SharePreferenceDemo.this.edit.putString("name",
                            SharePreferenceDemo.this.name.getText().toString());
                    SharePreferenceDemo.this.edit.putString("school",
                            SharePreferenceDemo.this.school.getText().toString());
                    SharePreferenceDemo.this.edit.putString("age",
                            SharePreferenceDemo.this.age.getText().toString());
                    SharePreferenceDemo.this.edit.commit();
                }
            });
            this.layout.addView(this.but, wrap);
    
            super.addContentView(this.layout, match);
        }
    
        public String getValue(String string) {
            if (string == "name" || string == "school" || string == "age") {
                return sf.getString(string, string);
            }
            return "null";
        }
    
    }
  • 相关阅读:
    Fabric简介
    推荐一个在线Markdown编写网站,可转化PDF,DOC格式
    7-独立事件和互不相容(概率论与数理统计学习笔记)
    6- 全概率公式/贝叶斯公式(概率论与数理统计学习笔记)
    5-条件概率/乘法公式(概率论与数理统计学习笔记)
    4-几何概型/频率/公理化(概率论与数理统计学习笔记)
    3-古典概率与排列组合(概率论与数理统计学习笔记)
    PLS-00306: 调用 'SYNCRN' 时参数个数或类型错误
    C# MongoDB 查询,分组,聚合,排序,条件,分页
    MVC + Vue.js 初体验(实现表单操作)
  • 原文地址:https://www.cnblogs.com/waddell/p/3394697.html
Copyright © 2011-2022 走看看