效果
本文的合集已经编著成书,高级Android开发强化实战,欢迎各位读友的建议和指导。在京东即可购买:https://item.jd.com/12385680.html
AndroidFavor是一个第三方库, 可以简化SharedPreferences, 使用依赖注入的方式声明. 我们根据常见的输入框, 把账户信息存在Favor的类中, 来看看这个库怎么用.
1. 准备
配置build.gradle
compile 'com.cocosw:favor:0.2.0'
compile 'com.jakewharton:butterknife:7.0.1'
布局, 输入账户和密码, 下面两行小字显示保存在SharedPreferences中的数据.
<LinearLayout
....>
<android.support.design.widget.TextInputLayout
android:id="@+id/user_name_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="textEmailAddress"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/password_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/commit_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="@color/colorAccent"
android:text="登录"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/user_name_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="username"/>
<TextView
android:id="@+id/password_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="password"/>
</LinearLayout>
使用TextInputLayout来绘制输入框.
页面逻辑, 验证邮箱和密码, 当输入正确时, 保存在SharedPreference里面, 并显示, 使用Favor库进行保存.
public class MainActivity extends AppCompatActivity {
private static final String EMAIL_PATTERN = "^[a-zA-Z0-9#_~!$&'()*+,;=:.\"(),:;<>@\\[\\]\\\\]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*$";
@Bind(R.id.user_name_wrapper) TextInputLayout mUserNameWrapper;
@Bind(R.id.password_wrapper) TextInputLayout mPasswordWrapper;
@Bind(R.id.user_name_show) TextView mUserNameShow;
@Bind(R.id.password_show) TextView mPasswordShow;
private Account mAccount; // 账户类
@OnClick(R.id.commit_button) void commitAccount(View view) {
hideKeyboard();
String username = mUserNameWrapper.getEditText().getText().toString();
String password = mPasswordWrapper.getEditText().getText().toString();
if (!validateEmail(username)) {
mUserNameWrapper.setError("邮箱地址错误");
} else if (!validatePassword(password)) {
mUserNameWrapper.setErrorEnabled(false);
mPasswordWrapper.setError("密码错误");
mPasswordWrapper.getEditText().setText("");
} else {
mUserNameWrapper.setErrorEnabled(false);
mPasswordWrapper.setErrorEnabled(false);
doLogin(username, password);
mUserNameShow.setText(mAccount.getUserName());
mPasswordShow.setText(mAccount.getPassword());
}
}
private void doLogin(String username, String password) {
mAccount.setUserName(username);
mAccount.setPassword(password);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
mAccount = new FavorAdapter.Builder(this).build().create(Account.class);
mUserNameShow.setText(mAccount.getUserName());
mPasswordShow.setText(mAccount.getPassword());
}
// 隐藏键盘
private void hideKeyboard() {
View view = getCurrentFocus();
if (view != null) {
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
// 验证邮箱
private boolean validateEmail(String email) {
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
// 验证密码
private boolean validatePassword(String password) {
return password.length() > 7;
}
...
}
2. Favor
初始化Favor类, 传入Context和Favor类型的类.
mAccount = new FavorAdapter.Builder(this).build().create(Account.class);
Account类, 必须要设定set和get方法, 名称相同, 可以提供默认值.
@AllFavor
public interface Account {
void setUserName(String userName);
@Default("No Name") String getUserName();
void setPassword(String password);
@Default("000000") String getPassword();
}
设置Favor类
private void doLogin(String username, String password) {
mAccount.setUserName(username);
mAccount.setPassword(password);
}
使用Favor类
mUserNameShow.setText(mAccount.getUserName());
mPasswordShow.setText(mAccount.getPassword());
Github下载地址
声明Favor的Interface, 就可以方便地使用SharedPreferences了.
Enjoy it!
网友评论