标签: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(); } }
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); } }
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>
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>
四、今日团队链接
https://www.cnblogs.com/three3/p/12747652.html
标签:reg color lan span 头部 type pen 进入 tostring
原文地址:https://www.cnblogs.com/xhj1074376195/p/12748633.html