登录界面中密码与用户名的保存

来源:互联网 发布:知念和凉介抱在一起睡 编辑:程序博客网 时间:2024/06/05 17:49

1.谈到保存就不能不说SharedPreferences类。

  1)SharedPreferencesAndroid平台上量级的数据存储方式,通常用来存储一些简单的配置信息例如用户名,密码,自定义参数的设置等

  2)SharedPreferencesvalue值只能是floatint,long,boolean,String,StringSet

2.源程序:

1)登录界面

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/logo_bg"    android:orientation="vertical" >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <ImageButton             android:id="@+id/img_btn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:background="@drawable/quit"/>        <TextView            android:id="@+id/tv_zh"            android:layout_width="wrap_content"            android:layout_height="35dip"            android:layout_marginLeft="12dip"            android:layout_marginTop="10dip"            android:gravity="bottom"            android:text="帐号:"            android:textColor="#000000"            android:textSize="18sp" />        <EditText            android:id="@+id/et_zh"            android:layout_width="fill_parent"            android:layout_height="40dip"            android:layout_below="@id/tv_zh"            android:layout_marginLeft="12dip"            android:layout_marginRight="10dip" />        <TextView            android:id="@+id/tv_mima"            android:layout_width="wrap_content"            android:layout_height="35dip"            android:layout_below="@id/et_zh"            android:layout_marginLeft="12dip"            android:layout_marginTop="10dip"            android:gravity="bottom"            android:text="密码:"            android:textColor="#000000"            android:textSize="18sp" />        <EditText            android:id="@+id/et_mima"            android:layout_width="fill_parent"            android:layout_height="40dip"            android:layout_below="@id/tv_mima"            android:layout_marginLeft="12dip"            android:layout_marginRight="10dip"            android:maxLines="200"            android:password="true"            android:scrollHorizontally="true" />        <CheckBox            android:id="@+id/cb_mima"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/et_mima"            android:layout_marginLeft="12dip"            android:text="记住密码"            android:textColor="#000000" />            </RelativeLayout>    <Button        android:id="@+id/btn_login"        android:layout_width="80dip"        android:layout_height="40dip"        android:layout_below="@id/et_mima"        android:layout_alignParentRight="true"        android:layout_gravity="center"        android:layout_marginRight="10dip"        android:gravity="center"        android:text="登录"        android:textColor="#000000"        android:textSize="18sp"/></LinearLayout>
2)加载页面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/logo_bg"    android:orientation="vertical" >    <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:layout_weight="3">        <ProgressBar            android:id="@+id/pgBar"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true" />        <TextView            android:id="@+id/tv1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/pgBar"            android:layout_centerHorizontal="true"            android:text="正在登录..."            android:textColor="#000000"            android:textSize="18sp" />    </RelativeLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:orientation="vertical" >        <Button            android:id="@+id/btn_back"            android:layout_width="70dip"            android:layout_height="35dip"            android:text="取消"            android:textColor="#000000"            android:textSize="12sp" />    </LinearLayout></LinearLayout>

3)登录成功页面

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_gravity="center"    android:background="@drawable/login_bg"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="登陆成功,进入用户界面"        android:textColor="#000000"        android:textSize="20sp" /></LinearLayout>

4)登陆界面

package com.liu.activity;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.EditText;import android.widget.ImageButton;import android.widget.Toast;public class LoginActivity extends Activity {      private EditText userName, password;   private CheckBox rem_pw, auto_login;   private Button btn_login;   private ImageButton btnQuit;    private String userNameValue,passwordValue;   private SharedPreferences sp;   public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);                  this.requestWindowFeature(Window.FEATURE_NO_TITLE);      setContentView(R.layout.login);            sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);      userName = (EditText) findViewById(R.id.et_zh);      password = (EditText) findViewById(R.id.et_mima);        rem_pw = (CheckBox) findViewById(R.id.cb_mima);      auto_login = (CheckBox) findViewById(R.id.cb_auto);        btn_login = (Button) findViewById(R.id.btn_login);        btnQuit = (ImageButton)findViewById(R.id.img_btn);                         if(sp.getBoolean("ISCHECK", false))        {                  rem_pw.setChecked(true);             userName.setText(sp.getString("USER_NAME", ""));             password.setText(sp.getString("PASSWORD", ""));                          if(sp.getBoolean("AUTO_ISCHECK", false))             {                                     auto_login.setChecked(true);                             Intent intent = new Intent(LoginActivity.this,LogoActivity.class);            LoginActivity.this.startActivity(intent);                         }        }                 btn_login.setOnClickListener(new OnClickListener() {         public void onClick(View v) {            userNameValue = userName.getText().toString();             passwordValue = password.getText().toString();                         if(userNameValue.equals("liu")&&passwordValue.equals("123"))            {               Toast.makeText(LoginActivity.this,"", Toast.LENGTH_SHORT).show();                         if(rem_pw.isChecked())               {                           Editor editor = sp.edit();                 editor.putString("USER_NAME", userNameValue);                 editor.putString("PASSWORD",passwordValue);                 editor.commit();               }                       Intent intent = new Intent(LoginActivity.this,LogoActivity.class);               LoginActivity.this.startActivity(intent);                                  }else{                              Toast.makeText(LoginActivity.this,"", Toast.LENGTH_LONG).show();            }                     }      });      rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {            if (rem_pw.isChecked()) {                                   System.out.println("true");               sp.edit().putBoolean("ISCHECK", true).commit();                           }else {                              System.out.println("false");               sp.edit().putBoolean("ISCHECK", false).commit();                           }         }      });            auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {            if (auto_login.isChecked()) {               System.out.println("true");               sp.edit().putBoolean("AUTO_ISCHECK", true).commit();            } else {               System.out.println("false");               sp.edit().putBoolean("AUTO_ISCHECK", false).commit();            }         }      });            btnQuit.setOnClickListener(new OnClickListener() {                  @Override         public void onClick(View v) {            finish();         }      });   }

5)登录加载页面

package com.liu.activity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class LogoActivity extends Activity {   private ProgressBar progressBar;   private Button backButton;   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      this.requestWindowFeature(Window.FEATURE_NO_TITLE);      setContentView(R.layout.logo);      progressBar = (ProgressBar) findViewById(R.id.pgBar);      backButton = (Button) findViewById(R.id.btn_back);      progressBar.setMax(3000);      Intent intent = new Intent(this, WelcomeAvtivity.class);      LogoActivity.this.startActivity(intent);      backButton.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View v) {            finish();         }      });   }}

6)登陆成功界面

package com.liu.activity;import android.app.Activity;import android.os.Bundle;public class WelcomeAvtivity extends Activity {   @Override   protected void onCreate(Bundle savedInstanceState) {      // TODO Auto-generated method stub      super.onCreate(savedInstanceState);      setContentView(R.layout.welcome);   }}
图示:
登录页面
登录成功页面
登录加载页面
后记:老师的题目放在U盘中,不知道为啥没有了,崩溃了一阵后开始了上网搜,自己勉勉强强的做出如此。
PS:图片没有换,用的是原图片。


1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 qq飞车求婚失败戒指怎么办 改脸型皮肤会下垂怎么办 情侣关系弄僵了怎么办 用微信交话费没有到账怎么办 微信交错话费了怎么办 微信缴费交错了怎么办 微信支付被投诉怎么办 微信q币充值错误怎么办 微信充值流量充错了怎么办 微信延迟到账怎么办 移动流量充错了怎么办 qq充话费等待发货怎么办 qq充值话费没到账怎么办 电信话费冲错了怎么办 微信手机充错话费充空号怎么办 京东地址写错了怎么办 京东售后不退款怎么办 冲了话费不到账怎么办 币安维护充值怎么办 微信话费未到账怎么办 微信话费交错了怎么办 北京移动查话费余额怎么办 淘宝卖家客服无法联系怎么办? 微信支付月限额怎么办 微信超额20万怎么办 微信支付超额了怎么办 微信零钱超额了怎么办 微信的充值冲错了怎么办 有流量还扣话费怎么办 自动取款机充值到电子账户怎么办 淘宝qb充错了怎么办 q币冲错了人家不给怎么办 qq充值话费错号怎么办 qq充错号码了怎么办 qq交话费不到账怎么办 充错手机号码而且是空号怎么办 微信钱包充错话费怎么办 QQ充值话费充到空号了怎么办 给别人充错话费怎么办 用qq交错话费对方是空号怎么办 号码变成空号了怎么办