`

记住密码跟自动登录的设置

 
阅读更多

自动登录跟记住密码

 

 

首先定义一个登陆页面的ActivityLoginActivity

 

写道
package cn.hwttnet.com.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class LoginActivity extends Activity {
private EditText username, pass, url;
CheckBox remberPass, autoLogin;
Button login;
SharedPreferences sp;
Editor ed;
ImageButton ins;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) findViewById(R.id.et_username);
pass = (EditText) findViewById(R.id.et_password);
url = (EditText) findViewById(R.id.et_url);
remberPass = (CheckBox) findViewById(R.id.remeber);
autoLogin = (CheckBox) findViewById(R.id.autoLogin);
login = (Button) findViewById(R.id.login);
ins = (ImageButton) findViewById(R.id.shuoming);
ins.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent3 = new Intent(LoginActivity.this,
InstrutionActivity.class);
startActivity(intent3);
}
});
sp = getSharedPreferences("users", MODE_WORLD_READABLE);
ed = sp.edit();
// 从SharedPreferences里边取出 记住密码的状态
if (sp.getBoolean("ISCHECK", false)) {
// 将记住密码设置为被点击状态
remberPass.setChecked(true);
// 然后将值赋值给EditText
username.setText(sp.getString("oa_name", ""));
pass.setText(sp.getString("oa_pass", ""));
url.setText(sp.getString("oa_url", ""));
// 获取自动登录按钮的状态
if (sp.getBoolean("AUTO_ISCHECK", false)) {
// 设置自动登录被点击 然后实现跳转
autoLogin.setChecked(true);
Intent intent1 = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent1);
}
}
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LoginMain();
}
});
// 将点击的checkBOx存入到users中
remberPass.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked1 = remberPass.isChecked();
ed.putBoolean("ISCHECK", isChecked1);
ed.commit();
}
});
// 设置自动登录默认为不点击
Boolean value1 = sp.getBoolean("AUTO_ISCHECK", false);
autoLogin.setChecked(value1);
autoLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean isChecked2 = autoLogin.isChecked();
ed.putBoolean("AUTO_ISCHECK", isChecked2);
ed.commit();
}
});
// 如果记住密码跟自动登录都被选中就选择登录跳转
if (remberPass.isChecked() && autoLogin.isChecked()) {
LoginMain();
}
}

protected void LoginMain() {
// TODO Auto-generated method stub
// 将信息存入到users里面
ed.putString("oa_name", username.getText().toString());
ed.putString("oa_pass", pass.getText().toString());
ed.putString("oa_url", url.getText().toString());
ed.commit();
if (TextUtils.isEmpty(username.getText().toString())) {
Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass.getText().toString())) {
Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(url.getText().toString())) {
Toast.makeText(this, "请输入连接地址", Toast.LENGTH_LONG).show();
return;
}

Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);

}
}

 定义一个登录界面的XML  login.xml

<?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/background"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/shuoming"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="250dip"
            android:layout_marginTop="40dip"
            android:src="@drawable/shuoming" />

        <ImageView
            android:id="@+id/image_logo"
            android:layout_width="250dip"
            android:layout_height="150dip"
            android:paddingLeft="8dip"
            android:paddingRight="8dip"
            android:src="@drawable/logo" />

        <TableLayout
            android:id="@+id/table01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:collapseColumns="4"
            android:stretchColumns="1" >

            <TableRow
                android:id="@+id/tableow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:paddingLeft="10dip"
                    android:textColor="#000000"
                    android:id="@+id/tv_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="用户名" />

                <EditText
                    android:id="@+id/et_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>

            <TableRow
                android:id="@+id/tableow2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                      android:paddingLeft="10dip"
                    android:textColor="#000000"
                    android:id="@+id/tv_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="密码" />

                <EditText
                    android:id="@+id/et_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>

            <TableRow
                android:textColor="#000000"
                android:id="@+id/tableow3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                      android:paddingLeft="10dip"
                     android:textColor="#000000"
                    android:id="@+id/tv_url"
                    android:layout_height="wrap_content"
                    android:text="地址" />

                <EditText
                    android:id="@+id/et_url"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>
        </TableLayout>

        <LinearLayout
            android:id="@+id/tableLayout2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TableRow
                android:id="@+id/tableow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <CheckBox
                    android:textColor="#000000"
                    android:id="@+id/remeber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="40dip"
                    android:text="记住密码" />

                <CheckBox
                    android:textColor="#000000"
                    android:id="@+id/autoLogin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="自动登录" />
            </TableRow>
        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/login"
        android:layout_width="180dip"
        android:layout_height="wrap_content"
         android:layout_marginLeft="80dip"
        android:text="登录" />

</LinearLayout>

 

以上就实现了登录得记住密码,跟自动登录的实现

分享到:
评论

相关推荐

    Android记住密码和自动登录实现

    在Android应用开发中,提供“记住密码”和“自动登录”的功能是提升用户体验的重要一环。用户在登录应用后,可以选择保存其凭证,以便下次打开应用时能够自动填充用户名和密码,无需再次输入。在Android 2.3.3及更高...

    Android自动记住密码和自动登录源码

    在Android开发中,"自动记住密码和自动登录"是一个常见的功能,它极大地方便了用户在使用应用时的登录流程。这个源码提供了一个实现这一功能的实例,可以帮助开发者理解和学习如何在Android应用中实现这一特性。下面...

    android记住密码自动登录简单demo

    这个"android记住密码自动登录简单demo"旨在为新手Android开发者提供一个基础的实现方案。下面我们将详细探讨如何在Android应用中实现这一功能。 首先,我们需要理解记住密码的基本原理。通常,用户在登录界面勾选...

    android记住密码自动登录实例

    在Android应用开发中,"记住密码"和"自动登录"是常见的用户便利功能,它能够提升用户体验,使得用户在下次打开应用时无需每次都输入用户名和密码。本实例将重点讲解如何利用SharedPreferences这一轻量级的数据存储...

    asp.net 记住密码 自动登录

    在ASP.NET开发中,"记住密码"和"自动登录"功能是提高用户体验的重要特性,尤其在用户频繁访问网站时。本教程将详细讲解如何利用Visual Studio 2010实现这些功能。 首先,我们需要理解ASP.NET的身份验证机制。默认...

    android记住密码自动登录源码

    在Android开发中,"记住密码...总的来说,实现"android记住密码自动登录源码"涉及的核心技术有:SharedPreferences的使用、用户界面交互以及登录验证流程。理解这些知识点可以帮助开发者创建更加便捷和用户友好的应用。

    Android 记住密码和自动登录界面的实现

    "Android记住密码和自动登录界面的实现"这个主题聚焦于提供一种方便、安全的方式,使用户能够快速登录应用,从而提高用户体验。这一过程通常涉及到SharedPreferences的使用,它是一个轻量级的数据存储机制,用于保存...

    android记住密码和自动登录功能源码

    在Android应用开发中,"记住密码"和"自动登录"是常见的用户友好功能,它们能够提升用户体验,使得用户在多次登录后不必重复输入账号和密码。本教程将深入讲解如何在Android客户端实现这两个功能,同时结合一个具有...

    C# winform记住密码及自动登陆源码

    在C# WinForm应用开发中,实现“记住密码”和“自动登录”的功能是一项常见的需求。这个源码示例提供了一种实现方式,其设计灵感来源于QQ客户端的用户登录界面,旨在帮助开发者学习如何在自己的应用程序中集成这些...

    Android自动登录及记住密码

    在Android应用开发中,实现自动登录和记住密码功能是提高用户体验的重要一环。这个功能使得用户在下次打开应用时无需每次都输入账号和密码,极大地节省了用户的时间。下面将详细介绍如何在Android环境中实现这一功能...

    C# 记住密码功能

    如果验证通过,就可以自动登录用户,而无需再次输入密码。 然而,记住密码功能也存在潜在风险。例如,如果用户的计算机被他人使用,他们可能不希望密码被自动填充。因此,开发者应当提供一个选项,让用户能够轻松地...

    c#自动登录和记住密码

    在C#编程中,自动登录和记住密码功能是常见的用户交互设计,主要目的是为了提高用户体验,使得用户在多次登录同一应用或网站时无需反复输入账号信息。本篇将详细讲解如何利用C#来实现这一功能,同时结合 ini 文件...

    Android中记住密码与自动登录

    在Android应用开发中,"记住密码"和"自动登录"是常见的用户便利功能,它们能够提升用户体验,使得用户在下次打开应用时无需每次都输入用户名和密码。本文将深入探讨如何在Android中实现这一功能。 首先,我们需要...

    安卓shareprefrence实现记住密码自动登录功能

    在Android应用开发中,"安卓shareprefrence实现记住密码自动登录功能"是一个常见的需求,它涉及到用户登录状态的持久化存储以及数据安全。SharePreferences是Android系统提供的一种轻量级的数据存储方式,常用于存储...

    java cookie 读写,记住密码 自动登录

    本主题将详细讲解如何使用Java进行Cookie的读写操作,以及如何利用Cookie实现记住密码和自动登录功能。 1. **Cookie基本概念** Cookie是由服务器发送到用户浏览器并存储在本地的一小块数据,当用户再次请求同一...

    jsp,自动登录,记住密码

    在IT行业中,"jsp,自动登录,记住密码"是一个常见的功能需求,特别是在Web应用程序开发中。JSP(JavaServer Pages)是一种动态网页技术,它允许开发者在HTML页面中嵌入Java代码,从而实现服务器端的逻辑处理。在这...

    Jsp+Servlet+Cookie实现记住密码,自动登录,防止表单提交,记录登录次数

    在这个实例中,我们将深入探讨如何利用这些技术实现记住密码、自动登录、防止表单重复提交以及记录登录次数的功能。 首先,让我们从"记住密码"功能开始。在用户登录时,如果用户勾选了“记住我”的选项,系统会将...

    安卓登录注册相关-Android条件判断用户名和密码登录跳转主页面记住密码自动登录.zip

    4. **记住密码和自动登录**:此功能通过SharedPreferences实现。SharedPreferences是Android提供的轻量级数据存储方式,用于保存简单的键值对数据,如用户名和加密后的密码。用户选择“记住密码”时,登录信息会被...

    c# 登录保存密码 自动登录

    在C#编程中,实现登录保存密码和自动登录功能是一项常见的需求,特别是在开发桌面应用程序或者Web应用程序时。这个实例化保存方式通常涉及到用户凭据的安全存储,以便在用户下次启动应用时能自动填充登录信息,提供...

Global site tag (gtag.js) - Google Analytics