Android
 Computer >> コンピューター >  >> プログラミング >> Android

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 ="https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://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 = "https://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からアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

Androidで仮想キーボードを閉じたり非表示にしたりするにはどうすればよいですか?

次に、ボタンをクリックします。以下に示すように、キーボードが非表示になります-

Androidで仮想キーボードを閉じたり非表示にしたりするにはどうすればよいですか?


  1. Androidでキーボードを変更する方法は?

    カスタマイズはAndroidOSの出現のバックボーンであり、そのような機能がAndroidフォンのデフォルトまたはストックキーボードを変更するとします。この変更は、Samsung、Google、Huawei、Xiaomiなどの携帯電話のブランドに関係なく行うことができます。 在庫のキーボードのほとんどは優れていますが、特定のユーザー要件については、ユーザーが別のキーボードを必要とする場合があります。たとえば、別の言語でキーボードを使用したり、方程式に数学記号を入力したりできますが、必要な機能はユーザーのデフォルトの電話キーボードではサポートされていません。 。 Androidキーボードを変

  2. Android で IP アドレスを非表示にする方法

    IPアドレスは、インターネットに関して最も一般的な用語の 1 つです。 IP アドレスについては聞いたことがあるはずですが、インターネットのバイブルにアクセスして詳細を知る時間はおそらくないでしょう。 Android で IP アドレスを隠すことに頭を悩ませる前に、あらゆるデバイスでの IP の基本と動作を知っておく必要があります。 IP とは? IP はインターネット プロトコルの略で、ウェブ ブラウジングとインターネット全体を駆動する一連のルールとプロトコルです。 IP アドレスは、ネットワークに接続されたハードウェアの一意の識別番号であり、IP ベースのネットワーク上の他の接続された