直接上已经实现的登录页面图
xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_login"
android:background="@drawable/bg11"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<LinearLayout
android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="vertical">
<EditText
android:id="@+id/phone_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone_hint"
android:inputType="phone"
android:maxLength="11"
android:textSize="25sp" />
<EditText
android:id="@+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:maxLength="32"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/checkbox_option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputText">
<CheckBox
android:id="@+id/remenber_password"
android:textColor="@color/colorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="记住密码" />
<CheckBox
android:id="@+id/login_auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:textColor="@color/colorFont"
android:text="自动登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/checkbox_option">
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="15dp"
android:background="@color/colorButton"
android:text="@string/action_login"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorFont"
android:textSize="20dp" />
<Button
android:id="@+id/register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="15dp"
android:background="@color/colorButton"
android:text="@string/action_register"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textColor="@color/colorFont"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
登录java代码
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import com.xujun.busticketsystem.utils.GetPostUtil;
import com.xujun.busticketsystem.utils.MD5Utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
public class LoginActivity extends AppCompatActivity {
//服务器端URL
//定义账号,密码输入框
private EditText mPhoneText;
private EditText mPasswordText;
//复选框
private CheckBox mRemenber;//记住密码
private boolean mPasswordFlag = false;//记住密码标志
private CheckBox mAutoLogin;//自动登录
private boolean mAutoLoginFlag = false;//自动登录标志
private String userPassword = "";
//定义登录,注册按钮
private Button mLoginButton;
private Button mRegisterButton;
//提示框
private ProgressDialog mDialog;
private static HttpURLConnection connection = null;
private static DataOutputStream out = null;
private static InputStream in = null;
private static URL realUrl = null;
private String params = "";//定义参数字符串
// private UserService mUserService = new UserServiceImpl();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//为组件赋值
mPhoneText = findViewById(R.id.phone_text);
mPasswordText = findViewById(R.id.password_text);
mRemenber = findViewById(R.id.remenber_password);
mAutoLogin = findViewById(R.id.login_auto);
mLoginButton = findViewById(R.id.login_button);
mRegisterButton = findViewById(R.id.register_button);
SharedPreferences sharedPreferences = getSharedPreferences("busApp", MODE_PRIVATE);
//如果不为空
if (sharedPreferences != null) {
String userName = sharedPreferences.getString("username", "");
userPassword = sharedPreferences.getString("password", "");
mPasswordFlag = sharedPreferences.getBoolean("remenber", false);
mAutoLoginFlag = sharedPreferences.getBoolean("auto", false);
mPhoneText.setText(userName);
}
//确定为true获取 记住密码,打钩
if (mPasswordFlag) {
mRemenber.setChecked(true);
mPasswordText.setText(userPassword);
}
//选择了自动登录后直接登录
if (mAutoLoginFlag){
mAutoLogin.setChecked(true);
String username = mPhoneText.getText().toString();
String password = mPasswordText.getText().toString();
login(username,password);
}
//注册监听
mRemenber.setOnClickListener(mListener);
mRemenber.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//记住密码
//如果是选中记住密码,取消记住密码、自动登录
if (!isChecked){
mAutoLogin.setChecked(false);
//清空密码输入框
mPasswordText.setText("");
}
}
});
// mAutoLogin.setOnClickListener(mListener);
mLoginButton.setOnClickListener(mListener);
mRegisterButton.setOnClickListener(mListener);
}
//点击事件
View.OnClickListener mListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
//根据view的id判断点击的是哪个视图
switch (v.getId()) {
case R.id.login_button:
//1 创建 SharePreferences 对象
String username = mPhoneText.getText().toString();
String password = mPasswordText.getText().toString();
Log.d("输入框获取的密码", "onClick: " + password);
//没有记住密码时 MD5密码加密
SharedPreferences sharedPreferences = getSharedPreferences("busApp", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//2 创建Editor对象,写入值
editor.putString("username", username);
if (mRemenber.isChecked()) {
if(!mPasswordFlag){
try {
password = MD5Utils.getMD5(password);
Log.d("记住密码选中且false加密后密码", "onClick: " + password);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
mPasswordFlag = true;
}
editor.putBoolean("remenber", mPasswordFlag);
editor.putString("password", password);
Log.d("记住密码选中写入的密码", "onClick: " + password);
//选中自动登录
if (mAutoLogin.isChecked()){
mAutoLoginFlag = true;
}else{
mAutoLoginFlag = false;
}
editor.putBoolean("auto", mAutoLoginFlag);
} else {
if(!mPasswordFlag){
try {
password = MD5Utils.getMD5(password);
Log.d("记住密码未选中且false加密后密码", "onClick: " + password);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
//取消自动登录和记住密码,清空密码
mPasswordFlag = false;
mAutoLoginFlag = false;
editor.putString("password", "");
editor.putBoolean("remenber", mPasswordFlag);
editor.putBoolean("auto", mAutoLoginFlag);
}
//3 提交
editor.commit();
//跳过登录进入主界面
// startActivity(new Intent(LoginActivity.this,MainActivity.class));
login(username, password);
break;
case R.id.register_button:
register();
break;
default:
break;
}
}
};
private void login(String username,String password) {
mDialog = new ProgressDialog(LoginActivity.this);
mDialog.setTitle("正在登陆...");
mDialog.setMessage("请稍后...");
mDialog.setCancelable(false);//设置不能通过后退键取消
mDialog.show();
//将登录名和密码拼接
params = "?username=" + username + "&password=" + password;
LoginTask task = new LoginTask();
task.execute();
}
private void register() {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
private void showResponse(final String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, response, Toast.LENGTH_SHORT).show();
mDialog.dismiss();
finish();
}
});
}
//得到字节输入流,将字节输入流转成String类型
public static String parseInfo(InputStream inputStream) {
BufferedReader reader = null;
String line = "";
StringBuilder response = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
response.append(line);
}
return response.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public class LoginTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String response = GetPostUtil.executeHttpGet(params,"LoginServlet");
showResponse(response);
return null;
}
}
}
这是本人毕设的Android代码,完全是可以用的。