记住密码和自动登录

来源:互联网 发布:外交 知乎 编辑:程序博客网 时间: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?
  1. 7,在AndroidMainfest中注册Activity。  

8,运行,成功!


LoginActivity.java

[java] view plaincopyprint?
  1. package com.example.minitwittersimulate;  
  2.   
  3.   
  4.   
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.view.Menu;  
  8. import android.view.Window;  
  9. import android.app.Activity;  
  10. import android.content.Intent;  
  11. import android.content.SharedPreferences;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.CheckBox;  
  17. import android.widget.EditText;  
  18. import android.widget.Toast;  
  19.   
  20.   
  21. public class LoginActivity extends Activity {  
  22.       
  23.       
  24.       
  25.     private EditText etUsername;  
  26.     private EditText etPassword;  
  27.     private CheckBox cbRememberPass;  
  28.     private CheckBox autologin1;  
  29.     private Button btnLogin ;  
  30.     private SharedPreferences sp;  
  31.     private String userNameValue,passwordValue;  
  32.       
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  38.         setContentView(R.layout.activity_main);  
  39.           
  40.         // 初始化用户名、密码、记住密码、自动登录、登录按钮  
  41.         etUsername = (EditText) findViewById(R.id.etUsername);  
  42.         etPassword = (EditText) findViewById(R.id.etPassword);  
  43.         cbRememberPass = (CheckBox) findViewById(R.id.cbRememberPass);  
  44.         autologin1 = (CheckBox) findViewById(R.id.autologin1);  
  45.         btnLogin = (Button) findViewById(R.id.btnLogin);  
  46.   
  47.         sp = getSharedPreferences("userInfo"0);  
  48.         String name=sp.getString("USER_NAME""");  
  49.         String pass =sp.getString("PASSWORD""");  
  50.                   
  51.   
  52.         boolean choseRemember =sp.getBoolean("cbRememberPass"false);  
  53.         boolean choseAutoLogin =sp.getBoolean("autologin1"false);  
  54.           
  55.           
  56.         //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码  
  57.         if(choseRemember){  
  58.             etUsername.setText(name);  
  59.             etPassword.setText(pass);  
  60.             cbRememberPass.setChecked(true);  
  61.         }  
  62.         //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录  
  63.         if(choseAutoLogin){  
  64.             autologin1.setChecked(true);  
  65.         }  
  66.           
  67.           
  68.           
  69.         btnLogin.setOnClickListener(new OnClickListener() {  
  70.               
  71.             // 默认可登录帐号xs,密码123  
  72.             @Override  
  73.             public void onClick(View arg0) {  
  74.                 userNameValue = etUsername.getText().toString();  
  75.                 passwordValue = etPassword.getText().toString();  
  76.                 SharedPreferences.Editor editor =sp.edit();  
  77.                   
  78.                 // TODO Auto-generated method stub  
  79.                 if (userNameValue.equals("xs")  
  80.                         && passwordValue.equals("123")) {  
  81.                     Toast.makeText(LoginActivity.this"登录成功",  
  82.                             Toast.LENGTH_SHORT).show();  
  83.                       
  84.                     //保存用户名和密码  
  85.                     editor.putString("USER_NAME", userNameValue);  
  86.                     editor.putString("PASSWORD", passwordValue);  
  87.                       
  88.                     //是否记住密码  
  89.                     if(cbRememberPass.isChecked()){                       
  90.                         editor.putBoolean("cbRememberPass"true);                        
  91.                     }else{  
  92.                         editor.putBoolean("cbRememberPass"false);               
  93.                     }  
  94.   
  95.                                                               
  96.                     //是否自动登录  
  97.                         if(autologin1.isChecked()){                           
  98.                             editor.putBoolean("autologin1"true);                            
  99.                         }else{  
  100.                             editor.putBoolean("autologin1"false);  
  101.                         }  
  102.                     editor.commit();  
  103.                           
  104.                     //跳转  
  105.                     //Intent intent =new Intent(MainActivity.this,SuccessActivity1.class);  
  106.                     //startActivity(intent);  
  107.                       
  108.                     Intent intent = new Intent();  
  109.                     intent.setClass(LoginActivity.this, SuccessActivity.class);  
  110.                     startActivity(intent);  
  111.                       
  112.                       
  113.                       
  114.                       
  115.                 } else {  
  116.                     Toast.makeText(LoginActivity.this"用户名或密码错误,请重新登录!",  
  117.                             Toast.LENGTH_SHORT).show();  
  118.                 }  
  119.   
  120.             }  
  121.   
  122.         });  
  123.          
  124.   
  125.     }  
  126.   
  127.           
  128.           
  129.       
  130.   
  131.     @Override  
  132.     public boolean onCreateOptionsMenu(Menu menu) {  
  133.         // Inflate the menu; this adds items to the action bar if it is present.  
  134.         getMenuInflater().inflate(R.menu.main, menu);  
  135.         return true;  
  136.     }  
  137.   
  138. }  


MainActivity.java

[java] view plaincopyprint?
  1. package com.example.minitwittersimulate;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.TextView;  
  9. import android.widget.Toast;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private SharedPreferences sp;  
  13.     private TextView talk;  
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.home);       
  18.           
  19.         talk =(TextView) findViewById(R.id.talk);  
  20.           
  21.         sp=getSharedPreferences("userInfo"0);  
  22.         String name =sp.getString("USER_NAME""");  
  23.         boolean choseAutoLogin =sp.getBoolean("autologin1"false);  
  24.         if(choseAutoLogin){  
  25.             talk.setVisibility(0);  
  26.             talk.setText(name+"自动登录成功");  
  27.               
  28.             Intent intent = new Intent();  
  29.             intent.setClass(MainActivity.this, SuccessActivity.class);  
  30.             startActivity(intent);  
  31.         }  
  32.   
  33.     }  
  34.     //跳转到登录页面  
  35.     public void go(View v){  
  36.         //Intent intent =new Intent(this, LoginActivity.class);  
  37.         //startActivity(intent);  
  38.         Intent intent = new Intent();  
  39.         intent.setClass(MainActivity.this, LoginActivity.class);  
  40.         startActivity(intent);  
  41.     }  
  42.       
  43.     //点击退出销毁登录记录  
  44.     public void out(View v){  
  45.         SharedPreferences spout =getSharedPreferences("userInfo"0);  
  46.         SharedPreferences.Editor ed =spout.edit();  
  47.         ed.clear();  
  48.         ed.commit();  
  49.         Toast.makeText(this"销毁记录", Toast.LENGTH_SHORT).show();  
  50.     }  
  51. }  


SuccessActiviy.java

[java] view plaincopyprint?
  1. package com.example.minitwittersimulate;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7.   
  8. public class SuccessActivity extends Activity {  
  9.       
  10.   
  11.       
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.sucess);  
  15.     }  
  16.     public void outactivity(View v){  
  17.           
  18.         System.exit(0);  
  19.     }  
  20. }  


activity_main.xml

[html] view plaincopyprint?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="@drawable/loginbg"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context=".MainActivity" >  
  12.   
  13.   <include layout="@layout/login_top"/>   
  14.   <include layout="@layout/login_bottom"/>"  
  15.   
  16. </LinearLayout>  


home.xml

<?xml version="1.0" encoding="utf-8"?>

[html] view plaincopyprint?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="@drawable/loginbg"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/talk"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="30dp"  
  11.         android:text="文本"   
  12.         android:gravity="center"  
  13.         android:visibility="gone"  
  14.         />  
  15.       
  16.     <Button  
  17.         android:id="@+id/gologin"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="登录"   
  21.         android:onClick="go"  
  22.         />  
  23.     <Button  
  24.         android:id="@+id/out"  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="注销"   
  28.          android:onClick="out"  
  29.         />  
  30. </LinearLayout>  

login_bottom.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/tvRegist"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_marginLeft="21dp"  
  13.         android:layout_marginTop="18dp"  
  14.         android:text="@string/tvRegister"  
  15.         android:autoLink="all"  
  16.         android:textColorLink="#FF0066CC" />  
  17.   
  18.     <ImageView  
  19.         android:id="@+id/imageView1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentBottom="true"  
  23.         android:layout_alignParentRight="true"  
  24.         android:layout_marginBottom="24dp"  
  25.         android:src="@drawable/panda" />  
  26.   
  27.     <ImageView  
  28.         android:id="@+id/imageView2"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_alignParentBottom="true"  
  32.         android:layout_centerHorizontal="true"  
  33.         android:layout_marginBottom="28dp"  
  34.         android:src="@drawable/icon" />  
  35.   
  36. </RelativeLayout>  

login_bottom.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/btnbg_roundcorner"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/tvUsername"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:text="@string/tvName"  
  18.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  19.   
  20.     <EditText  
  21.         android:id="@+id/etUsername"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_alignLeft="@+id/tvUsername"  
  25.         android:layout_below="@+id/tvUsername"  
  26.         android:background="@android:drawable/edit_text"  
  27.         android:ems="10" >  
  28.   
  29.         <requestFocus />  
  30.     </EditText>  
  31.   
  32.     <TextView  
  33.         android:id="@+id/tvPassword"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:layout_alignLeft="@+id/etUsername"  
  37.         android:layout_below="@+id/etUsername"  
  38.         android:text="@string/tvPassword"  
  39.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  40.   
  41.     <EditText  
  42.         android:id="@+id/etPassword"  
  43.         android:layout_width="match_parent"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignLeft="@+id/tvPassword"  
  46.         android:layout_below="@+id/tvPassword"  
  47.         android:layout_marginTop="16dp"  
  48.         android:background="@android:drawable/edit_text"  
  49.         android:ems="10"  
  50.         android:inputType="textPassword" />  
  51.   
  52.     <Button  
  53.         android:id="@+id/btnLogin"  
  54.         android:layout_width="wrap_content"  
  55.         android:layout_height="wrap_content"  
  56.         android:layout_alignRight="@+id/etPassword"  
  57.         android:layout_below="@+id/etPassword"  
  58.         android:layout_marginTop="20dp"  
  59.         android:background="#FF72CAE1"  
  60.         android:text="@string/btnLogin" />  
  61.   
  62.     <CheckBox  
  63.         android:id="@+id/cbRememberPass"  
  64.         android:layout_width="wrap_content"  
  65.         android:layout_height="wrap_content"  
  66.         android:layout_alignLeft="@+id/etPassword"  
  67.         android:layout_alignTop="@+id/btnLogin"  
  68.         android:text="记住密码" />  
  69.   
  70.     <CheckBox  
  71.         android:id="@+id/autologin1"  
  72.         android:layout_width="wrap_content"  
  73.         android:layout_height="wrap_content"  
  74.         android:layout_alignLeft="@+id/cbRememberPass"  
  75.         android:layout_below="@+id/cbRememberPass"  
  76.         android:text="自动登录" />  
  77.   
  78. </RelativeLayout>  


success.xml

<?xml version="1.0" encoding="utf-8"?>

[html] view plaincopyprint?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="@drawable/loginbg"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="30dp"  
  11.         android:gravity="center"  
  12.         android:text="登录成功,重启程序才可以看到效果哦~" />  
  13.       
  14. </LinearLayout>  
0 0
原创粉丝点击