安卓注册界面
xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".RegisterActivity">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:gravity="center"
android:padding="10dp"
android:text="注册账号"
android:textColor="@color/white"
android:textSize="18sp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="手机号:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/tel"
android:hint="请输入手机号"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="密 码:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/pwd"
android:hint="请输入密码"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="姓 名:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/name"
android:hint="请输入姓名"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="地 址:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/address"
android:hint="请输入地址"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="收款类型:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/amountType"
android:hint="支付宝/微信"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="收款账号:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/amount"
android:hint="18588263531"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/ok"
android:text="同意服务条款:"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/commit"
android:text="《鬼市商户入驻使用协议》"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/msg"
android:gravity="center"
android:hint="验证码"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/getMsg"
android:text="获取验证码"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/register"
android:layout_marginTop="20dp"
android:text="注 册"
android:textSize="16sp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
******************************************************************************************
java文件
package com.culturer.guishi_shoper;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.culturer.guishi_shoper.utils.MD5Util;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.kymjs.rxvolley.http.VolleyError;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Map;
public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
private String session = "";
private EditText tel;
private EditText pwd;
private EditText name;
private EditText address;
private EditText amountType;
private EditText amount;
private CheckBox ok;
private TextView commit;
private EditText msg;
private Button getMsg;
private Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取对应布局文件
setContentView(R.layout.activity_register);
//底部导航栏初始化
changeTopBackground();
//view控件等初始化
initView();
}
//初始化view
private void initView(){
//基本控件初始化
initBaseView();
}
private void initBaseView(){
//获取相应控件
tel = findViewById(R.id.tel);
pwd = findViewById(R.id.pwd);
name = findViewById(R.id.name);
address = findViewById(R.id.address);
amountType = findViewById(R.id.amountType);
amount = findViewById(R.id.amount);
ok = findViewById(R.id.ok);
commit = findViewById(R.id.commit);
msg = findViewById(R.id.msg);
getMsg = findViewById(R.id.getMsg);
register = findViewById(R.id.register);
//checkbox的点击事件
ok.setOnCheckedChangeListener((compoundButton, b) -> {
if (b){
register.setEnabled(true);
}else {
register.setEnabled(false);
}
});
//注册两个点击事件
register.setOnClickListener(view -> register());
getMsg.setOnClickListener(view -> getMsg());
}
//获取短信验证码
private void getMsg(){
HttpParams params = new HttpParams(); //http请求
params.put("Tel",tel.getText().toString()); //获取手机号
params.put("options","getMsg"); //后台相应的getMsg请求,option为getMsg,在controllers的utils文件中
HttpCallback callback = new HttpCallback() {
@Override
//服务器返回成功的数据等
public void onSuccess(Map<String, String> headers, byte[] t) {
String message = new String(t);
Log.i(TAG, "onSuccess: "+ message);
try {
JSONObject jsonObject = new JSONObject(message);
int status = jsonObject.getInt("status");
if (status == 400 ){
String msg = jsonObject.getString("msg");
Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_LONG).show();
}if (status == 200 ){
session = headers.get("Set-Cookie").split(";")[0];
Log.i(TAG, "session "+session);
}else {
Toast.makeText(RegisterActivity.this,"发生未知错误!",Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
//服务器返回失败的数据等
public void onFailure(VolleyError error) {
Toast.makeText(RegisterActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
Log.i(TAG, "onFailure: "+error.getMessage());
}
};
//配置文件如下192.168.43.146在cmd的ipconfig可查看,/url是服务器地址
new RxVolley.Builder()
.url("http://192.168.43.146:8080"+"/utils")
.httpMethod(RxVolley.Method.POST) //default GET or POST/PUT/DELETE/HEAD/OPTIONS/TRACE/PATCH
.contentType(RxVolley.ContentType.FORM)//default FORM or JSON
.params(params)
.shouldCache(false) //default: get true, post false
.callback(callback) //callback是返回数据
.encoding("UTF-8") //default
.doTask();
}
//注册
private void register(){
//对应控件的input数据,全部写入数据库
HttpParams params = new HttpParams(); //http请求
params.put("options","register");
params.put("code",msg.getText().toString());
params.putHeaders("content-type","application/x-www-form-urlencoded");
params.putHeaders("Cookie",session);
params.put("Tel",tel.getText().toString());
params.put("Pwd",MD5Util.encrypt(pwd.getText().toString().trim()));
params.put("Payee",name.getText().toString());
params.put("Address",address.getText().toString());
params.put("Amount",amount.getText().toString());
params.put("AmountType",amountType.getText().toString());
HttpCallback callback = new HttpCallback() {
@Override
//返回的成功数据
public void onSuccess(String t) {
Log.i(TAG, "onSuccess: "+t);
try {
JSONObject jsonObject = new JSONObject(t);
int status = jsonObject.getInt("status");
Log.i(TAG, "onSuccess: "+status);
String msg = jsonObject.getString("msg");
Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_LONG).show();
if (status == 200){
Intent intent = new Intent(RegisterActivity.this,CreateStoreActivity.class);
startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
//返回失败原因及数据
public void onFailure(VolleyError error) {
Toast.makeText(RegisterActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
Log.i(TAG, "onFailure: "+error.getMessage());
}
};
//相关配置等
new RxVolley.Builder()
.url("http://192.168.43.146:8080"+"/shopper")
.httpMethod(RxVolley.Method.POST) //default GET or POST/PUT/DELETE/HEAD/OPTIONS/TRACE/PATCH
.contentType(RxVolley.ContentType.FORM)//default FORM or JSON
.params(params)
.shouldCache(false) //default: get true, post false
.callback(callback)
.encoding("UTF-8") //default
.doTask();
}
//修改顶部状态栏颜色
private void changeTopBackground(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
//设置修改状态栏
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//设置状态栏的颜色,和你的app主题或者标题栏颜色设置一致就ok了
window.setStatusBarColor(getColor(R.color.black));
}
}
}

浙公网安备 33010602011771号