温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android怎么实现记住用户名和密码以及自动登录功能

发布时间:2021-09-01 07:41:44 来源:亿速云 阅读:104 作者:chen 栏目:编程语言

这篇文章主要讲解了“android怎么实现记住用户名和密码以及自动登录功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“android怎么实现记住用户名和密码以及自动登录功能”吧!

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; } }

用户名和密码是写死的,为了方便有需要的人学习,稍微解释一下

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);  }   }

以上代码意思是如果记住密码就拿到本地存储的用户名和密码,如果是自动登录则直接跳转的下一个网页

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);

以上代码意思是说如果是记住密码的状态,则把用户名和密码写到本地

注意一点哈,跳转到下一个activity时,要修改一下AndroidManifest.xml文件,ok,结束。

感谢各位的阅读,以上就是“android怎么实现记住用户名和密码以及自动登录功能”的内容了,经过本文的学习后,相信大家对android怎么实现记住用户名和密码以及自动登录功能这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI