android SharedPreferences 的使用

来源:互联网 发布:福莱预算软件 编辑:程序博客网 时间:2024/04/20 07:52

package com.yek;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyShared extends Activity {//SharedPreferences的创建、写数据、取数据
    /** Called when the activity is first created. */
 SharedPreferences sp;
 SharedPreferences.Editor editor;
 Context context;
 String name;
 Button mybutton;
 TextView mytext;
 EditText username;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mybutton = (Button)findViewById(R.id.mybutton);
        mytext = (TextView)findViewById(R.id.mytext);
        username = (EditText)findViewById(R.id.myedit);
        SharedPreferencesHelper(this, "myShared");
        mybutton.setOnClickListener(new View.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    String inputname = username.getText().toString();
    putValue("name", inputname);
       name = getValue("name");
       if(name.equals("")){
        mytext.setBackgroundColor(Color.WHITE);
        mytext.setText("你还没输入武将姓名");
       }else{
        mytext.setBackgroundColor(Color.WHITE);
        mytext.setText("你输入的武将为:"+name);
       }
   }
        });
    }
   
    public void SharedPreferencesHelper(Context c, String name){//创建
     this.context = c;
     sp = context.getSharedPreferences(name, 0);
     editor = sp.edit();
    }
   
    public void putValue(String key, String value){//写数据
     editor = sp.edit();
     editor.putString(key, value);
     editor.commit();
    }
   
    public String getValue(String key){                   //取值
     return sp.getString(key, null);//有值的话取出值,没有的话就返回null
    }
}

 

 

 

xml代码:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
    android:id="@+id/myedit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入武将的姓名"
    android:textSize="17dip"
    android:textColor="#00BFFF"
    />
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">
<Button 
    android:id="@+id/mybutton"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:text="点击获得武将的姓名"
    android:textSize="17dip"
    android:textColor="#00BFFF"
    />
</RelativeLayout>
   
<TextView
    android:id="@+id/mytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="17dip"
    android:textColor="#00BFFF"
    />
</LinearLayout>

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 内存太小怎么办手机 苹果手机屏幕不能滑动怎么办 vivo手机软件不兼容怎么办 微信无法录音怎么办 手机卡住了怎么办vivo 好钱包闪退怎么办 闲鱼认证失败怎么办 闲鱼买了假门票怎么办 买黄金买到假的怎么办 闲鱼被买家骗了怎么办 闲鱼上小视频没法保存怎么办 qq空间无法查看怎么办 华为手机电池不耐用怎么办 内内被动过怎么办 hp电脑开机黑屏怎么办 网上开店快递费怎么办 保温杯外壳掉漆怎么办 拖鞋前面磨脚怎么办 塑料拖鞋磨脚怎么办 路由器进不去设置界面怎么办 手机号丢了微信登不上怎么办 电脑总是闪黑屏怎么办 支付宝破产钱怎么办 淘宝号码注册过怎么办 农行k宝怎么办信用卡 电脑页面无法显示怎么办 对方银行停止收款怎么办 淘宝东西买太多怎么办 淘宝号黑号了怎么办… 中通包裹异常怎么办 包裹退回去了怎么办 qq支付密码错误怎么办 ie8出现闪退怎么办 平板输不了密码怎么办 华硕笔记本键盘打不开怎么办 电脑打不开rar文件怎么办 苹果手机淘宝卡怎么办 淘宝联盟网址打不开怎么办 淘宝买东西卖家不退货怎么办 手机清理后微信打不开视频怎么办 搜索历史已关闭怎么办