Android_開發(fā)Day27鍵盤焦點和第三方庫
目的:
學(xué)會使用第三方庫來解決問題财异,學(xué)會焦點的監(jiān)聽即與之相關(guān)的鍵盤的隱藏彈出
技術(shù):
<1> 第三方庫的使用方法:
先到GitHub上去找一個第三方庫院刁,比如這里我們要找一個虛化庫,因此只要搜索blur就行了撑螺,然后找一個你喜歡的點進去看使用說明里伯,如下圖:
從中我們知道需要加上一行代碼城瞎,因此我們回到Android studio去添加代碼如下圖路徑:
代碼添加完成后點擊Sync Now然后系統(tǒng)會自動下載你要的那個第三方庫,然后只需要在xml里面使用就可以了疾瓮,使用方法還得看作者的說明脖镀,看有哪些功能,怎么用這些功能狼电。
<2> 系統(tǒng)鍵盤的隱藏和顯示:
系統(tǒng)鍵盤的隱藏和顯示是專門有一個類來管理的InputMethodManager蜒灰,要先獲取該類的一個對象才能對鍵盤進行操作,因此隱藏和顯示鍵盤的步驟:
第一步:獲取系統(tǒng)的InputMethodManager
通過代碼:
//1.獲取系統(tǒng)輸入的管理器
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
來獲取
第二步:隱藏或彈出鍵盤
通過方法hideSoftInputFromWindow()來隱藏鍵盤肩碟,參數(shù)兩個强窖,一個是WindowToken類,用任意一個View對象.getWindowToken()即可削祈,第二個參數(shù)給0就行翅溺。代碼:
//2.隱藏鍵盤
inputManager.hideSoftInputFromWindow(user.getWindowToken(), 0);
通過showSoftInput()來顯示鍵盤,需要兩個參數(shù)髓抑,一個是誰要調(diào)用我的鍵盤咙崎,該控件一定是要獲取焦點,用requestFocus()方法來為該控件獲取焦點吨拍,然后第二個參數(shù)給0就行褪猛。
<3> 監(jiān)聽焦點的變化:
焦點的變化要用到監(jiān)聽器,EditText(對象).setOnFocusChangeListener()羹饰,new一個View.OnFocusChangeListener() 匿名類伊滋,實現(xiàn)public void onFocusChange(View view, boolean b)方法碳却,該方法是由程序自動調(diào)用的,參數(shù)view就是要監(jiān)聽的焦點的對象新啼,用的時候要強制轉(zhuǎn)換成相應(yīng)的對象追城,b是是否獲取焦點,以此為條件來判斷燥撞。
技術(shù)如何使用:
做一個登錄界面座柱,要求背景虛化,然后當用戶將輸入框內(nèi)的輸入內(nèi)容后才能點擊登錄按鈕物舒,否則為不可用狀態(tài)色洞,當用戶輸入密碼時有個動畫捂住眼睛,點擊其余地方時放開眼睛冠胯。
背景的虛化要用一個第三方庫火诸,輸入框的內(nèi)容需要用一個文本監(jiān)聽器來判斷是否有文本,登錄按鈕的狀態(tài)可以在xml里面配置荠察,眼睛是否捂上需要進行文本框焦點的監(jiān)聽置蜀,參考代碼如下:
xml里面配置界面和按鈕代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg_blure"
android:scaleType="fitXY"/>
<RelativeLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignTop="@id/centerInParent"
android:layout_marginTop="-77dp">
<ImageView
android:id="@+id/iv_head"
android:layout_width="200dp"
android:layout_height="108dp"
android:src="@drawable/owl_head"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/iv_leftHand"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/icon_hand"
android:layout_alignBottom="@id/iv_head"
android:layout_marginBottom="-20dp"/>
<ImageView
android:id="@+id/iv_rightHand"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/icon_hand"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/iv_head"
android:layout_marginBottom="-20dp"/>
<ImageView
android:id="@+id/iv_left_arm"
android:layout_width="65dp"
android:layout_height="40dp"
android:src="@drawable/arm_left"
android:layout_below="@id/iv_head"
/>
<ImageView
android:id="@+id/iv_right_arm"
android:layout_width="65dp"
android:layout_height="40dp"
android:src="@drawable/arm_right"
android:layout_below="@id/iv_head"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<ImageView
android:id="@+id/centerInParent"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_centerInParent="true"/>
<io.alterac.blurkit.BlurLayout
android:id="@+id/iv_back"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="@drawable/input_bg_shape"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_alignTop="@id/centerInParent"
android:layout_marginTop="20dp"
app:blk_fps="0"
app:blk_blurRadius="25"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:orientation="vertical"
android:padding="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_alignTop="@id/centerInParent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="炫酷登錄"
android:textColor="#E2B7B7"
android:textSize="20sp"
android:textAlignment="center"/>
<EditText
android:id="@+id/et_user"
style="@style/EditTextStyle"
android:hint="請輸入用戶名"
android:inputType="text"
android:drawableLeft="@drawable/icon_font_user"
/>
<EditText
android:id="@+id/et_password"
style="@style/EditTextStyle"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:drawableLeft="@drawable/icon_font_password"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="登錄"
android:textColor="#ffff"
android:layout_marginTop="20dp"
android:background="@drawable/login_shape"
android:enabled="false"/>
</LinearLayout>
</RelativeLayout>
另外加上配置登錄按鈕的xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#3C7BAD"/>
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#3F6480"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#39a4f9"/>
</shape>
</item>
</selector>
接下來就是MainActivity的代碼:
package com.example.login_demo;
import androidx.appcompat.app.AppCompatActivity;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements TextWatcher {
private EditText user;
private EditText password;
private Button loginButton;
private ImageView leftArm;
private ImageView rightArm;
private ImageView leftHand;
private ImageView rightHand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
*showSoftInput() 彈出鍵盤
* hideSoftInputFromWindow() 隱藏鍵盤
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//隱藏鍵盤
//1.獲取系統(tǒng)輸入的管理器
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
//2.隱藏鍵盤
inputManager.hideSoftInputFromWindow(user.getWindowToken(), 0);
//3.取消焦點
View focus = getCurrentFocus();//取消焦點//對應(yīng)的requestFocus()取消焦點
if (focus != null){
focus.clearFocus();
}
}
return true;
}
void initView(){
user = findViewById(R.id.et_user);
password = findViewById(R.id.et_password);
loginButton = findViewById(R.id.button);
leftArm = findViewById(R.id.iv_left_arm);
rightArm = findViewById(R.id.iv_right_arm);
leftHand = findViewById(R.id.iv_leftHand);
rightHand = findViewById(R.id.iv_rightHand);
//監(jiān)聽文本改變以判斷是否可以被點擊
user.addTextChangedListener(this);
password.addTextChangedListener(this);
//監(jiān)聽焦點的改變控制是捂住眼睛
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
//捂住眼睛
//Toast.makeText(getApplicationContext(), "捂住眼睛", Toast.LENGTH_SHORT).show();
close();
}else {
//打開
//Toast.makeText(getApplicationContext(), "放手", Toast.LENGTH_SHORT).show();
open();
}
}
});
}
@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) {
//判斷兩個輸入框有沒有內(nèi)容
if (user.getText().toString().length() > 0 && password.getText().toString().length() > 0){
//按鈕可以點擊了
loginButton.setEnabled(true);
}else {
//按鈕不可以點擊
loginButton.setEnabled(false);
}
}
public void close(){
//右邊
//RotateAnimation rAnim = new RotateAnimation(0, 120, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
RotateAnimation rAnim = new RotateAnimation(0, -162, 0, 0);
rAnim.setDuration(500);
rAnim.setFillAfter(true);
rightArm.startAnimation(rAnim);
//左邊
RotateAnimation lAnim = new RotateAnimation(0, 162, leftArm.getWidth(), 0);
lAnim.setDuration(500);
lAnim.setFillAfter(true);
leftArm.startAnimation(lAnim);
//放下手
TranslateAnimation down = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.hand_down_translate);
leftHand.startAnimation(down);
rightHand.startAnimation(down);
}
public void open(){
//右邊
//RotateAnimation rAnim = new RotateAnimation(0, 120, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
RotateAnimation rAnim = new RotateAnimation(-162, 0, 0, 0);
rAnim.setDuration(500);
rAnim.setFillAfter(true);
rightArm.startAnimation(rAnim);
//左邊
RotateAnimation lAnim = new RotateAnimation(162, 0, leftArm.getWidth(), 0);
lAnim.setDuration(500);
lAnim.setFillAfter(true);
leftArm.startAnimation(lAnim);
//手放上來
TranslateAnimation up = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.hand_up_translate);
leftHand.startAnimation(up);
rightHand.startAnimation(up);
}
}
實際使用效果:
總結(jié):
監(jiān)聽器的合理使用可以實時的監(jiān)控和改變控件的狀態(tài),因此當我們要完成一個需要實時的改變和監(jiān)聽某個控件的狀態(tài)的時候就可以仿照監(jiān)聽器的原理來實時改變悉盆。