安卓UI練習(一)--登陸介面和邏輯程式碼
阿新 • • 發佈:2018-12-31
登陸介面和邏輯程式碼
最近感覺自己安卓方面真的的差的好多,,,但是我覺得自己只有好好每天學習敲程式碼練習,會有收穫的,這周就是重新看了下安卓UI方面的東西,下面是登陸介面的xml和邏輯處理方面的程式碼//參考了好多別人的部落格
效果圖
圖示都是在網上找的,推薦阿里巴巴向量圖示庫這個網站網站
介面雖然簡單,但是考慮了好多的問題
密碼的暗文輸入
控制元件的隱藏
文字監聽器TextWatcher的用法
各種邏輯,,,,
xml的程式碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:filterTouchesWhenObscured="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id ="@+id/login_layout"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/usename_layout"
android:layout_marginTop="55dp"
android:gravity="center">
<EditText
android:layout_width="fill_parent"
android:layout_height="40dp"
android:id="@+id/usename"
android:layout_marginTop="5dp"
android:inputType="number"
android:paddingRight="60dp"
android:maxLength="20"
android:paddingLeft="55dp"/>
<ImageView
android:layout_width="31dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_gravity="left|center_horizontal"
android:src="@drawable/usename"
android:layout_marginLeft="8dp"
android:visibility="visible"/>
<TextView
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_gravity="left|center_vertical"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="+62"
android:textSize="18sp"
android:visibility="invisible"/>
<Button
android:layout_width="31dp"
android:layout_height="35dp"
android:id="@+id/bt_usename_clear"
android:layout_gravity="right|center_horizontal"
android:layout_marginRight="10dp"
android:background="@drawable/sw"
android:visibility="invisible"/>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/usecode_layout"
android:layout_below="@id/usename_layout"
android:layout_marginTop="6dp"
android:gravity="center" >
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:inputType="textPassword"
android:maxLength="20"
android:paddingLeft="55dp"
android:paddingRight="60dp" />
<ImageView
android:layout_width="31dp"
android:layout_height="35dp"
android:layout_marginStart="8dp"
android:layout_gravity="left|center_vertical"
android:src="@drawable/ss"
android:layout_marginLeft="8dp" />
<Button
android:layout_width="23dp"
android:layout_height="23dp"
android:id="@+id/bt_pwd_eyes"
android:background="@drawable/noeye"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
/>
<Button
android:layout_width="23dp"
android:layout_height="23dp"
android:id="@+id/bt_pwd_clear"
android:background="@drawable/sw"
android:visibility="invisible"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="33dp"/>
</FrameLayout>
<Button
android:id="@+id/login"
android:layout_width="fill_parent"
android:layout_height="44dp"
android:layout_below="@id/usecode_layout"
android:layout_marginTop="30dp"
android:background="#ff336699"
android:textColor="@android:color/white"
android:gravity="center"
android:text="登入" />
<Button
android:id="@+id/login_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/login"
android:layout_below="@id/login"
android:background="#00000000"
android:text="忘記密碼"
android:textSize="16sp" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/login"
android:layout_below="@id/login"
android:background="#00000000"
android:gravity="left|center_vertical"
android:text="註冊"
android:textSize="16sp"
android:visibility="visible" />
</RelativeLayout>
</RelativeLayout>
在佈局方面總的是一個大的相對佈局,然後先是一個幀佈局佈置使用者賬號輸入那行,然後再是一個幀佈局佈置密碼輸入那行的佈局,然後登陸,註冊,忘記密碼放好就行
邏輯activity的程式碼
因為在登陸的過程中我們的控制元件的隱藏和出現是UI變化,,,然後不能放在主執行緒裡面進行UI的變化,然後我們就非同步處理這個UI 先定義一群常量作為標註,然後new一個Handler的物件,重寫handleMassage()方法 對具體的Message處理邏輯
package com.example.hasee.uidome;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 這個就是登陸介面的activity
* 註冊 登陸 忘記密碼 要考慮這幾種情況 轉其他activity
*/
public class Main2Activity extends AppCompatActivity implements View.OnClickListener,View.OnLongClickListener{
//宣告控制元件物件
private EditText et_name,et_pass;
private Button mLoginButton,mLoginError,mRegister,ONLYTEST;
int slectIndx = 1;
int tempSelect = slectIndx;
private boolean flase;
boolean isReLogin = flase;
private int SERVER_FLAG = 0;
private RelativeLayout countryselsct;
private TextView county_phone_sn,countryName;
private final static int LOGIN_ENABLE = 0x01;
private final static int LOGIN_UNABLF = 0x02;
private final static int PASS_ERROR = 0x03;
private final static int NAME_ERROR = 0x04; //上面是訊息的常量值
final Handler UiMangerHandler = new Handler(){ //處理UI的操作的
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case LOGIN_ENABLE:
mLoginButton.setClickable(true);
break;
case LOGIN_UNABLF:
mLoginButton.setClickable(false);
break;
case PASS_ERROR:
break;
case NAME_ERROR:
break;
}
super.handleMessage(msg);
}
};
private Button bt_username_clear;
private Button bt_pwd_clear;
private Button bt_pwd_eye;
private TextWatcher username_watcher;
private TextWatcher password_watcher; //文字監視器
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
et_name = (EditText) findViewById(R.id.bt_usename_clear);
et_pass = (EditText) findViewById(R.id.bt_pwd_clear);
bt_pwd_clear = (Button)findViewById(R.id.bt_usename_clear);
bt_pwd_clear = (Button)findViewById(R.id.bt_pwd_clear);
bt_pwd_eye = (Button)findViewById(R.id.bt_pwd_eyes);
bt_username_clear.setOnClickListener(this);
bt_pwd_eye.setOnClickListener(this);
bt_pwd_clear.setOnClickListener(this);
initWatcher();
et_name.addTextChangedListener(username_watcher);
et_pass.addTextChangedListener(password_watcher);
mLoginButton = (Button) findViewById(R.id.login);
mLoginError = (Button) findViewById(R.id.login_error);
mRegister = (Button) findViewById(R.id.register);
//ONLYTEST = (Button) findViewById(R.id.registfer);
ONLYTEST.setOnClickListener(this);
ONLYTEST.setOnLongClickListener((View.OnLongClickListener) this);
mLoginButton.setOnClickListener(this);
mLoginError.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
private void initWatcher(){
username_watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
et_pass.setText("");
if (editable.toString().length()>0){
bt_username_clear.setVisibility(View.VISIBLE);
}else{
bt_username_clear.setVisibility(View.INVISIBLE);
}
}
};
password_watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (editable.toString().length()>0){
bt_pwd_clear.setVisibility(View.VISIBLE);
}else{
bt_pwd_clear.setVisibility(View.INVISIBLE);
}
}
};
}
@Override
public void onClick(View view) {
switch (view.getId()){
//登陸activity
case R.id.login:
//login();
break;
//忘記密碼
case R.id.login_error:
break;
//註冊
case R.id.register:
break;
}
}
private void login(){
}
@Override
public boolean onLongClick(View view) {
switch (view.getId()){
case R.id.register:
if(SERVER_FLAG > 9){
break;
}
}
return true;
}
/**
* 監聽back的那塊
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if (isReLogin){
}
}
return super.onKeyDown(keyCode, event);
}
}
TextWatcher的用法
就三個方法,先用繫結 et_name.addTextChangedListener(username_watcher);
et_pass.addTextChangedListener(password_watcher);
然後進行邏輯處理
TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {//表示最終內容
Log.d("afterTextChanged", s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start/*開始的位置*/, int count/*被改變的舊內容數*/, int after/*改變後的內容數量*/) {
//這裡的s表示改變之前的內容,通常start和count組合,可以在s中讀取本次改變欄位中被改變的內容。而after表示改變後新的內容的數量。
}
@Override
public void onTextChanged(CharSequence s, int start/*開始位置*/, int before/*改變前的內容數量*/, int count/*新增數*/) {
//這裡的s表示改變之後的內容,通常start和count組合,可以在s中讀取本次改變欄位中新的內容。而before表示被改變的內容的數量。
}
};
注意
這個只是我練習的Dome,,,不保證能用,好多都沒寫玩,,就是個殼子