记住密码和自动登录
来源:互联网 发布:外交 知乎 编辑:程序博客网 时间:2024/04/30 06:45
1,在AndroidMainfest文件中注册activity;运行不成功
2,在MainActivity.java文件中的Intent intent = new Intent();intent.setClass(MainActivity.this,
SuccessActivity1.class);
这两句后边加这一句 startActivity(intent);
3,替换文件,改包名:package com.example.minitwittersimulate;
4,创建home.xml文件
5,创建LoginActivity.java文件
6,将LoginActivity.java中的MainActivity改为LoginAvtivity 此时运行显示错误,因为没有在
AndroidMainfest.xml文件中注册LoginActivity
[java] view plaincopyprint?
- 7,在AndroidMainfest中注册Activity。
8,运行,成功!
LoginActivity.java
[java] view plaincopyprint?
- package com.example.minitwittersimulate;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.Window;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
- public class LoginActivity extends Activity {
- private EditText etUsername;
- private EditText etPassword;
- private CheckBox cbRememberPass;
- private CheckBox autologin1;
- private Button btnLogin ;
- private SharedPreferences sp;
- private String userNameValue,passwordValue;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- // 初始化用户名、密码、记住密码、自动登录、登录按钮
- etUsername = (EditText) findViewById(R.id.etUsername);
- etPassword = (EditText) findViewById(R.id.etPassword);
- cbRememberPass = (CheckBox) findViewById(R.id.cbRememberPass);
- autologin1 = (CheckBox) findViewById(R.id.autologin1);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- sp = getSharedPreferences("userInfo", 0);
- String name=sp.getString("USER_NAME", "");
- String pass =sp.getString("PASSWORD", "");
- boolean choseRemember =sp.getBoolean("cbRememberPass", false);
- boolean choseAutoLogin =sp.getBoolean("autologin1", false);
- //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
- if(choseRemember){
- etUsername.setText(name);
- etPassword.setText(pass);
- cbRememberPass.setChecked(true);
- }
- //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录
- if(choseAutoLogin){
- autologin1.setChecked(true);
- }
- btnLogin.setOnClickListener(new OnClickListener() {
- // 默认可登录帐号xs,密码123
- @Override
- public void onClick(View arg0) {
- userNameValue = etUsername.getText().toString();
- passwordValue = etPassword.getText().toString();
- SharedPreferences.Editor editor =sp.edit();
- // TODO Auto-generated method stub
- if (userNameValue.equals("xs")
- && passwordValue.equals("123")) {
- Toast.makeText(LoginActivity.this, "登录成功",
- Toast.LENGTH_SHORT).show();
- //保存用户名和密码
- editor.putString("USER_NAME", userNameValue);
- editor.putString("PASSWORD", passwordValue);
- //是否记住密码
- if(cbRememberPass.isChecked()){
- editor.putBoolean("cbRememberPass", true);
- }else{
- editor.putBoolean("cbRememberPass", false);
- }
- //是否自动登录
- if(autologin1.isChecked()){
- editor.putBoolean("autologin1", true);
- }else{
- editor.putBoolean("autologin1", false);
- }
- editor.commit();
- //跳转
- //Intent intent =new Intent(MainActivity.this,SuccessActivity1.class);
- //startActivity(intent);
- Intent intent = new Intent();
- intent.setClass(LoginActivity.this, SuccessActivity.class);
- startActivity(intent);
- } else {
- Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录!",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- @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;
- }
- }
MainActivity.java
[java] view plaincopyprint?
- package com.example.minitwittersimulate;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private SharedPreferences sp;
- private TextView talk;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.home);
- talk =(TextView) findViewById(R.id.talk);
- sp=getSharedPreferences("userInfo", 0);
- String name =sp.getString("USER_NAME", "");
- boolean choseAutoLogin =sp.getBoolean("autologin1", false);
- if(choseAutoLogin){
- talk.setVisibility(0);
- talk.setText(name+"自动登录成功");
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, SuccessActivity.class);
- startActivity(intent);
- }
- }
- //跳转到登录页面
- public void go(View v){
- //Intent intent =new Intent(this, LoginActivity.class);
- //startActivity(intent);
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, LoginActivity.class);
- startActivity(intent);
- }
- //点击退出销毁登录记录
- public void out(View v){
- SharedPreferences spout =getSharedPreferences("userInfo", 0);
- SharedPreferences.Editor ed =spout.edit();
- ed.clear();
- ed.commit();
- Toast.makeText(this, "销毁记录", Toast.LENGTH_SHORT).show();
- }
- }
[java] view plaincopyprint?
- package com.example.minitwittersimulate;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- public class SuccessActivity extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sucess);
- }
- public void outactivity(View v){
- System.exit(0);
- }
- }
activity_main.xml
[html] view plaincopyprint?
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:background="@drawable/loginbg"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <include layout="@layout/login_top"/>
- <include layout="@layout/login_bottom"/>"
- </LinearLayout>
home.xml
<?xml version="1.0" encoding="utf-8"?>
[html] view plaincopyprint?
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/loginbg"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/talk"
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:text="文本"
- android:gravity="center"
- android:visibility="gone"
- />
- <Button
- android:id="@+id/gologin"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录"
- android:onClick="go"
- />
- <Button
- android:id="@+id/out"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="注销"
- android:onClick="out"
- />
- </LinearLayout>
login_bottom.xml
[html] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/tvRegist"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="21dp"
- android:layout_marginTop="18dp"
- android:text="@string/tvRegister"
- android:autoLink="all"
- android:textColorLink="#FF0066CC" />
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:layout_marginBottom="24dp"
- android:src="@drawable/panda" />
- <ImageView
- android:id="@+id/imageView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="28dp"
- android:src="@drawable/icon" />
- </RelativeLayout>
login_bottom.xml
[html] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/btnbg_roundcorner"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin" >
- <TextView
- android:id="@+id/tvUsername"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="@string/tvName"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <EditText
- android:id="@+id/etUsername"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/tvUsername"
- android:layout_below="@+id/tvUsername"
- android:background="@android:drawable/edit_text"
- android:ems="10" >
- <requestFocus />
- </EditText>
- <TextView
- android:id="@+id/tvPassword"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/etUsername"
- android:layout_below="@+id/etUsername"
- android:text="@string/tvPassword"
- android:textAppearance="?android:attr/textAppearanceMedium" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/tvPassword"
- android:layout_below="@+id/tvPassword"
- android:layout_marginTop="16dp"
- android:background="@android:drawable/edit_text"
- android:ems="10"
- android:inputType="textPassword" />
- <Button
- android:id="@+id/btnLogin"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/etPassword"
- android:layout_below="@+id/etPassword"
- android:layout_marginTop="20dp"
- android:background="#FF72CAE1"
- android:text="@string/btnLogin" />
- <CheckBox
- android:id="@+id/cbRememberPass"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/etPassword"
- android:layout_alignTop="@+id/btnLogin"
- android:text="记住密码" />
- <CheckBox
- android:id="@+id/autologin1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/cbRememberPass"
- android:layout_below="@+id/cbRememberPass"
- android:text="自动登录" />
- </RelativeLayout>
success.xml
<?xml version="1.0" encoding="utf-8"?>
[html] view plaincopyprint?
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/loginbg"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="match_parent"
- android:layout_height="30dp"
- android:gravity="center"
- android:text="登录成功,重启程序才可以看到效果哦~" />
- </LinearLayout>
0 0
- 记住密码和自动登录
- 自动登录和记住密码
- 记住密码和自动登录
- 记住密码和自动登录
- 记住密码,自动登录
- 什么是记住密码和自动登录
- 记住密码和自动登录效果
- 实现记住密码和自动登录功能
- 实现用户记住密码和自动登录
- 安卓记住密码和自动登录
- 安卓记住密码和自动登录
- android 自动登录和记住密码
- Android App记住密码和自动登录
- Android实现自动登录和记住密码
- android记住密码 和自动登录
- 实现自动登录和记住密码
- 用户登录流程中,记住密码和自动登录逻辑。
- php 记住密码自动登录
- Java程序设计第十七周之猜猜看
- 把树莓派用于YAAW离线下载的Mass Storage U盘用虚拟目录建到Vsftpd的子目录里去
- 浅谈图片服务器的架构演进
- 怎样理解阻塞非阻塞与同步异步的区别?
- 半同步/半异步并发模式进程池实现
- 记住密码和自动登录
- Linux文件系统结构
- UI基础:视图控制器.屏幕旋转.MVC
- Android如何实现毛玻璃效果之Android高级模糊技术
- for do_while() while()
- Android英汉词典
- 程序报告
- BP神经网络PYthon实现(带有”增加充量项“)
- Android-Shortcut