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" android:id = "@+id/rootview" 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.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
EditText editText=findViewById(R.id.editext);
editText.requestFocus();
constraintLayout=findViewById(R.id.rootview);
constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
constraintLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = constraintLayout.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
if (keypadHeight > screenHeight * 0.15) {
Toast.makeText(MainActivity.this,"Keyboard is showing",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"keyboard closed",Toast.LENGTH_LONG).show();
}
}
});
button.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
hideSoftkeybard(v);
break;
}
}
private void hideSoftkeybard(View v) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
} 上記のコードでは、viewTreeObserverリスナーを使用して、特定のアクティビティでのビューグループの変更を検索しました。そのオブザーバーでは、次のコードを使用してルート要素の高さを見つけました-
int screenHeight = constraintLayout.getRootView().getHeight();
次に、以下に示すようにキーボードの高さを見つける必要があります
Rect r = new Rect(); constraintLayout.getWindowVisibleDisplayFrame(r); int keypadHeight = screenHeight - r.bottom;
次に、以下に示すように、キーボードの高さとルートビューの高さを比較する必要があります
if (keypadHeight > screenHeight * 0.15) {
Toast.makeText(MainActivity.this,"Keyboard is showing",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"keyboard closed",Toast.LENGTH_LONG).show();
} ステップ4 −以下に示すように、AndroidManifest.xmlファイルに次のコードを追加します
<?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をstateAlwaysVisibleとして追加しました。 edittextがリクエストに焦点を合わせている場合、キーボードが表示されるので便利です。
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[実行]をクリックします ツールバーの
アイコン。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します
上記の出力では、キーボードが表示されている場合、キーボードが表示されているときにメッセージが表示されます。
ボタンをクリックして、キーボードを非表示にします。上記のようにキーボードを閉じるとメッセージが表示されます。
-
Android Phone でキーボードのサイズを変更する方法
お気付きかもしれませんが、人々は大きな電話画面を好む傾向にあります。シックな見た目だけでなく、年配の方からの視認性も格段にアップ。ただし、画面を拡大すると、片手で入力する習慣のあるユーザーにとって問題が生じました。しかし、ありがたいことに、この問題に対処するための解決策があります。この投稿では、Android フォンでキーボードのサイズを変更する方法をいくつか紹介します。 キーボードのサイズを変更するには、いくつかの方法があります。拡大して見やすく正確に入力するか、サイズを縮小して片手で入力しやすくすることができます。それはすべて、あなたが何に慣れているかによって異なります。最も一般的なキー
-
Android でバッテリーの状態を確認する方法
周りのほとんどのアイテムは、何らかの形のバッテリーを使用して機能します。バッテリーは、携帯電話からリモコンに至るまで、私たちの生活に欠かせないものになり、どこにでもあります。モバイルに関して言えば、リチウムイオン電池は長時間使用すると劣化します。バッテリーの劣化は避けられず、充電時間の不規則/延長、充電容量の低下、発熱の問題、排水不良、バッテリーの膨張などの形で明らかになります。バッテリーとデバイス自体の寿命を延ばすには、バッテリーの状態を定期的に監視することが重要です。そしてそれを適切に維持します。この記事では、貴重なハンドヘルド デバイスを最大限に活用できるように、オンラインで Andro