Android中软键盘使用总结(一)
1.通过代码方式隐藏软键盘.
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
2.通过代码方式开启软件盘.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
3.如何禁止软键盘弹出.
1).在XML文件里 添加一个TextView
譬如:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"/>
注:android:focusableInTouchMode="true"一定要有
2). TextView tv = (TextView) findViewById(R.id.textview);
tv.requestFocus();
4.如何获取软键盘高度.
1).全局变量
private RelativeLayout rl;//父布局id
private int statusBarHeight;
2).
final Context context = getApplicationContext();
rl.getViewTreeObserver(). addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
// r will be populated with the coordinates of your view that area still visible.
rl.getWindowVisibleDisplayFrame(r);
int screenHeight = rl.getRootView().getHeight();
int heightDiff = screenHeight - (r.bottom - r.top);
if (heightDiff > 100)
statusBarHeight = 0;
try {
Class<?> c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
e.printStackTrace();
}
int realKeyboardHeight = heightDiff - statusBarHeight;
Log.i("print","软键盘高度: " + realKeyboardHeight);
}
});
5.如何监听软键盘的弹出与隐藏状态.
1).最外层父布局.
rl = (RelativeLayout) findViewById(R.id.rl);
2).下面这些代码不要在onCreate()方法里:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
rl.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = rl.getRootView().getHeight() - rl.getHeight();
if (heightDiff > 200) {
弹出的时候处理的逻辑
} else {
隐藏的时候处理的逻辑
}
}
});
更多相关文章
- Android 检测键盘是否显示及隐藏键盘
- Android平台mass storage相关代码
- 使用代码为textview设置drawableLeft
- [置顶] Android 2.3.5源代码 更新至android 6.0,可以下载,度娘网盘
- 关于LinearLayout布局中,子控件平分宽度
- Android应用程序键盘(Keyboard)消息处理机制分析(12)
- cocos2dx 调用java层代码