如何在Android中捕獲虛擬鍵盤顯示/隱藏事件?


簡介

在Android應用程式中,當用戶點選任何文字輸入欄位時,經常會彈出鍵盤。在某些情況下,我們必須限制使用者透過選單輸入文字輸入欄位中的資料。在這種情況下,我們必須顯式地隱藏該文字輸入欄位的鍵盤。在這篇文章中,我們將瞭解如何在Android應用程式中捕獲虛擬鍵盤的顯示/隱藏事件。

實現

我們將建立一個簡單的應用程式,其中我們將顯示一個簡單的TextView,用於顯示應用程式的標題。在這個TextView之後,我們將建立一個EditText,用於在我們的應用程式中開啟虛擬鍵盤。在開啟和關閉鍵盤時,我們將顯示吐司訊息。我們將遵循分步指南來實現這一點。

步驟1:在Android Studio中建立一個新專案

導航到Android Studio,如下圖所示。在下面的螢幕中,單擊“新建專案”以建立一個新的Android Studio專案。

單擊“新建專案”後,您將看到下面的螢幕。

在這個螢幕中,我們只需選擇“Empty Activity”,然後單擊“Next”。單擊“Next”後,您將看到下面的螢幕。

在這個螢幕中,我們只需指定專案名稱。然後包名將自動生成。

注意 - 請確保將語言選擇為Java。

指定所有詳細資訊後,單擊“Finish”以建立一個新的Android Studio專案。

專案建立完成後,我們將看到開啟的兩個檔案:activity_main.xml和MainActivity.java檔案。

步驟3:使用activity_main.xml

導航到activity_main.xml。如果此檔案不可見,則開啟此檔案。在左側窗格中導航到app>res>layout>activity_main.xml以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋,以便詳細瞭解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idCLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- text view for displaying  heading of the application -->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idEdtMessage"
      android:layout_margin="10dp"
      android:padding="5dp"
      android:text="Detect Show/Hide events for Virtual Keyboard"
      android:textAlignment="center"
      android:textAllCaps="false"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- creating an edit text to open and close virtual keyboard -->
   <EditText
      android:id="@+id/idEdtMessage"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:hint="Enter your message" />
</RelativeLayout>

說明 - 在上面的程式碼中,我們建立了一個RelativeLayout作為根佈局,並在其中建立了我們的TextView,用於顯示應用程式的標題。在這個TextView之後,我們將建立一個EditText,點選此EditText將開啟鍵盤。

步驟4:使用MainActivity.java檔案

導航到MainActivity.java。如果此檔案不可見,則開啟此檔案。在左側窗格中導航到app>res>layout>MainActivity.java以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋,以便詳細瞭解。

package com.example.androidjavaapp;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line creating a variable for button and edit text.
   private RelativeLayout relativeLayout;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line we are initializing variables for relative layout.
      relativeLayout = findViewById(R.id.idCLayout);
      
      // on below line we are calling tree observer to get the tree.
      relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
         @Override
         public void onGlobalLayout() {
            
            // on below line we are creating a variable for rect
            Rect rect = new Rect();
            
            // on below line getting frame for our relative layout.
            relativeLayout.getWindowVisibleDisplayFrame(rect);
            
            // on below line getting screen height for relative layout.
            int screenHeight = relativeLayout.getRootView().getHeight();
            
            // on below line getting keypad height.
            int keypadHeight = screenHeight - rect.bottom;
            
            // on below line we are checking if keypad height is greater than screen height.
            if (keypadHeight > screenHeight * 0.15) {
            
            // displaying toast message as keyboard showing.
            Toast.makeText(MainActivity.this, "Keyboard is showing", Toast.LENGTH_LONG).show();
            } else {
               
               // displaying toast message as keyboard closed.
               Toast.makeText(MainActivity.this, "keyboard closed", Toast.LENGTH_LONG).show();
            }
         }
      });
   }
}

說明 - 在上面的程式碼中,我們首先為RelativeLayout建立變數。在onCreate方法中,我們使用在activity_main.xml檔案中指定的id初始化listview變數。

之後,我們為RelativeLayout新增一個全域性佈局監聽器。在全域性佈局方法中,我們為rect建立一個變數。然後,我們新增一個條件來檢查鍵盤高度是否大於螢幕高度。如果大於,則表示鍵盤可見,在這種情況下,我們將顯示鍵盤可見的訊息。在else條件中,我們顯示一個吐司訊息,表示鍵盤隱藏。

新增上述程式碼後,我們只需單擊頂部欄中的綠色圖示即可在移動裝置上執行我們的應用程式。

注意 - 請確保已連線到您的真實裝置或模擬器。

輸出

結論

在上面的教程中,我們瞭解瞭如何在Android應用程式中捕獲虛擬鍵盤的顯示和隱藏。

更新於:2023年3月30日

1K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.