Android实战项目真枪实弹第四炮[通俗易懂]

Android (78) 2023-07-23 20:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说Android实战项目真枪实弹第四炮[通俗易懂],希望能够帮助你!!!。

【威哥说】跟大家汇报一下哈,磨砺营的小伙伴们下周开始将进入最后的项目阶段,冲刺阶段来了,大家已经提前进入了状态,每天晚上自觉学习到十二点,甚至深夜,疲惫对于我们来说已然是自轻松面对,做最好的自己,请期待我们的项目展示吧,加油。

【正文】

大家好,今天我们继续Android项目实战的第四节,本节会带大家完成几乎每个项目都会有的登录注册模块,上节我们已经给大家封装好了数据库的架构,本节我们就来使用它来保存和读取数据,一句代码搞定,非常简单。

首先登录模块,这里我们需要用到自定以edittext,原生效果实在不给力,虽然我们的Demo比较简单,但是效果还是要有的,也给大家引入自定义View的实例。

public class MyEditText extends EditText {

private Drawable mDrawable;

private Context mContext;

//画笔

private Paint mPaint;

private int color;

private boolean hasFocus = false;

public MyEditText(Context context) {

super(context);

mContext = context;

initView();

}

public MyEditText(Context context, AttributeSet attrs) {

super(context, attrs);

mContext = context;

initView();

}

private void initView() {

mDrawable = getResources().getDrawable(R.mipmap.sign_check2x);

//开始画线

mPaint = new Paint();

mPaint.setStrokeWidth(3.0f);

color = Color.parseColor("#bfbfbf");

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

mPaint.setColor(color);

int x = this.getScrollX();

int w = this.getMeasuredWidth();

canvas.drawLine(0,this.getHeight() - 1,w+x,this.getHeight() - 1,mPaint);

}

@Override

protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {

super.onFocusChanged(focused, direction, previouslyFocusedRect);

this.hasFocus = focused;

if (focused){

setColor(Color.parseColor("#00C17C"));

}else {

setColor(Color.parseColor("#bfbfbf"));

}

}

private void setColor(int i) {

this.color = i;

this.setTextColor(color);

invalidate();

}

public void setImageVisible(boolean isVisible){

if (isVisible){

setCompoundDrawablesWithIntrinsicBounds(null,null,mDrawable,null);

}else {

setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);

}

}

@Override

protected void finalize() throws Throwable {

super.finalize();

}

}

然后开始布局:

<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:orientation="vertical"

tools:context="com.moliying.androidlifehelper.activity.LoginActivity">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher"

android:layout_gravity="center"

/>

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="磨砺营IT教育"

android:textSize="50sp"

android:gravity="center"

android:textColor="#00c117"

/>

<com.moliying.androidlifehelper.view.MyEditText

android:id="@+id/user_name_login_et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="输入用户名"

android:background="@null"

android:layout_marginLeft="10dp"

android:layout_marginTop="10dp"

android:layout_marginRight="10dp"

android:padding="10dp"

android:singleLine="true"

android:textCursorDrawable="@drawable/edittext_cursor"

/>

<com.moliying.androidlifehelper.view.MyEditText

android:id="@+id/pass_word_login_et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="请输入密码"

android:background="@null"

android:layout_marginLeft="10dp"

android:layout_marginTop="10dp"

android:layout_marginRight="10dp"

android:padding="10dp"

android:singleLine="true"

android:textCursorDrawable="@drawable/edittext_cursor"

android:password="true"

/>

<Button

android:id="@+id/login_btn"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:padding="5dp"

android:textSize="20sp"

android:background="@drawable/login_btn_selector"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginTop="20dp"

/>

<TextView

android:id="@+id/regist_tv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="注册"

android:layout_marginTop="15dp"

android:textSize="20sp"

android:clickable="true"

/>

</LinearLayout>

下面是activity代码的实现,先把代码贴出来,然后再讲解:

public class LoginActivity extends BaseActivity implements View.OnClickListener {

//^([\\S\\s]){2,16}

private MyEditText usernameEditText;

private MyEditText passwordEditText;

private Button loginButton;

private TextView registTextView;

private boolean isName;

private boolean isPassword;

private TextWatcher watcher = new TextWatcher() {

@Override

public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

//用户名限制到2到16位

if (usernameEditText.getText().toString().trim().matches("^([\\S\\s]){2,16}")) {

usernameEditText.setImageVisible(true);

isName = true;

} else {

usernameEditText.setImageVisible(false);

isName = false;

}

//密码限制到6到16位

if (passwordEditText.getText().toString().trim().matches("^([\\S\\s]){6,16}")) {

passwordEditText.setImageVisible(true);

isPassword = true;

} else {

passwordEditText.setImageVisible(false);

isPassword = false;

}

}

@Override

public void afterTextChanged(Editable s) {

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

initView();

}

private void initView() {

usernameEditText = (MyEditText) findViewById(R.id.user_name_login_et);

passwordEditText = (MyEditText) findViewById(R.id.pass_word_login_et);

loginButton = (Button) findViewById(R.id.login_btn);

registTextView = (TextView) findViewById(R.id.regist_tv);

//加入点击监听

loginButton.setOnClickListener(this);

registTextView.setOnClickListener(this);

//加入观察者

usernameEditText.addTextChangedListener(watcher);

passwordEditText.addTextChangedListener(watcher);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.login_btn:

submitLogin();

break;

case R.id.regist_tv:

startActivity(new Intent(LoginActivity.this, RegistActivity.class));

finish();

break;

}

}

/**

* 提交登录信息

*/

private void submitLogin() {

String username = usernameEditText.getText().toString().trim();

String password = passwordEditText.getText().toString().trim();

if (isName && isPassword) {

//查询数据库

UserModelBean userModelBean = LocalDataModel.getInstance().selectUser(username);

if (userModelBean == null) {

Toast.makeText(this, "您输入的姓名或密码有误,请重新输入", Toast.LENGTH_SHORT).show();

return;

}

if ((username.equals(userModelBean.getUsername())) && (password.equals(userModelBean.getPassword()))) {

new SharedXmlUtil(this).write(Const.LOGINUSERNAME, username);

startActivity(new Intent(LoginActivity.this, MainActivity.class));

finish();

} else {

Toast.makeText(this, "您输入的密码有误,请重新输入", Toast.LENGTH_SHORT).show();

}

}

}

}

这里主要给edittext设置一个TextWatcher,动态监听,使用正则来判断是否符合规范。放置两个成员的标记,保存状态。这样,在点击按钮事件里面直接判断标记,登录是否成功,然后再进行相应的操作。这里可以看到,保存数据库仅仅用一行代码,这里不明白的参看第三节的封装。

注册模块的UI和逻辑基本和登录一样,这里不再贴出代码,唯一不同就是按钮事件有点区别,注册是保存数据库,如下:

private void submitRegist() {

String username = usernameEditText.getText().toString().trim();

String password = passwordEditText.getText().toString().trim();

if (isName && isPassword){

//保存数据库

UserModelBean user = new UserModelBean();

user.setUsername(username);

user.setPassword(password);

LocalDataModel.getInstance().saveUser(user);

new SharedXmlUtil(this).write(Const.LOGINUSERNAME, username);

startActivity(new Intent(RegistActivity.this, MainActivity.class));

finish();

}

}

Ok,现在大家可以把注册和登录模块完成了,下节课咱们讲解首页的实现,本节课今天就到这里,下节再见!感谢大家的关注!不要忘了赞赏啊!!!

本文出自微信公众号mjw-java,更多内容关注微信公众号或访问www.moliying.com

发表回复