Android 实现记住用户名和密码的功能

Android 实现记住用户名和密码的功能

结果演示:
技术分享

源代码下载地址:

https://github.com/GXS1225/Android————-.git

分析

(1)判断是否输入了账号和密码

    if(name.trim().equals("")){
                Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show();
                return;
            }
            if(pswd.trim().equals("")){
                Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show();
                return;
            }

(2)在layout_main.xml定义一个 CheckBox,进行判断

boolean CheckBoxLogin = checkbox.isChecked();
            if (CheckBoxLogin)
            {
                    Editor editor = sp.edit();
                    editor.putString("uname", name);
                    editor.putString("upswd", pswd);
                    editor.putBoolean("auto", true);
                    editor.commit();
            }
            else
            {  
                Editor editor = sp.edit();
                editor.putString("uname", null);
                editor.putString("upswd", null);
                editor.putBoolean("auto", false);
                editor.commit();
                }

步骤

先写一个登陆的界面: layout_main.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:orientation="vertical" 
    android:background="#ADD8E6">

    <RelativeLayout
        android:id="@+id/login_div"
        android:layout_width="fill_parent"
        android:layout_height="221dp"
        android:layout_margin="15dip"
        android:background="@drawable/btn_bg"
        android:padding="15dip" >

        <TextView
            android:id="@+id/login_user_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_margin="5dp"
            android:text="@string/user"
            android:textSize="16dp"
            android:typeface="sans" />

        <EditText
            android:id="@+id/user_input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/login_user_input"
            android:background="@android:drawable/editbox_background"
            android:inputType="text"
            android:singleLine="true" />

        <TextView
            android:id="@+id/login_pass_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/user_input"
            android:layout_margin="5dp"
            android:text="@string/pass"
            android:textSize="16dp"
            android:typeface="sans" />

        <EditText
            android:id="@+id/pass_input"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/login_pass_input"
            android:background="@android:drawable/editbox_background"
            android:inputType="textPassword"
            android:singleLine="true" />

        <CheckBox
            android:id="@+id/checkBoxLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/pass_input"
            android:layout_alignParentBottom="true"
            android:text="@string/no_user" />

        <Button
            android:id="@+id/new_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/pass_input"
            android:layout_marginRight="28dp"
            android:onClick="To_Title"
            android:text="@string/new_user" />

    </RelativeLayout>

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical" >

         <TextView
             android:layout_width="402dp"
             android:layout_height="51dp"
             android:layout_marginLeft="50dp"
             android:background="@drawable/gxs_ziti" />

         <Button
             android:layout_width="120dp"
             android:layout_height="120dp"
             android:onClick="To_fruist"
             android:background="@drawable/gxs2" 
             android:layout_marginLeft="80dp"
             />



     </LinearLayout>

</LinearLayout>

Welcome.java

package com.gxs.login;
import com.example.login.R;
import com.gxs.listview.*;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Welcome extends Activity implements OnClickListener{
    private EditText uname = null;
    private EditText upswd = null;
    private CheckBox checkbox = null;
    private Button login = null;
    SharedPreferences sp = null; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        init();

    }

    public void init()
    {
        uname = (EditText) findViewById(R.id.user_input);
        upswd = (EditText) findViewById(R.id.pass_input);
        checkbox = (CheckBox) findViewById(R.id.checkBoxLogin);
        login = (Button) findViewById(R.id.new_user);
        if (sp.getBoolean("auto", false))
            {
                uname.setText(sp.getString("uname", null));
                upswd.setText(sp.getString("upswd", null)); 
                checkbox.setChecked(true);

            }
        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == login){
            String name = uname.getText().toString();
            String pswd = upswd.getText().toString();
            if(name.trim().equals("")){
                Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show();
                return;
            }
            if(pswd.trim().equals("")){
                Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show();
                return;
            }
            boolean autoLogin = checkbox.isChecked();
            if (autoLogin)
            {
                    Editor editor = sp.edit();
                    editor.putString("uname", name);
                    editor.putString("upswd", pswd);
                    editor.putBoolean("auto", true);
                    editor.commit();
            }
            else
            {  
                Editor editor = sp.edit();
                editor.putString("uname", null);
                editor.putString("upswd", null);
                editor.putBoolean("auto", false);
                editor.commit();
                }
            //Intent跳转
            Intent intent = new Intent(Welcome.this,Content.class);
            startActivity(intent);
            finish();
        }


    }

}

Content.java

package com.gxs.listview;
import java.util.List;
import com.example.login.R;
import com.gxs.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Content extends Activity{
    private ListView listview_fruits;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content);

    }
}

content.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"
    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=".Welcome" 
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="内容"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。