大佬教程收集整理的这篇文章主要介绍了android实现记住用户名和密码以及自动登录,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
毕业刚开始上班接触的第一个项目移动护士站,接到了第一任务就是登录,要用到自动登录功能,所以在这做个记录,以后用的时候直接来粘贴复制,废话少说,直奔主题
由于xml文件内容,就不展现在这了,自己写一写就好,爸妈再也不用担心我的学习了,so easy
package com.sdufe.login; 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.Menu; import android.view.View; import android.widget.button; import android.widget.checkBox; import android.widget.EditText; import android.widget.Toast; /** * @author lili.guo * * 2014-6-6下午3:20:17 */ public class MainActivity extends Activity { private EditText username_et; private EditText password_et; private checkBox rem; private checkBox auto; private Button login; private String username,password; SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); sp=getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE); username_et=(EditText) findViewById(R.id.userName); password_et=(EditText) findViewById(R.id.password); rem=(checkBox) findViewById(R.id.remember); auto=(checkBox) findViewById(R.id.autologin); login=(Button) findViewById(R.id.login); if (rem.ischecked()) { username_et.setText(sp.getString("username","")); password_et.setText(sp.getString("password","")); if (auto.ischecked()) { Intent intent1=new Intent(); intent1.setClass(getApplicationContext(),Welcome.class); startActivity(intent1); } } login.setOnClickListener(new View.onClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub username=username_et.getText().toString(); password=password_et.getText().toString(); if (username.equals("Thea")&&password.equals("123")) { Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show(); if (rem.ischecked()) { Editor editor=sp.edit(); editor.putString("username",userName); editor.putString("password",password); editor.commit(); } Intent intent2=new Intent(); intent2.setClass(getApplicationContext(),Welcome.class); startActivity(intent2); } } }); } @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; } }@H_419_17@if (rem.ischecked()) { username_et.setText(sp.getString("username",Welcome.class); startActivity(intent1); } }@H_419_17@以上代码意思是如果记住密码就拿到本地存储的用户名和密码,如果是自动登录则直接跳转的下一个网页
if (rem.ischecked()) { Editor editor=sp.edit(); editor.putString("username",Welcome.class); startActivity(intent2);@H_419_17@以上代码意思是说如果是记住密码的状态,则把用户名和密码写到本地
以上是大佬教程为你收集整理的android实现记住用户名和密码以及自动登录全部内容,希望文章能够帮你解决android实现记住用户名和密码以及自动登录所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。