您的位置:首页 > 其它

logi登入可记住密码并且显示登入成功

2013-09-12 11:20 351 查看
logi登入可记住密码并且显示登入成功
package com.mxj.login;
importandroid.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.text.Editable;

import android.text.InputType;

import android.text.TextWatcher;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.Toast;
public classLogin2Activity extends Activity {

 

 private AutoCompleteTextView user =  null;

 private EditText password = null;

 private CheckBox checkbox = null;

 private Button login = null;

 

 private SharedPreferences sp = null;

 private String userStr;

 private String passwordStr;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        user =(AutoCompleteTextView)super.findViewById(R.id.user);

        password =(EditText)super.findViewById(R.id.password);

        checkbox =(CheckBox)super.findViewById(R.id.checkbox);

        login = (Button)super.findViewById(R.id.login);

       

       

        sp =this.getSharedPreferences("pwdFile", MODE_PRIVATE);

  this.checkbox.setChecked(true);// 默认记住密码

  this.user.setThreshold(1);// 输入一个字母就开始提示

  password.setInputType(InputType.TYPE_CLASS_TEXT

    | InputType.TYPE_TEXT_VARIATION_PASSWORD);// 隐藏密码获显示密码
  user.addTextChangedListener(newTextWatcher() {
   publicvoid onTextChanged(CharSequence s, int start, int before,

     int count) {

    // TODO Auto-generated method stub

    String[] allUserName = new String[sp.getAll().size()];//sp.getAll().size()返回的是有多少个键值对

    allUserName = sp.getAll().keySet().toArray(newString[0]);

    // sp.getAll()返回一张hash map

    // keySet()得到的是a set of the keys.

    // hash map是由key-value组成的

    ArrayAdapter<String> adapter = newArrayAdapter<String>(

      Login2Activity.this,

      android.R.layout.simple_dropdown_item_1line,

      allUserName);

    user.setAdapter(adapter);// 设置数据适配器适配器

   }
   

   public void beforeTextChanged(CharSequence s, int start, intcount,

     int after) {

    // TODO Auto-generated method stub
   }
   

   public void afterTextChanged(Editable s) {

    // TODO Auto-generated method stub

    password.setText(sp.getString(user.getText().toString(),""));// 自动输入密码
   }

  });

  // 登录

  login.setOnClickListener(new OnClickListener() {
   publicvoid onClick(View v) {

    // TODO Auto-generated method stub

    userStr = user.getText().toString();

    passwordStr = password.getText().toString();

    if (!((userStr.equals("123")) &&(passwordStr.equals("123")))) {

     Toast.makeText(Login2Activity.this, "密码错误,请重新登录",

       Toast.LENGTH_SHORT).show();

    } else {

     if (checkbox.isChecked()) {

      sp.edit().putString(userStr,passwordStr).commit();

     }

     Toast.makeText(Login2Activity.this, "登录成功,正在获取用户数据……",

       Toast.LENGTH_SHORT).show();

    }

   }

  });

 }
}
-------------------------------------------------------------------------------------------------万恶的下划线--------------------------------------------------------------------------------------------------------------------
 
 
<main.xml>
 
<?xmlversion="1.0" encoding="utf-8"?>

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >
   <TextView

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:gravity="center_horizontal" 

        android:textSize="25sp"

        android:text="登录界面" />

   

    <LinearLayout

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:orientation="horizontal">

       

        <TextView

           android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:textSize="20sp"

           android:text="帐号:"/>

        <AutoCompleteTextView

           android:id="@+id/user"

           android:layout_width="200dp"

           android:layout_height="wrap_content"

           android:hint="请输入帐号..."/>

    </LinearLayout>

   

    <LinearLayout

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:orientation="horizontal">

       

        <TextView

            android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:textSize="20sp"

           android:text="密码:"/>

        <EditText

           android:id="@+id/password"

           android:layout_width="200dp"

           android:layout_height="wrap_content"

           android:hint="请输入密码..."/>

    </LinearLayout>

   

    <LinearLayout

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:gravity="center_horizontal"

       android:orientation="horizontal">

       

        <CheckBox

           android:id="@+id/checkbox"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="记住密码"/>

       

        <Button

           android:id="@+id/login"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="登录"/>

    </LinearLayout>

   

< /LinearLayout>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐