android 自动登录
来源:互联网 发布:有那些发表文章的软件 编辑:程序博客网 时间:2024/06/05 16:19
//主登录布局
<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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="22sp" />
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#0d0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="22sp" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入密码"
android:inputType="numberPassword"
android:maxLines="11"
android:password="true"
android:scrollHorizontally="true" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/jizhumima"
android:layout_width="50dp"
android:layout_height="50dp"
android:checked="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/jizhulogin"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:textSize="20sp" />
</LinearLayout>
<Button
android:id="@+id/login"
android:layout_width="120dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:text="登录"
android:textSize="30sp" />
</LinearLayout>
//正在登录布局
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<ProgressBar
android:id="@+id/pgBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pgBar"
android:layout_centerHorizontal="true"
android:text="正在登录..."
android:textColor="#000000"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/tv1" >
</RadioGroup>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/btn_back"
android:layout_width="70dip"
android:layout_height="35dip"
android:text="取消"
android:textColor="#000000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
欢迎登录界面
<RelativeLayout 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: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=".WelcomeActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到欢迎界面" />
</RelativeLayout>
*/主界面代码
package com.example.logindenglu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private View view;
private EditText inputpassword;
private EditText inputusername;
private CheckBox jzpw;
private CheckBox jzl;
private Button login;
SharedPreferences sp;
private String userNameValue;
private String passwordValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = View.inflate(this, R.layout.activity_main, null);
setContentView(view);
inputpassword = (EditText) view.findViewById(R.id.password);
inputusername = (EditText) view.findViewById(R.id.username);
jzpw = (CheckBox) view.findViewById(R.id.jizhumima);
jzl = (CheckBox) view.findViewById(R.id.jizhulogin);
login = (Button) findViewById(R.id.login);
sp = this.getSharedPreferences("userinfo", Context.MODE_WORLD_READABLE);
// 判断记住密码多选框的状态
if (sp.getBoolean("ISCHECK",false)) {
// 设置默认是记录密码状态
jzpw.setChecked(true);
inputpassword.setText(sp.getString("USER_NAME", ""));
inputusername.setText(sp.getString("PASSWORD", ""));
// 判断自动登陆多选框状态
if (sp.getBoolean("AUTO_ISCHECK",false)) {
// 设置默认是自动登录状态
jzl.setChecked(true);
// 跳转界面
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
MainActivity.this.startActivity(intent);
}
}
// 登录监听事件 现在默认为用户名为:liu 密码:123
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userNameValue = inputusername.getText().toString();
passwordValue = inputpassword.getText().toString();
if (userNameValue.equals("liu") && passwordValue.equals("123")) {
Toast.makeText(MainActivity.this, "登录成功",
Toast.LENGTH_SHORT).show();
// 登录成功和记住密码框为选中状态才保存用户信息
if (jzpw.isChecked()) {
// 记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
editor.commit();
}
// 跳转界面
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
MainActivity.this.startActivity(intent);
// finish();
} else {
Toast.makeText(MainActivity.this, "用户名或密码错误,请重新登录",
Toast.LENGTH_LONG).show();
}
}
});
// 监听记住密码多选框按钮事件
jzpw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (jzpw.isChecked()) {
System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();
} else {
System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
// 监听自动登录多选框事件
jzl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (jzl.isChecked()) {
System.out.println("自动登录已选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
} else {
System.out.println("自动登录没有选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
}
}
*正在登录代码
package com.example.logindenglu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class LoginActivity extends Activity {
private ProgressBar progressBar;
private Button backButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
progressBar = (ProgressBar) findViewById(R.id.pgBar);
backButton = (Button) findViewById(R.id.btn_back);
Intent intent = new Intent(this, WelcomeActivity.class);
LoginActivity.this.startActivity(intent);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
//欢迎登录界面
package com.example.logindenglu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.welcome, menu);
return true;
}
}
- Android 自动登录
- android开机自动登录
- Android用户自动登录
- android自动登录界面
- Android自动登录界面
- Android自动登录界面
- Android用户自动登录
- Android自动登录
- android自动登录
- Android用户自动登录
- android 自动登录
- android 自动登录
- Android 自动登录
- 自动登录(android)
- android应用第二次登录实现自动登录
- android自动登录已知WIFI
- Android记住密码自动登录
- android自动登录的实现
- jquery 操作cookie
- JavaScript强化教程 —— Cocos2d-JS游戏快速接入微信JS-SDK
- 23.2.0 bug集合
- 为何Eclipse的调试快捷键F8失效?
- android 加载gif
- android 自动登录
- 探讨Android开发值得注意的规范
- XML解析——Dom4j
- ondraw() 和dispatchdraw()的区别
- HDU 5831 Rikka with Parenthesis II(机智)
- 使用纯前端JavaScript 实现Excel IO
- 浅谈JSON和JSONP
- 使用二进制进行BFS Flip Game
- UITableView实现多选行