Android开发实习——蓄电池防盗
主要任务:搭建登录页面和修改密码页面(修改密码功能实现基于ygq的帮忙)。
UI图
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_top"
android:layout_width="match_parent"
android:layout_height="239dp"
android:background="#0B85E8"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp">
<ImageView
android:id="@+id/tv_1"
android:layout_width="85dp"
android:layout_height="85dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="10dp"
tools:ignore="MissingConstraints"
android:background="@mipmap/login_logo" />
<TextView
android:id="@+id/tv_2"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="蓄电池防盗"
android:textSize="38dp"
android:textColor="#FFFFFF"
android:layout_marginTop="20dp"
android:gravity="center" />
</LinearLayout>
<EditText
android:id="@+id/edit_text_1"
android:layout_width="match_parent"
android:layout_height="45dp"
app:layout_constraintTop_toBottomOf="@+id/linear_top"
tools:ignore="MissingConstraints"
android:layout_marginTop="69dp"
android:hint="请输入用户名"
android:paddingLeft="15dp"
android:textSize="16sp"
android:drawableLeft="@mipmap/ic_login_username"
android:background="@drawable/shape1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="19dp"
app:layout_constraintTop_toBottomOf="@+id/edit_text_1"
tools:ignore="MissingConstraints"
android:hint=" 请输入密码"
android:inputType="textPassword"
android:paddingLeft="15dp"
android:drawableLeft="@mipmap/ic_login_password"
android:textSize="16sp"
android:background="@drawable/shape1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<Button
android:id="@+id/btn_Login"
android:layout_width="match_parent"
app:layout_constraintTop_toBottomOf="@+id/edit_text_2"
android:layout_height="45dp"
android:layout_marginTop="51dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:text="登录"
android:textColor="#ffffff"
android:textSize="16dp"
android:enabled="false"
android:background="@drawable/selector_main_add_devices"
tools:ignore="MissingConstraints"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cc.makepower.tt.monitor.ui.setting.PasswordActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@color/colorPrimaryTran10"
android:elevation="2dp"
app:navigationIcon="@mipmap/ic_goback"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_height="@dimen/title_translucent_height"
android:paddingTop="@dimen/title_margin_top"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/toolbar_tv"
android:textColor="@color/colorWhite"
android:textSize="@dimen/toolbar_text_size"
android:text="@string/title_set_password"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
<EditText
android:id="@+id/edit_text_1"
android:layout_width="match_parent"
android:layout_height="45dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
tools:ignore="MissingConstraints"
android:layout_marginTop="69dp"
android:hint=" 请输入新密码"
android:paddingLeft="15dp"
android:textSize="16sp"
android:background="@drawable/shape1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="19dp"
app:layout_constraintTop_toBottomOf="@+id/edit_text_1"
tools:ignore="MissingConstraints"
android:hint=" 请再次输入新密码"
android:inputType="textPassword"
android:paddingLeft="15dp"
android:textSize="16sp"
android:background="@drawable/shape1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<Button
android:id="@+id/btn_put"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:text="确认修改"
android:textColor="#ffffff"
app:layout_constraintTop_toBottomOf="@id/edit_text_2"
android:background="@drawable/shape"/>
</android.support.constraint.ConstraintLayout>
修改密码功能用到MVP框架附上文章一篇转载自:
https://www.jianshu.com/p/1f91cfd68d48
PasswordActivity.Java
package cc.makepower.accumulator.ui.setting;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import butterknife.BindView;
import cc.makepower.accumulator.R;
import cc.makepower.accumulator.contract.PasswordContract;
import cc.makepower.accumulator.presenter.PasswordPresenter;
import cc.makepower.software.android.Base.APresenter;
import cc.makepower.software.android.Base.BaseActivity;
/**
- Created by CY on 2018/1/6 0006.
*/
public class PasswordActivity extends BaseActivity implements PasswordContract.View{
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.edit_text_1)
EditText edit_text_1;
@BindView(R.id.edit_text_2)
EditText edit_text_2;
// @BindView(R.id.et_check_password)
// EditText check_password;
@BindView(R.id.btn_put)
Button button;
private PasswordPresenter passwordPresenter;
@Override
protected int getLayoutId() {
return R.layout.alert_password;
}
@Override
protected void afterInCreate(Bundle savedInstanceState) {
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// finish();
if (TextUtils.isEmpty(edit_text_1.getText().toString())){
showToast("密码不能为空");
return;
}
if (TextUtils.isEmpty(edit_text_2.getText().toString())){
showToast("密码不能为空");
return;
}
click();
}
});
}
@Override
protected APresenter injectPresenter() {
passwordPresenter=new PasswordPresenter(PasswordActivity.this);
return passwordPresenter;
}
void click(){
passwordPresenter.changePwd(edit_text_1.getText().toString(),edit_text_2.getText().toString());
}
@Override
public void changePwdResult(boolean resu) {
if (resu){
finish();
}else
showToast("密码修改失败");
}
}
PasswordPresenter.Java
package cc.makepower.accumulator.presenter;
import android.content.Context;
import cc.makepower.accumulator.R;
import cc.makepower.accumulator.contract.PasswordContract;
import cc.makepower.software.android.Base.APresenter;
import cc.makepower.software.android.Beans.PostResult;
import cc.makepower.software.android.Beans.ResultBean;
import cc.makepower.software.android.remote.RestDataSource;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class PasswordPresenter extends APresenter<PasswordContract.View>{
private RestDataSource restDataSource;
public PasswordPresenter(Context context){
restDataSource=RestDataSource.getInstance(context);
}
public void changePwd(String oldPwd,String newPwd){
unSubscribe();
getView().showProgress(R.string.please_wait);
subscription=restDataSource.changePwd(newPwd,newPwd)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<PostResult>() {
@Override
public void onCompleted() {
getView().hideProgress();
}
@Override
public void onError(Throwable e) {
getView().hideProgress();
getView().showToast(showError(e));
getView().changePwdResult(false);
}
@Override
public void onNext(PostResult postResult) {
getView().hideProgress();
getView().changePwdResult(postResult.getStatus()==PostResult.status.SUCCESS);
}
});
}
}
PasswordContract.Java(接口类文件)
package cc.makepower.accumulator.contract;
import cc.makepower.software.android.Base.BaseView;
public interface PasswordContract {
interface View extends BaseView{
void changePwdResult(boolean resu);
}
}