Android日常學習(實作登錄界面):
最近看到身邊太多大佬學習方面突飛猛進,深感自己不能再這么墮落下去了,準備開始寫一些Android博客來記錄自己的學習,由于自己的Android知識淺薄以及Java也學得不咋樣,純屬小白分享,大神勿噴! 今天來實作以下登錄界面:如下:

當輸入內容時,登錄按鈕會自動檢測并做出相應變化:
若密碼匹配成功,將跳轉到新activity,這里由于是剛開始寫,便傳入一個空的activity(后續寫一個GirdView加入各個模塊實作訓練功能):

布局代碼就不用多說了,這里用了相對布局嵌套線性布局來實作簡單的界面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:background="@mipmap/loginbak"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<RelativeLayout
android:id="@+id/relay"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/lout1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/user"
android:layout_width="40dp"
android:layout_height="40dp"/>
<EditText
android:id="@+id/et_user"
android:layout_gravity="center_vertical"
android:hint="請輸入賬號:"
android:background="@drawable/editlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/lout2"
android:layout_below="@id/lout1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/user"
android:layout_width="40dp"
android:layout_height="40dp"/>
<EditText
android:id="@+id/et_password"
android:layout_gravity="center_vertical"
android:hint="請輸入密碼:"
android:inputType="textPassword"
android:background="@drawable/editlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
<Button
android:id="@+id/bt_login"
android:background="@color/Edit_bak"
android:layout_centerHorizontal="true"
android:text="@string/login"
android:enabled="false"
android:layout_marginTop="20dp"
android:layout_below="@id/relay"
android:layout_width="wrap_content"
android:layout_height="40dp"/>
</RelativeLayout>
這里我給Edittext加上了圓角即顏色效果,具體做法是在res/Drawable下創建shape代碼(注意,只有在Drawable目錄新建才有shape選項),代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="15dp"/>
<solid android:color="@color/Edit_bak"/>
<stroke android:color="@color/Edit_stroke"/>
</shape>
名稱為editlayout,然后在相應的xml代碼中參考即可,
接下來實作Java邏輯代碼:
在MainActivity中:
private EditText et_user;
private EditText et_password;
private Button login;
private final String USER = "China";
private final String PASSWORD = "Chinastrong";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Checkinput();
}
這是大致邏輯,在init中去實體化各控制元件并判斷SharedPrefence中是否以及保存上上次輸入的賬戶密碼,如果是則改變登錄的點擊權限及按鈕的顏色,并把賬戶密碼匯入到控制元件中,最后實作游標移動:
private void init() {
et_user = findViewById(R.id.et_user);
et_password = findViewById(R.id.et_password);
login = findViewById(R.id.bt_login);
login.setOnClickListener(this);
//獲取第一次輸入保存的賬號和密碼 判斷Edittext是否有內容
if (!getSharedPreferences("data",Context.MODE_PRIVATE).getString("User","").equals("")) {
et_user.setText(USER);
et_password.setText(PASSWORD);
login.setEnabled(true);//設定為可點擊
login.setBackgroundColor(getResources().getColor(R.color.colorBtnEnable));
et_user.setSelection(et_user.getText().toString().length());//游標移動到最后一位
et_password.setSelection(et_password.getText().toString().length());
}
}
Checkinput()函式去時刻監視賬號,密碼兩個輸入框的狀態,并匯入相應的監控改變控制元件的邏輯函式,在afterTextChanged中去呼叫函式setBtnBg()判斷并作出回應:
private void Checkinput()//檢測兩個輸入框是否有內容輸入
{
et_user.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
setBtnBg();
}
});
et_password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
setBtnBg();
}
});
}
setBtnBg()代碼,若兩者都有內容,則相應改變登錄控制元件狀態:
private void setBtnBg()
{
if(et_user.getText().length()>0&&et_password.getText().length()>0) {
login.setEnabled(true);
login.setBackgroundColor(getResources().getColor(R.color.colorBtnEnable));
}
else
{
login.setEnabled(false);
login.setBackgroundColor(getResources().getColor(R.color.Edit_bak));
}
}
最后實作登錄點擊事件及SharedPrefence的寫入,這里我讓activity繼承了View.OnClickListener介面,并實作了相應方法:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
if (et_user.getText().toString().equals(USER) && et_password.getText().toString().equals(PASSWORD)) {
getSharedPreferences("data", Context.MODE_PRIVATE)
.edit()
.putString("User",et_user.getText().toString())
.putString("password",et_user.getText().toString())
.apply();
Toast.makeText(this,"兩秒后將執行跳轉",Toast.LENGTH_LONG).show();
final Timer time = new Timer();//實作延時三秒跳轉
time.schedule(new TimerTask() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
}, 2000);
}
else
Toast.makeText(this,"賬戶或密碼錯誤",Toast.LENGTH_LONG).show();
break;
}
}
與默認賬號密碼匹配,若成功,獲取SharedPrefence并實作寫入操作,并實作跳轉(這里寫入了一個空activity,后續將寫入更多訓練模塊進去),若匹配不成功,則提示密碼或賬號錯誤,
關于實作登錄界面的介紹就到這里,本人屬于小白水平,歡迎提出批評意見,
后續將繼續更新日常訓練內容,,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/234276.html
標籤:其他
