日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 键盘点击事件监听事件,Android 键盘事件触发以及监听

發布時間:2025/3/15 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 键盘点击事件监听事件,Android 键盘事件触发以及监听 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Android 手動顯示和隱藏軟鍵盤

如果輸入法在窗口上已經顯示,則隱藏,反之則顯示

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

view為接受軟鍵盤輸入的視圖,SHOW_FORCED表示強制顯示

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //強制隱藏鍵盤

調用隱藏系統默認的輸入法

// (WidgetSearchActivity是當前的Activity)

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

獲取輸入法打開的狀態

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開

二、Android 鍵盤彈起和回落事件監聽

AndroidMainfest.xml

android:name=".MainActivity"

android:windowSoftInputMode="stateAlwaysHidden|adjustResize">

MainActivity

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.LinearLayout;

import android.widget.Toast;

/**

* 捕捉界面鍵盤彈起動作

* 例如 京東金融 登陸界面 在輸入框獲取焦點后鍵盤彈出把整個布局上移

* 鍵盤回落后布局也相應回落

*

*/

public class MainActivity extends AppCompatActivity {

private LinearLayout root_view;

private int screenHeight = 0;

private int keyHeight = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initListener();

initOtherData();

}

private void initView() {

root_view = (LinearLayout) findViewById(R.id.root_view);

}

private void initListener() {

root_view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

@Override

public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {

Toast.makeText(MainActivity.this,"鍵盤彈起",Toast.LENGTH_SHORT).show();

} else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {

Toast.makeText(MainActivity.this,"鍵盤落下",Toast.LENGTH_SHORT).show();

}

}

});

}

private void initOtherData() {

screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();

keyHeight = screenHeight / 3;

}

}

activity_main.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/root_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center_horizontal"

android:orientation="vertical"

tools:context="open.ppdai.com.keyboardlogin.MainActivity">

你也可以看這里

和這里

總結

以上是生活随笔為你收集整理的android 键盘点击事件监听事件,Android 键盘事件触发以及监听的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。