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