Android自定义控件实现登陆界面以及SharedPreferences实现记住密码功能

1.自定义控件

通过继承LinearLayout来实现一个自定义控件,左边为放置图标的ImageView,右边为输入账号或密码的EditText。

布局文件myedittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/imageView" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/editText"
        android:maxLength="13" />
</LinearLayout>

类MyEditText.java

package com.example.ba1.chat;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by b a 1 on 2015/6/27.
 */
public class MyEditText extends LinearLayout {
    private ImageView imageView=null;
    private EditText editText=null;
    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.mytextview, this);//设置布局文件
        imageView=(ImageView)findViewById(R.id.imageView);
        editText=(EditText)findViewById(R.id.editText);
    }
    public void setHint(String s){
        editText.setHint(s);
    }
    public void setBackground(int resId) {
        imageView.setImageResource(resId);
    }
    public Editable getText(){
        return editText.getText();
    }
    public void setInputType(int a){
        editText.setInputType(a);
    }
    public void setText(String s){
        editText.setText(s);
    }
}

2.SharedPreferences实现记住密码

登录Activity.java

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;


public class login extends BaseActivity{
    private MyEditText username=null;
    private MyEditText password=null;
    private Button log=null;
    private Button register=null;
    private CheckBox checkBox=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username=(MyEditText)findViewById(R.id.username);
        password=(MyEditText)findViewById(R.id.password);
        username.setBackground(R.drawable.user_name);
        password.setBackground(R.drawable.password);
        log=(Button)findViewById(R.id.log);
        register=(Button)findViewById(R.id.register);
        checkBox=(CheckBox)findViewById(R.id.checkBox);
        username.setHint("请输入账号");
        password.setHint("请输入密码");
        password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//将密码设置为password
        final SharedPreferences sharedPreferences= getPreferences(MODE_PRIVATE);
        username.setText(sharedPreferences.getString("username",""));
        password.setText(sharedPreferences.getString("password",""));
        if(sharedPreferences.getString("password","")!="")
            checkBox.setChecked(true);
        log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkBox.isChecked()){
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("username",username.getText().toString());
                    editor.putString("password",password.getText().toString());
                    editor.apply();
                }
                else{
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("username",username.getText().toString());
                    editor.putString("password","");
                    editor.apply();
                }
            }
        });
    }
}

布局文件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" android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".login"
    android:background="@drawable/back">

    <com.example.ba1.chat.MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/username"
        android:layout_marginTop="85dp"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/password"></com.example.ba1.chat.MyEditText>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/log"
        android:id="@+id/log"
        android:background="#ff3d7cff"
        android:layout_marginTop="68dp"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/register"
        android:id="@+id/register"
        android:background="#ff3d7cff"
        android:layout_marginTop="41dp"
        android:layout_below="@+id/log"
        android:layout_alignStart="@+id/log" />

    <com.example.ba1.chat.MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        android:layout_below="@+id/username"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"></com.example.ba1.chat.MyEditText>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/checkBox"
        android:layout_below="@+id/password"
        android:layout_alignEnd="@+id/log" />


</RelativeLayout>
效果图:

3.SharedPreferences中commit()和apply()的区别(转载的)

1. apply没有返回值而commit返回boolean表明修改是否提交成功 
2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步的提交到硬件磁盘,因此,在多个并发的提交commit的时候,他们会等待正在处理的commit保存到磁盘后在操作,从而降低了效率。而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,这样从一定程度上提高了很多效率。 
3. apply方法不会提示任何失败的提示。 
由于在一个进程中,sharedPreference是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,建议使用apply,当然需要确保提交成功且有后续操作的话,还是需要用commit的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值