Android实现记住密码自动登录

activity_main.xml:

<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"
    tools:context="com.example.login.MainActivity" 
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号:" />
    <EditText 
        android:id="@+id/main_et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:" />
<EditText 
   android:id="@+id/main_et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        />
<CheckBox 
   android:id="@+id/main_cb1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="记住密码"  
        android:textColor="#000000"
   />
<CheckBox 
   android:id="@+id/main_cb2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="自动登录"  
        android:textColor="#000000"
   />
<Button 
   android:id="@+id/main_btn"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="登录"
   />
</LinearLayout>

MainActivity:

public class MainActivity extends Activity {
private EditText et1,et2;
private CheckBox cb1,cb2;
private Button btn;
private SharedPreferences sp;
private String username,password;
@SuppressLint("WorldReadableFiles")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidget();
/*Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。*/
sp = this.getSharedPreferences("userinfo", Context.MODE_WORLD_READABLE);
//判断记住密码的多选框状态,默认为false
if(sp.getBoolean("ISCHECK", false)){
cb1.setChecked(true);
et1.setText(sp.getString("USER_NAME", ""));  
       et2.setText(sp.getString("PASSWORD", ""));  
       //判断自动登录选择框状态
       if(sp.getBoolean("AUTO_LOGIN", false)){
        //设置默认是自动登录状态  
                cb2.setChecked(true);  
                //跳转界面  
                Intent intent = new Intent(MainActivity.this,LoadingActivity.class);  
                MainActivity.this.startActivity(intent); 
       }
}
//记住密码多选框按钮事件
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb1.isChecked()){
                    sp.edit().putBoolean("ISCHECK", true).commit();  
}else{
sp.edit().putBoolean("ISCHECK", false).commit();  
}
}
});
//记住自动登录多选框按钮事件
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cb2.isChecked()){
                    sp.edit().putBoolean("AUTO_LOGIN", true).commit();  
}else{
sp.edit().putBoolean("AUTO_LOGIN", false).commit();  
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
username = et1.getText().toString().trim();
password = et2.getText().toString().trim();
if(username.equals("1234") && password.equals("1234")){
Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();  
//登录成功和记住密码框为选中状态才保存用户信息  
                    if(cb1.isChecked()){  
                        //记住用户名、密码、  
                        Editor editor = sp.edit();  
                        editor.putString("USER_NAME", username);  
                        editor.putString("PASSWORD",password);  
                        editor.commit();  
                    }  
                    //跳转界面  
                    Intent intent = new Intent(MainActivity.this,LoadingActivity.class);  
                    MainActivity.this.startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"用户名或密码输入错误", Toast.LENGTH_SHORT).show();  
}
}
});
}
//初始化控件
private void initWidget(){
et1 = (EditText) findViewById(R.id.main_et1);
et2 = (EditText) findViewById(R.id.main_et2);
cb1 = (CheckBox) findViewById(R.id.main_cb1);
cb2 = (CheckBox) findViewById(R.id.main_cb2);
btn = (Button) findViewById(R.id.main_btn);
}
}

activity_loading.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" >  
    <RelativeLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"   
        android:layout_weight="3">  
        <ProgressBar  
            android:id="@+id/loading_pb"  
            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/loading_pb"  
            android:layout_centerHorizontal="true"  
            android:text="正在登录..."  
            android:textColor="#000000"  
            android:textSize="18sp" />  
    </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/loading_btn"  
            android:layout_width="70dip"  
            android:layout_height="35dip"  
            android:text="取消"  
            android:textColor="#000000"  
            android:textSize="12sp" />  
    </LinearLayout>  
</LinearLayout>

LoadingActivity:

public class LoadingActivity extends Activity {
private Button btn;
private UIHandler uIHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
        btn = (Button) findViewById(R.id.loading_btn);  
        btn.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                finish();  
            }  
        }); 
        uIHandler = new UIHandler();
        UIThread uIThread = new UIThread();
        uIThread.start();
}
@SuppressLint("HandlerLeak")
class UIHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Intent intent = new Intent(LoadingActivity.this, WelcomeAvtivity.class);  
       LoadingActivity.this.startActivity(intent);
       finish();
}
};
private class UIThread extends Thread{  
        @Override  
        public void run() {  
            try {  
                Thread.sleep(3000);  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }  
            Message msg = new Message();  
            LoadingActivity.this.uIHandler.sendMessage(msg);  
        }  
    }
}

activity_welcome_avtivity.xml:

<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"
    tools:context="${relativePackage}.${activityClass}" >
    <TextView
        android:id="@+id/welcome_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button 
        android:id="@+id/welcome_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/welcome_tv"
        android:text="注销登录"
        android:onClick="click"
        />
</RelativeLayout>

WelcomeAvtivity:

public class WelcomeAvtivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_avtivity);
}
@SuppressLint("WorldReadableFiles")
public void click(View v) {
if(v.getId() == R.id.welcome_btn){
            finish();
}
}
}

界面预览图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值