如何在Android上關閉或隱藏虛擬鍵盤?


在Android中,有些情況下,我們需要強制關閉Android預設鍵盤。這個例子可以幫助你做到這一點。

步驟1 − 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。

步驟2 − 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <EditText  
      android:id = "@+id/editext"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content">
   </EditText>
   <Button
      android:id = "@+id/button"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:text = "Click here to hide"
      app:layout_constraintBottom_toBottomOf = "parent"
      app:layout_constraintLeft_toLeftOf = "parent"
      app:layout_constraintRight_toRightOf = "parent"
      app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout>

步驟3− 將以下程式碼新增到 src/MainActivity.java

import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   Handler mHandler;
   ProgressDialog mProgressBar;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button = findViewById(R.id.button);
      EditText editext = findViewById(R.id.editext);
      editext.requestFocus();
      button.setOnClickListener(this);
   }
   @RequiresApi(api = Build.VERSION_CODES.O)
   @Override
   public void onClick(View v) {
      switch (v.getId()) {
         case R.id.button:
            hideKeybaord(v);
            break;
      }
   }
   private void hideKeybaord(View v) {
      InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
      inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
   }
}

在上面的程式碼中,當您點選按鈕時,它將隱藏鍵盤。要隱藏鍵盤,請使用以下程式碼。

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);

步驟5 − 將以下程式碼新增到清單檔案,如下所示。

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication">
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity android:name = ".MainActivity" android:windowSoftInputMode = "stateAlwaysVisible">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

在上面的程式碼中包含windowSoftInputMode,這意味著當請求編輯文字焦點時,它將始終顯示鍵盤。

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟您專案中的一個活動檔案,然後單擊執行播放圖示 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 −

Keyboard

現在點選按鈕。它將隱藏鍵盤,如下所示 −

Closed Keyboard

更新於:2020年6月25日

5K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.