自动登录界面
2015-06-27 21:17
375 查看
LoginActivity.java
package com.example.autologin;
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 username;
private EditText userpassword;
private CheckBox remember;
private CheckBox autologin;
private Button login;
private SharedPreferences sp;
private String userNameValue,passwordValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// 初始化用户名、密码、记住密码、自动登录、登录按钮
username = (EditText) findViewById(R.id.username);
userpassword = (EditText) findViewById(R.id.userpassword);
remember = (CheckBox) findViewById(R.id.remember);
autologin = (CheckBox) findViewById(R.id.autologin);
login = (Button) findViewById(R.id.login);
sp = getSharedPreferences("userInfo", 0);
String name=sp.getString("USER_NAME", "");
String pass =sp.getString("PASSWORD", "");
boolean choseRemember =sp.getBoolean("remember", false);
boolean choseAutoLogin =sp.getBoolean("autologin", false);
// Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
//如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码
if(choseRemember){
username.setText(name);
userpassword.setText(pass);
remember.setChecked(true);
}
//如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录
if(choseAutoLogin){
autologin.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
// 默认可登录帐号android,密码123
@Override
public void onClick(View arg0) {
userNameValue = username.getText().toString();
passwordValue = userpassword.getText().toString();
SharedPreferences.Editor editor =sp.edit();
// TODO Auto-generated method stub
if (userNameValue.equals("android")
&& passwordValue.equals("123")) {
Toast.makeText(LoginActivity.this, "登录成功",
Toast.LENGTH_SHORT).show();
//保存用户名和密码
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
//是否记住密码
if(remember.isChecked()){
editor.putBoolean("remember", true);
}else{
editor.putBoolean("remember", false);
}
//是否自动登录
if(autologin.isChecked()){
editor.putBoolean("autologin", true);
}else{
editor.putBoolean("autologin", false);
}
editor.commit();
//跳转
Intent intent =new Intent(LoginActivity.this,SuccessActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
MainActivity.java
package com.example.autologin;
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("autologin", false);
if(choseAutoLogin){
talk.setVisibility(0);
talk.setText(name+"自动登录成功");
}
}
//跳转到登录页面
public void go(View v){
Intent intent =new Intent(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();
}
}
SuccessActivity.java
package com.example.autologin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class SuccessActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
}
public void outactivity(View v){
System.exit(0);
}
}
home.xml
<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="41dp"
android:background="@drawable/e"
android:textColor="#050505"
android:onClick="go"
android:text="登录" />
<Button
android:id="@+id/out"
android:layout_width="match_parent"
android:layout_height="43dp"
android:background="@drawable/a"
android:textColor="#050505"
android:onClick="out"
android:text="退出登录" />
login.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:text="用户名:" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#050505"
android:text="密码:" />
<EditText
android:id="@+id/userpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" >
</EditText>
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:text="记住密码" />
<CheckBox
android:id="@+id/autologin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:text="自动登录" />
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="0.00"
android:background="@drawable/f"
android:text="登录" />
success.xml
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="登录成功,重启程序才可以看到效果哦~"
android:textColor="#050505"
android:background="@drawable/f" />
相关文章推荐
- eclipse中hibernate配置文件自动提示
- iOS开发——CoreBluetooth蓝牙开发(二)
- C++13.3 析构函数destructor
- 多路归并使用败者树的性能分析
- 使用AsyncTask实现网络数据的异步访问
- 需求调研报告(用户需求说明书)编写规范
- Spring过滤器filter拦截服务
- 150.按规定输出
- 将IDEA快捷键改为Eclipse模式
- 去中心化:微信抄袭泛滥的原罪?
- 2015062706 - EffactiveJava笔记 - 第39条 必要时进行保护性拷贝
- cif安装错误异常处理过程记录
- MSQL优化基础(SQL执行过程,链接方式案例)
- 如何停止一个正在运行的java线程?
- C语言qsort
- 如何统计博客园的访问量
- 牛腩新闻发布系统(2)——大战CSS
- 如何快速从Ubuntu系统的eclipse下导出jar包(MapReduce程序)
- DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程
- PhoneGap Eclipse 分别在虚拟机和手机上进行调试