android 自动登录和记住密码

运行效果图如下 :

一、布局文件

 1、mian.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/blue"
    >
    <TextView
    android:layout_height="50dp"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:textSize="24sp"
    android:text="登录界面"
    android:textColor="@color/white"
    android:background="@drawable/song_index_item_bg"/>
    <ImageView
    android:id="@+id/iv"
    android:layout_height="220dp"
    android:layout_width="230dp"
    android:layout_marginTop="110dp"
    android:src="@drawable/background"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY"/>
    <TextView
    android:id="@+id/tv_name"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="15sp"
    android:layout_marginTop="7dp"
    android:text="账号:"
    android:layout_alignLeft="@+id/iv"
    android:layout_alignTop="@+id/iv"
    android:layout_marginLeft="7dp"
    android:textColor="@color/green"/>
    <EditText
    android:id="@+id/et_name"
    android:layout_height="25dp"
    android:layout_width="210dp"
    android:layout_marginTop="7dp"
    android:layout_below="@+id/tv_name"
    android:background="@drawable/shape"
    android:layout_alignLeft="@+id/tv_name"/>
    <TextView
    android:id="@+id/tv_pass"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textSize="15sp"
    android:text="密码:"
    android:layout_alignLeft="@+id/tv_name"
    android:layout_below="@+id/et_name"
    android:layout_marginTop="7dp"
    android:textColor="@color/green"/>
    <EditText
    android:id="@+id/et_pass"
    android:layout_height="25dp"
    android:layout_width="210dp"
    android:background="@drawable/shape"
    android:layout_marginTop="7dp"
    android:layout_below="@+id/tv_pass"
    android:layout_alignLeft="@+id/tv_name"/>
    <CheckBox
    android:id="@+id/isremenber"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignLeft="@+id/tv_name"
    android:layout_marginTop="10dp"
    android:text="记住密码"
    android:textSize="11sp"
    android:textColor="@color/huise"
    android:layout_below="@+id/et_pass"
    android:button="@drawable/mycheckbox"/>
    <CheckBox
    android:id="@+id/isloginself"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignRight="@+id/et_name"
    android:layout_marginTop="10dp"
    android:text="自动登录"
    android:textSize="11sp"
    android:textColor="@color/huise"
    android:layout_below="@+id/et_pass"
    android:button="@drawable/mycheckbox"/>
    <Button
    android:id="@+id/longin"
    android:layout_height="25dp"
    android:layout_width="210dp"
    android:layout_below="@+id/isloginself"
    android:layout_alignLeft="@+id/et_pass"
    android:layout_marginTop="10dp"
    android:text="登录"
    android:gravity="center"
    android:background="@drawable/loginbtn"/>
</RelativeLayout>
2、show.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/white">
  <TextView
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:text="登录成功!"
  android:textSize="40sp"
  android:gravity="center"
  android:background="@drawable/chat_bg_default"/>
   
</LinearLayout>

3、colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue">#3FA1E2</color>
    <color name="huise">#3F3E38</color>
    <color name="yellow">#FFA500</color>
    <color name="wuse">#0000</color>
    <color name="heise">#030303</color>
    <color name="blue1">#90CEF7</color>
    <color name="blue2">#0707C6</color>
    <color name="red">#F70938</color>
    <color name="green">#316424</color>
    <color name="white">#fff</color>
</resources>
4、mycheckbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_checked="true" android:drawable="@drawable/checkbox_pressed"></item>
    <item  android:state_checked="false" android:drawable="@drawable/checkbox"></item>
    <item android:drawable="@drawable/checkbox"></item>
</selector>
5、shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
  xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/> 
    <corners android:radius="8px"/> 
    <stroke android:color="#3FA1E2" 
       android:width="1px" 
        /> 
   
</shape>
下面是主要的java代码

1、LoginActivity.java

package zzz.agldtx.cc;

import android.app.Activity;
import android.app.ProgressDialog;
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.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;


public class LonginActivity extends Activity {
 private EditText name;
 private EditText pass;
 private CheckBox isRemenber;
 private CheckBox isLoginSelf;
 private Button longin;
 private ProgressDialog mDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        name=(EditText)findViewById(R.id.et_name);
        pass=(EditText)findViewById(R.id.et_pass);
        isRemenber=(CheckBox)findViewById(R.id.isremenber);
        isLoginSelf=(CheckBox)findViewById(R.id.isloginself);
        longin=(Button)findViewById(R.id.longin);
       
        final SharedPreferences pre=getSharedPreferences("longinvalue", MODE_WORLD_WRITEABLE);
        if(pre!=null){
         //记住了密码
           if(pre.getBoolean("isrmb", false)==true){
            name.setText(pre.getString("name", null));
            pass.setText(pre.getString("pass", null));
            isRemenber.setChecked(true) ;
           }
           if(pre.getBoolean("islgs", false)==true){
            isLoginSelf.setChecked(true);
            creatDialog();
            new Thread(){
             public void run() {
              try {

      Thread.sleep(3000);
            if(mDialog.isShowing()){
           mDialog.dismiss();
          }
      Intent intent2=new Intent(LonginActivity.this,Show.class);
      startActivity(intent2);
     } catch (Exception e) {
      // TODO: handle exception
     }
             }
            }.start();
           }
        }
        isRemenber.setOnCheckedChangeListener( new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
         if(isRemenber.isChecked()==false){
          isLoginSelf.setChecked(false);
         }
   }
  });
        isLoginSelf.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
      isRemenber.setChecked(true);
   }
  });
        longin.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(!name.getText().toString().equals("")&&!pass.getText().toString().equals("")){
     if(isLoginSelf.isChecked()){
      pre.edit().putBoolean("isrmb", true).putBoolean("islgs", true).putString("name", name.getText().toString())
      .putString("pass", pass.getText().toString()).commit();
     }else{
      if(isRemenber.isChecked()){
       pre.edit().putBoolean("isrmb", true).putBoolean("islgs", false).putString("name", name.getText().toString())
       .putString("pass", pass.getText().toString()).commit();
      }else{
       pre.edit().putBoolean("isrmb", false).putBoolean("islgs", false).putString("name", name.getText().toString())
       .putString("pass", pass.getText().toString()).commit();
      }
     }
     Intent intent=new Intent(LonginActivity.this,Show.class);
     startActivity(intent);
    }else{
     Toast.makeText(getApplicationContext(), "密码或账号不能为空!", Toast.LENGTH_LONG).show();
    }

   }
  });
    }
    private void creatDialog() {
  // TODO Auto-generated method stub
       mDialog=new ProgressDialog(this);
       mDialog.setTitle("验证中");
       mDialog.setMessage("正在登陆请稍后");
       mDialog.setIndeterminate(true);
       mDialog.setCancelable(true);
       mDialog.show();
     
 }
}

2、show.java

package zzz.agldtx.cc;

import android.app.Activity;
import android.os.Bundle;

public class Show extends Activity{
  @Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.show);
}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值