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 }
  • 相关阅读:
    枚举
    张三先唱一遍要表演的歌曲,老师觉得张三唱歌不过关,
    不断要求用户输入一个数字(假定用户输入的都是正整数
    不断要求用户输入学生姓名,输入q结束.
    要求用户输入用户名和密码,只要不是admin、888888就
    计算1到100的整数和
    c# 九九乘法表
    c#三角形
    C#循环判断密码
    什么是发动机号,发动机号码是什么?
  • 原文地址:https://www.cnblogs.com/DuSizhong/p/2480274.html
Copyright © 2011-2022 走看看