单独注册界面

原创 2018年04月17日 20:48:58

activity_reg

<?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="com.bwie.test.mvpdemo.RegActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <ImageView
            android:id="@+id/icon_back"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/zuo"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"/>

    <EditText
        android:layout_marginTop="50dp"
        android:id="@+id/reg_mobile"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:singleLine="true"
        android:layout_gravity="center_horizontal"/>

    <EditText
        android:layout_marginTop="50dp"
        android:id="@+id/reg_password"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:singleLine="true"
        android:layout_gravity="center_horizontal"
        android:password="true"/>

    <Button
        android:id="@+id/reg"
        android:layout_marginTop="50dp"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:text="立即注册"
        android:gravity="center"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>

RegActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bwie.test.bean.RegBean;
import com.bwie.test.presenter.RegPresenter;
import com.bwie.test.view.ApiUtil;
import com.bwie.test.view.RegPreCallBack;

import java.util.HashMap;
import java.util.Map;

public class RegActivity extends AppCompatActivity implements RegPreCallBack{
    private EditText mobileEdit;
    private EditText passwordEdit;
    private ImageView backImage;
    private Button regBtn;
    private RegPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        presenter = new RegPresenter(this);

        //查找控件
        backImage = (ImageView) findViewById(R.id.icon_back);
        mobileEdit = (EditText) findViewById(R.id.reg_mobile);
        passwordEdit = (EditText) findViewById(R.id.reg_password);
        regBtn = (Button) findViewById(R.id.reg);

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //finish();
            }
        });

        regBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mobile = mobileEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                Map<String, String> params = new HashMap<>();
                params.put("mobile", mobile);
                params.put("password", password);
                presenter.postRegData(ApiUtil.REG_URL, params);
            }
        });
    }

    @Override
    public void onRegPreResponseSuccess(final RegBean regBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RegActivity.this, regBean.getMsg(), Toast.LENGTH_SHORT).show();
                finish();
                if("0".equals(regBean.getCode())){
                    //finish();
                }
            }
        });
    }

    @Override
    public void onRegPreResponseError() {

    }
}

model文件夹下RegModelCallBack

import com.bwie.test.bean.RegBean;

public interface RegModelCallBack {
    void onRegModelResponseSuccess(RegBean regBean);
    void onRegModelResponseError();
}

RegModel

import com.bwie.test.bean.RegBean;
import com.bwie.test.view.OkHttp3Util;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegModel {
    private RegModelCallBack callBack;

    public RegModel(RegModelCallBack callBack) {
        this.callBack = callBack;
    }

    public void postRegData(String url, Map<String, String> params){
        OkHttp3Util.doPost(url, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                callBack.onRegModelResponseError();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                RegBean regBean = gson.fromJson(response.body().string(), RegBean.class);
                callBack.onRegModelResponseSuccess(regBean);
            }
        });
    }
}

presenter文件夹下RegPreCallBack

public interface RegPreCallBack {
    void onRegPreResponseSuccess(RegBean regBean);
    void onRegPreResponseError();
}

RegPresenter

import com.bwie.test.bean.RegBean;
import com.bwie.test.model.RegModel;
import com.bwie.test.model.RegModelCallBack;
import com.bwie.test.view.RegPreCallBack;
import java.util.Map;

public class RegPresenter implements RegModelCallBack{
    private RegPreCallBack regPreCallBack;
    private RegModel regModel;

    public RegPresenter(RegPreCallBack regPreCallBack) {
        this.regPreCallBack = regPreCallBack;
        this.regModel = new RegModel(this);
    }

    public void postRegData(String url, Map<String, String> params){
        regModel.postRegData(url, params);
    }

    @Override
    public void onRegModelResponseSuccess(RegBean regBean) {
        regPreCallBack.onRegPreResponseSuccess(regBean);
    }

    @Override
    public void onRegModelResponseError() {
        regPreCallBack.onRegPreResponseError();
    }
}

view文件夹下ApiUtil

public class ApiUtil {
    //public static final String LOGIN_URL = "http://120.27.23.105/user/login";
    public static final String REG_URL = "http://120.27.23.105/user/reg";
    //public static final String GOOD_LIST = "http://120.27.23.105/product/getProducts";
}

OkHttp3Util

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp3Util {
    private static OkHttpClient okHttpClient = null;

    private OkHttp3Util() {
    }

    public static OkHttpClient getInstance() {
        if (okHttpClient == null) {
            //加同步安全
            synchronized (OkHttp3Util.class) {
                if (okHttpClient == null) {
                    //okhttp可以缓存数据....指定缓存路径
                    File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
                    //指定缓存大小
                    int cacheSize = 10 * 1024 * 1024;

                    okHttpClient = new OkHttpClient.Builder()//构建器
                            .connectTimeout(15, TimeUnit.SECONDS)//连接超时
                            .writeTimeout(20, TimeUnit.SECONDS)//写入超时
                            .readTimeout(20, TimeUnit.SECONDS)//读取超时
                            .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))//设置缓存
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    /**
     * get请求
     */
    public static void doGet(String oldUrl, Callback callback) {
        //要添加的公共参数...map
        Map<String,String> map = new HashMap<>();
        map.put("source","android");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(oldUrl);

        if (oldUrl.contains("?")){
            if (oldUrl.indexOf("?") == oldUrl.length()-1){
            }else {
                stringBuilder.append("&");
            }
        }else {
            //不包含? 属于1类型,,,先拼接上?号
            stringBuilder.append("?");
        }

        for (Map.Entry<String,String> entry: map.entrySet()) {
            stringBuilder.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
        }

        if (stringBuilder.indexOf("&") != -1){
            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&"));
        }
        String newUrl = stringBuilder.toString();

        OkHttpClient okHttpClient = getInstance();
        Request request = new Request.Builder().url(newUrl).build();
      
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    /**
     * post请求
     */
    public static void doPost(String url, Map<String, String> params, Callback callback) {
        Map<String,String> map = new HashMap<>();
        map.put("source","android");

        //创建OkHttpClient请求对象
        OkHttpClient okHttpClient = getInstance();

        FormBody.Builder builder = new FormBody.Builder();
        for (String key : params.keySet()) {
            builder.add(key, params.get(key));
        }

        for (String key : map.keySet()) {
            builder.add(key, map.get(key));
        }

        Request request = new Request.Builder().url(url).post(builder.build()).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    /**
     * post请求上传文件包括图片
     */
    public static void uploadFile(String url, File file, String fileName,Map<String,String> params,
                                        Callback callback) {
        //创建OkHttpClient请求对象
        OkHttpClient okHttpClient = getInstance();

        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);

        //参数
        if (params != null){
            for (String key : params.keySet()){
                builder.addFormDataPart(key,params.get(key));
            }
        }
        builder.addFormDataPart("file",fileName,RequestBody.create(MediaType.parse(
                                  "application/octet-stream"),file));

        //构建
        MultipartBody multipartBody = builder.build();

        //创建Request
        Request request = new Request.Builder().url(url).post(multipartBody).build();

        //得到Call
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }

    /**
     * Post请求发送JSON数据
     */
    public static void doPostJson(String url, String jsonParams, Callback callback) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), 
                                                jsonParams);
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    /**
     * 下载文件
     */
    public static void download(final Activity context, final String url, final String saveDir) {
        Request request = new Request.Builder().url(url).build();
        Call call = getInstance().newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //com.orhanobut.logger.Logger.e(e.getLocalizedMessage());
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;
                try {
                    is = response.body().byteStream();
                    //apk保存路径
                    final String fileDir = isExistDir(saveDir);
                    //文件
                    File file = new File(fileDir, getNameFromUrl(url));
                    fos = new FileOutputStream(file);
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                    }

                    fos.flush();
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url),
                                                            Toast.LENGTH_SHORT).show();
                        }
                    });
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    context.startActivity(intent);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (is != null) is.close();
                    if (fos != null) fos.close();
                }
            }
        });
    }

    /**
     * 判断下载目录是否存在
     */
    public static String isExistDir(String saveDir) throws IOException {
        // 下载位置
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
            if (!downloadFile.mkdirs()) {
                downloadFile.createNewFile();
            }
            String savePath = downloadFile.getAbsolutePath();
            Log.e("savePath", savePath);
            return savePath;
        }
        return null;
    }

    /**
     * @param url
     * @return 从下载连接中解析出文件名
     */
    private static String getNameFromUrl(String url) {
        return url.substring(url.lastIndexOf("/") + 1);
    }
} 

使用的依赖和方法

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.6.0'
compile 'com.squareup.okio:okio:1.11.0'
compile 'com.google.code.gson:gson:2.8.2'
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

bean文件夹下RegBean

public class RegBean {
    private String msg;
    private String code;
    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

【系列课】Springmvc4+Mybatis3+Spring4+Bootstrap3之注册

【技术架构】 把手教你整合最优雅SSM4框架: 当我们看大部分教学课堂中把SSH作为最核心教学内容。但实际互联网企业应用中,SpringMVC可以完全替代Struts系列,注解方式,通过restful风格,再配上企业实战架构SpringMVC4 , Spring 4, MyBatis3,JQuery,JQueryUI,Echarts3,Bootstrap3,Ajax堪称完美。
  • 2017年05月02日 22:59

一个ASP.NET用户注册页面

  • 2009年11月29日 18:06
  • 214KB
  • 下载

[专业级界面控件套装].Codejock.Xtreme.Suite.Pro.ActiveX.v15.0.1.Retail注册机

  • 2011年03月04日 16:37
  • 207KB
  • 下载

用Android写注册界面

LinearLayout 控制布局 TextView用于显示文字 EditText输入框 RadioGroup单选按钮和RadioButton一起用 CheckBox复选框 Spinner下...
  • gh6267
  • gh6267
  • 2017-09-05 16:44:07
  • 511

注册界面及短信验证

布局文件:
  • duoluo9
  • duoluo9
  • 2016-11-03 18:02:06
  • 523

Android简单的用户信息注册界面

一、运行测试效果
  • oFanJunBin
  • oFanJunBin
  • 2014-06-11 22:51:47
  • 5168

java实现注册界面设计

学习了java,是否有种迫切编出可视化界面的渴望,将以下代码复制粘贴运行,然后恭喜你,成功了...
  • Wang_Hong_Yi
  • Wang_Hong_Yi
  • 2017-05-07 21:04:30
  • 1400

java web (登陆注册界面)

--java的代码部分 package com.demo.db.utils; import java.sql.Connection; import java.sql.DriverMana...
  • Blake01
  • Blake01
  • 2015-09-19 19:57:03
  • 1534

Android 用户注册界面的实现

本文主要讲解Android中用户注册界面的实现。详细请看代码: 1.MainActivity.java public class MainActivity extends Activity imp...
  • tanghua0809
  • tanghua0809
  • 2015-08-15 23:19:27
  • 802

CSS编写静态网易注册界面

注册网易免费邮箱 body{ font-size:12px; font-family:"宋体"; co...
  • shwanglp
  • shwanglp
  • 2016-12-11 21:47:37
  • 2567
收藏助手
不良信息举报
您举报文章:单独注册界面
举报原因:
原因补充:

(最多只允许输入30个字)