码迷,mamicode.com
首页 > 其他好文 > 详细

团队冲刺--第一阶段(五)

时间:2020-04-22 00:19:54      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:reg   color   lan   span   头部   type   pen   进入   tostring   

一、前言

  昨天实现了发布的功能。

  今天学习了sqlite数据库,完善了登录注册功能。

  明天美化页面,学习头部标题栏定义。

二、成果展示

 技术图片

 三、代码

RegisterActivity.java

技术图片技术图片
package com.androidlearing.tdtreehole.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.androidlearing.tdtreehole.R;
import com.androidlearing.tdtreehole.httputils.HttpUtils;
import com.androidlearing.tdtreehole.pojo.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "RegisterActivity";
    private EditText mEt_username;
    private EditText mEt_password;
    private EditText mEt_repassword;
    private Button mBtn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        mEt_username = findViewById(R.id.et_username);
        mEt_password = findViewById(R.id.et_password);
        mEt_repassword = findViewById(R.id.et_repassword);
        mBtn_register = findViewById(R.id.btn_register);
        mBtn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setData();
            }
        });
    }

    private void setData() {
        User user = new User();
        user.setUsername(mEt_username.getText().toString());
        user.setPassword(mEt_password.getText().toString());
        user.setRepassword(mEt_repassword.getText().toString());
        if (user.getUsername().equals("")){
            Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_LONG).show();
        }else if (user.getPassword().equals("")){
            Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
        }else if (user.getRepassword().equals("")){
            Toast.makeText(RegisterActivity.this, "请确认密码", Toast.LENGTH_LONG).show();
        }else{
            insertUser(user);
        }
    }

    private void insertUser(final User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "进入到插入方法了...");
                HttpUtils httpUtils = new HttpUtils();
                Call call = httpUtils.getCalls("http://101.200.132.210/TDTreeHole/register?username=" + user.getUsername() + "&&password=" + user.getPassword() );
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        //请求失败的回调方法
                        Log.d(TAG, "onFailure and Exception is -->" + e);
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        //请求成功的回调方法
                        Log.d(TAG, "onResponse...");
                        Gson gson = new Gson();
                        Type type = new TypeToken<String>() {}.getType();
                        String result = gson.fromJson(response.body().string(), type);
                        Log.d(TAG,"result =="+result);
                        if (result.equals("注册成功")){
                            Intent intent = new Intent(RegisterActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                        Looper.prepare();
                        Toast.makeText(RegisterActivity.this, result.trim(), Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }
                });
            }
        }).start();
    }
}
View Code

LoginActivity.java

技术图片技术图片
package com.androidlearing.tdtreehole.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.androidlearing.tdtreehole.R;
import com.androidlearing.tdtreehole.httputils.HttpUtils;
import com.androidlearing.tdtreehole.pojo.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";
    private EditText mEt_username;
    private EditText mEt_password;
    private Button mBtn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        mEt_username = findViewById(R.id.et_username);
        mEt_password = findViewById(R.id.et_password);
        mBtn_login = findViewById(R.id.btn_login);
        mBtn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setData();
            }
        });
    }

    private void setData() {
        User user = new User();
        user.setUsername(mEt_username.getText().toString());
        user.setPassword(mEt_password.getText().toString());
        if (user.getUsername().equals("")){
            Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_LONG).show();
        }else if (user.getPassword().equals("")){
            Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
        }else {
            insertUser(user);
        }
    }

    private void insertUser(final User user) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "进入到插入方法了...");
                HttpUtils httpUtils = new HttpUtils();
                Call call = httpUtils.getCalls("http://101.200.132.210/TDTreeHole/login?username=" + user.getUsername() + "&&password=" + user.getPassword() );
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        //请求失败的回调方法
                        Log.d(TAG, "onFailure and Exception is -->" + e);
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        //请求成功的回调方法
                        Log.d(TAG, "onResponse...");
                        Gson gson = new Gson();
                        Type type = new TypeToken<String>() {}.getType();
                        String result = gson.fromJson(response.body().string(), type);
                        Log.d(TAG,"result =="+result);
                        if (result.equals("登录成功")){
                            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);
                        }
                        Looper.prepare();
                        Toast.makeText(LoginActivity.this, result.trim(), Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }
                });
            }
        }).start();
    }

    public void Register(View view){
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }
}
View Code

activity_register.xml

技术图片技术图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/logo"/>

    <EditText
        android:id="@+id/et_username"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="35dp"
        android:hint="请输入用户名:" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:inputType="textPassword"
        android:hint="请输入密码:" />

    <EditText
        android:id="@+id/et_repassword"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:inputType="textPassword"
        android:hint="请确认密码:" />

    <Button
        android:id="@+id/btn_register"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp"
        android:text="注册" />

</LinearLayout>
View Code

activity_login.xml

技术图片技术图片
<?xml version="1.0" encoding="utf-8"?>
<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=".activity.LoginActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="20dp"
            android:src="@drawable/logo"/>


    <EditText
        android:id="@+id/et_username"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="35dp"
        android:hint="请输入用户名:" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:inputType="textPassword"
        android:hint="请输入密码:" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="5dp"
        android:text="登录" />

    <Button
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:onClick="Register"
        android:text="注册" />

</LinearLayout>
View Code

四、今日团队链接

https://www.cnblogs.com/three3/p/12747652.html

 

团队冲刺--第一阶段(五)

标签:reg   color   lan   span   头部   type   pen   进入   tostring   

原文地址:https://www.cnblogs.com/xhj1074376195/p/12748633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!