要求:
现在需要单击一行文本框以弹出先前隐藏的输入框。键入后,按回车键或其他东西隐藏键盘和输入框,然后将输入框的内容填充到文本框中。
实现:
对此要求的第一个响应是编写一个监视器来监视键盘的显示和隐藏,以控制输入框的显示和隐藏以及控制文本框的内容。
所以我做了以下事情:
指定android:windowSoftInputMode =“ adjustResize | stateAlwaysHidden”
这种方法是在键盘弹出时更改布局。当布局更改为屏幕的1/3时,让Activity实现LayoutchangeListener来监视布局更改
我们认为这是由键盘引起的。
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值
//现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起
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();
}
}
问题:
是的,确实可以监视软键盘的弹出和隐藏。所有这些都是因为先前设置了indowSoftInputMode = adjustResize,但是在全屏模式下此属性无效,并且键盘的弹出和隐藏操作都不会触发onLayouChangeListener。
在项目中使用SystemBarTintManager后,活动变为全屏模式
所以我做了以下操作
//contentlayout是最外层布局
mChildOfContent = contentlayout.getChildAt(0);
mChildOfContent.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}});
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-374183-1.html
吴亦凡