Android賬號登入到顯示介面
LoginActivity 繼承BaseActivity 封裝類登入成功跳轉主頁面
根據網址得出賬號與密碼
import android.content.Intent; import android.content.SharedPreferences; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;
import com.bwie.newsmonth04.base.BaseActivity; import com.bwie.newsmonth04.bean.LoginResult; import com.bwie.newsmonth04.constant.ServerUrl; import com.bwie.newsmonth04.utils.HttpUtils; import com.google.gson.Gson;
public class LoginActivity extends BaseActivity implements View.OnClickListener { public static final int FLAG = 123;
private EditText etUsername; private EditText etPassword; private Button btnLogin; private SharedPreferences sp; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case FLAG: String result = (String) msg.obj; Gson gson = new Gson(); LoginResult loginResult = gson.fromJson(result, LoginResult.class); if (loginResult != null) { if (loginResult.getCode() == 0) { sp.edit().putBoolean("is_first", false).commit(); Intent intent = new Intent(mContext, HomeActivity.class); startActivity(intent); } } break; } } }; @Override protected int getContentView() { return R.layout.activity_login; } @Override protected void initView() { etUsername = findViewById(R.id.et_username); etPassword = findViewById(R.id.et_password); btnLogin = findViewById(R.id.btn_login); } @Override protected void initData() { super.initData(); sp = getSharedPreferences("config", MODE_PRIVATE); } @Override protected void setListener() { super.setListener(); btnLogin.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: final String username = etUsername.getText().toString().trim(); final String password = etPassword.getText().toString();
// if (username.equals())
new Thread(new Runnable() {
@Override
public void run() {
String url = ServerUrl.LOGIN_URL + "mobile=" + username + "&password=" + password;
String result = HttpUtils.getStringFromHttp(url);
Message msg = Message.obtain();
msg.what = FLAG;
msg.obj = result;
handler.sendMessage(msg);
}
}).start();
break;
}
}
}
LoginResult類
package com.bwie.newsmonth04.bean;
/**
- Created by eric on 2018/9/20. */
public class LoginResult { private String msg; private int code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
佈局檔案
<?xml version="1.0" encoding="utf-8"?><EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入使用者名稱" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登入" />