Android implements remembering user names and passwords and automatic login

The mobile nurse station, the first project I came into contact with just after graduation, received the first task is to log in. It needs to use the automatic login function, so make a record here and paste and copy it directly when I use it in the future. Don't talk nonsense and go straight to the subject

Let's start with the renderings. Since we only realize the functions, the interface is not beautified. I'm sorry

Because the content of the XML file is not displayed here, just write it yourself. My parents don't have to worry about my study anymore. 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.Check@R_890_2419@;
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 Check@R_890_2419@ rem;
 private Check@R_890_2419@ 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=(Check@R_890_2419@) findViewById(R.id.remember);
 auto=(Check@R_890_2419@) 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;
 }

}

The user name and password are written dead. In order to facilitate people in need to learn, please explain it a little

if (rem.isChecked()) {

  username_et.setText(sp.getString("username",Welcome.class);
  startActivity(intent1);
  }

 }

The above code means that if you remember the password, you will get the locally stored user name and password. If you log in automatically, you will directly jump to the next web page

if (rem.isChecked()) {
   Editor editor=sp.edit();
   editor.putString("username",Welcome.class);
   startActivity(intent2);

The above code means that if you remember the password, write the user name and password locally

Note that when you jump to the next activity, you need to modify the androidmanifest.xml file. OK, end.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>