标签:
你不能左右天气,但可以改变心情。你不能改变容貌,但可以掌握自己。你不能预见明天,但可以珍惜今天。
本讲内容:记住密码和自动登录界面的实现
示例效果图
下面是res/layout/activity_login.xml
布局文件:(登录介面)
<?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="match_parent"
android:background="@drawable/login" >
<ImageButton
android:id="@+id/img_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/quit" />
<TextView
android:id="@+id/tv_zh"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:gravity="bottom"
android:text="帐号:"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_zh"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/tv_zh"
android:layout_marginLeft="15dp"
android:layout_marginRight="12dp" />
<TextView
android:id="@+id/tv_pws"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_below="@+id/et_zh"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:gravity="bottom"
android:text="密码:"
android:textColor="#000000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_pws"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/tv_pws"
android:layout_marginLeft="15dp"
android:layout_marginRight="12dp" />
<CheckBox
android:id="@+id/cb_pws"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_pws"
android:layout_marginLeft="15dp"
android:text="记住密码"
android:textColor="#000000" />
<CheckBox
android:id="@+id/cb_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_pws"
android:layout_marginLeft="15dp"
android:text="自动登录"
android:textColor="#000000" />
<Button
android:id="@+id/btn_login"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/cb_auto"
android:layout_below="@+id/et_pws"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="登录"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_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="20sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="@+id/btn_back"
android:layout_width="70dp"
android:layout_height="35dp"
android:text="取消"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="登陆成功,进入用户界面"
android:textColor="#ff00ff"
android:textSize="25sp" />
</LinearLayout>public class LoginActivity extends Activity{
private EditText userName,password;
private CheckBox rem_pw,auto_login;
private Button btn_login;
private ImageButton btnQuit;
private SharedPreferences sp;
private String userNameValue,passwordValue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题
setContentView(R.layout.activity_login);
initView();
initData();
}
private void initView() {
//获得实例对象
userName=(EditText) findViewById(R.id.et_zh);
password=(EditText) findViewById(R.id.et_pws);
rem_pw=(CheckBox) findViewById(R.id.cb_pws);
auto_login=(CheckBox) findViewById(R.id.cb_auto);
btn_login=(Button) findViewById(R.id.btn_login);
btnQuit=(ImageButton) findViewById(R.id.img_btn);
sp=this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
}
private void initData() {
//判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false)){
//设置默认是记录密码状态
rem_pw.setChecked(true);
userName.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
if(sp.getBoolean("AUTO_ISCHECH", false)){
//设置默认是自动登录状态
auto_login.setChecked(true);
//跳转界面
Intent intent=new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
}
}
// 登录监听事件 现在默认为用户名为:jin 密码:1230
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
userNameValue=userName.getText().toString();
passwordValue=password.getText().toString();
if(userNameValue.equals("jin")&&passwordValue.equals("1230")){
//Toast.makeText(LoginActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
//登录成功和记住密码框为选中状态才保存用户信息
if(rem_pw.isChecked()){
//记住用户名、密码
Editor editor=sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
editor.commit();
}
//跳转界面
Intent intent = new Intent(LoginActivity.this,LogoActivity.class);
LoginActivity.this.startActivity(intent);
//finish();
}else{
Toast.makeText(LoginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
});
//监听记住密码多选框按钮事件
rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(rem_pw.isChecked()){
Toast.makeText(LoginActivity.this,"记住密码已选中", Toast.LENGTH_LONG).show();
sp.edit().putBoolean("ISCHECK", true).commit();
}else{
Toast.makeText(LoginActivity.this,"记住密码没有选中", Toast.LENGTH_LONG).show();
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
//监听自动登录多选框事件
auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(auto_login.isChecked()){
Toast.makeText(LoginActivity.this,"自动登录已选中", Toast.LENGTH_LONG).show();
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
}else{
Toast.makeText(LoginActivity.this,"自动登录没有选中", Toast.LENGTH_LONG).show();
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
}
}
});
//删除事件(退出程序)
btnQuit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
}
}
public class LogoActivity extends Activity implements Runnable{
private ProgressBar progressBar;
private Button backButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_logo);
progressBar=(ProgressBar) findViewById(R.id.pgBar);
backButton=(Button) findViewById(R.id.btn_back);
progressBar.setMax(3000);
//启动一个延迟线程
new Thread(this).start();
backButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
public void run() {
try {
Thread.sleep(2000);//延迟两秒时间
startActivity(new Intent(this,WelcomeActivity.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class WelcomeActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
}Take
your time and enjoy it 要原码的、路过的、学习过的请留个言,顶个呗~~
标签:
原文地址:http://blog.csdn.net/liguojin1230/article/details/44749403