Android_開發(fā)_Day27_鍵盤焦點和第三方庫

Android_開發(fā)Day27鍵盤焦點和第三方庫

目的:

學(xué)會使用第三方庫來解決問題财异,學(xué)會焦點的監(jiān)聽即與之相關(guān)的鍵盤的隱藏彈出

技術(shù):

<1> 第三方庫的使用方法:

先到GitHub上去找一個第三方庫院刁,比如這里我們要找一個虛化庫,因此只要搜索blur就行了撑螺,然后找一個你喜歡的點進去看使用說明里伯,如下圖:


第三方庫的說明.png

從中我們知道需要加上一行代碼城瞎,因此我們回到Android studio去添加代碼如下圖路徑:


加入位置.png

代碼添加的位置.png

代碼添加完成后點擊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);
    }
}

實際使用效果:

按鈕不可點擊.jpg

按鈕可點擊.jpg

總結(jié):

監(jiān)聽器的合理使用可以實時的監(jiān)控和改變控件的狀態(tài),因此當我們要完成一個需要實時的改變和監(jiān)聽某個控件的狀態(tài)的時候就可以仿照監(jiān)聽器的原理來實時改變悉盆。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末盯荤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子焕盟,更是在濱河造成了極大的恐慌秋秤,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脚翘,死亡現(xiàn)場離奇詭異灼卢,居然都是意外死亡,警方通過查閱死者的電腦和手機来农,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門鞋真,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人沃于,你說我怎么就攤上這事灿巧。” “怎么了揽涮?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長饿肺。 經(jīng)常有香客問我蒋困,道長,這世上最難降的妖魔是什么敬辣? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任雪标,我火速辦了婚禮零院,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘村刨。我一直安慰自己告抄,他們只是感情好,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布嵌牺。 她就那樣靜靜地躺著打洼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪逆粹。 梳的紋絲不亂的頭發(fā)上募疮,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音僻弹,去河邊找鬼阿浓。 笑死,一個胖子當著我的面吹牛蹋绽,可吹牛的內(nèi)容都是我干的芭毙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼卸耘,長吁一口氣:“原來是場噩夢啊……” “哼退敦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鹊奖,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤苛聘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后忠聚,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體设哗,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年两蟀,在試婚紗的時候發(fā)現(xiàn)自己被綠了网梢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡赂毯,死狀恐怖战虏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情党涕,我是刑警寧澤烦感,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站膛堤,受9級特大地震影響手趣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜肥荔,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一绿渣、第九天 我趴在偏房一處隱蔽的房頂上張望朝群。 院中可真熱鬧,春花似錦中符、人聲如沸姜胖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽右莱。三九已至,卻和暖如春吧凉,著一層夾襖步出監(jiān)牢的瞬間隧出,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工阀捅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留胀瞪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓饲鄙,卻偏偏與公主長得像凄诞,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子忍级,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容