实验 记住密码

来源:互联网 发布:python在哪里下载 编辑:程序博客网 时间:2024/05/14 15:22

一.实验现象


二.实验部分代码

package com.example.minitwittersimulate;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.os.Message;import android.preference.PreferenceManager;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class LoginActivity extends Activity {private static final String FILE = null;private SharedPreferences sp ; private EditText Username; private EditText Password; private String Usernamevalue,Passwordvalue; private CheckBox cbRP; private Button btnLogin; private String isMemory = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_login);sp = PreferenceManager.getDefaultSharedPreferences(this);          Username = (EditText) findViewById(R.id.etUsername);         Password = (EditText) findViewById(R.id.etPassword);           cbRP = (CheckBox) findViewById(R.id.cbRememberPass);         btnLogin = (Button) findViewById(R.id.btnLogin);         boolean isRemember =sp.getBoolean("cbRememberpass", false);         if(isRemember){            //设置默认是记录密码状态           String name=sp.getString("name", "");         String password=sp.getString("password", "");            Username.setText(name);              Password.setText(password);            cbRP.setChecked(true);         }         Editor editor = sp.edit();         editor.putString("Username", Username.toString());         editor.putString("Password", Password.toString());         editor.commit();         btnLogin.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View v) {          Usernamevalue = Username.getText().toString();          Passwordvalue = Password.getText().toString();         remenber();         Intent it=new Intent(LoginActivity.this, MainActivity.class);//跳转         startActivity(it);         finish();         }private void remenber() {// TODO Auto-generated method stubEditor edit = sp.edit();if (cbRP.isChecked()) {edit.putBoolean("cbRememberpass", true);edit.putString("name", Usernamevalue);edit.putString("password", Passwordvalue);edit.commit();} else if (!cbRP.isChecked()) {edit.clear();edit.commit();}}         });}@Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }       }
三.实验体会心得

   第一次登陆成功后,将输入的账户和密码存储起来,然后用点击事件,第二次登陆的时候则会主动出现不用用户再输入了,但我这个程序,还没有完全实现这个功能。本程序登陆成功后,打开这个APP就会自动出现用户名和密码。当输入用户名时,不能主动弹出密码。本程序还需要改进。

0 0
原创粉丝点击