直接上已經實現的登錄頁面圖
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代碼,完全是可以用的。