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 键盘事件触发以及监听的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 以卖香蕉为例,从4个方面了解SQL的数据
- 下一篇: android bitmap对比,And