zoukankan      html  css  js  c++  java
  • zz:android sharedpreferences用法

     1 import android.app.Activity;
     2 import android.content.SharedPreferences;
     3 import android.os.Bundle;
     4 import android.view.MotionEvent;
     5 import android.view.View;
     6 import android.view.View.OnTouchListener;
     7 import android.widget.EditText;
     8 import android.widget.ImageButton;
     9 
    10 public class MySharedPreferencesActivity extends Activity {
    11     private EditText user = null;
    12     private EditText password = null;
    13     
    14     private ImageButton loginBtn = null;
    15     @Override
    16     public void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.main);
    19         user = (EditText)findViewById(R.id.user);
    20         password = (EditText)findViewById(R.id.pass);
    21         loginBtn = (ImageButton)findViewById(R.id.loginButton);
    22         initView();
    23         loginBtn.setOnTouchListener(new OnTouchListener(){
    24 
    25             @Override
    26             public boolean onTouch(View v, MotionEvent event) {
    27                 if(event.getAction()==MotionEvent.ACTION_DOWN){
    28                     v.setBackgroundResource(R.drawable.dengluxitong1);
    29                     SharedPreferences userInfo = getSharedPreferences("user_info", 0);
    30                     userInfo.edit().putString("name", user.getText().toString()).commit();
    31                     userInfo.edit().putString("pass", password.getText().toString()).commit();
    32                 }
    33                 else if(event.getAction()==MotionEvent.ACTION_UP){
    34                     v.setBackgroundResource(R.drawable.dengluxitong);
    35                 }
    36                 return false;
    37             }
    38             
    39         });
    40     }
    41     private void initView() {
    42         SharedPreferences userInfo = getSharedPreferences("user_info", 0);
    43         String username = userInfo.getString("name", "");
    44         String pass = userInfo.getString("pass", "");
    45         user.setText(username);
    46         password.setText(pass);
    47     }
    48 }
  • 相关阅读:
    Samba 4.0 RC3 发布
    SymmetricDS 3.1.7 发布,数据同步和复制
    Express.js 3.0 发布,Node.js 的高性能封装
    GIFLIB 5.0.1 发布,C语言的GIF处理库
    jQuery UI 1.9.1 发布
    SVN Access Manager 0.5.5.14 发布 SVN 管理工具
    DynamicReports 3.0.3 发布 Java 报表工具
    HttpComponents HttpClient 4.2.2 GA 发布
    AppCan 2.0 正式发布,推移动应用云服务
    Ruby 2.0 的新功能已经冻结
  • 原文地址:https://www.cnblogs.com/DuSizhong/p/2480274.html
Copyright © 2011-2022 走看看