Android中软键盘使用总结(一)
来源:互联网 发布:外汇指标软件视频 编辑:程序博客网 时间:2024/05/18 03:32
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
譬如:<TextView
android:id="@+id/tv"
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 软键盘 EditText 使用总结
- android软键盘(一)
- Android软键盘总结
- android软键盘隐藏总结
- android软键盘隐藏总结
- android 隐藏软键盘总结
- android软键盘隐藏总结
- Android在dialog中使用EditText不能弹出软键盘
- android中调用软键盘
- Android 中自定义软键盘
- Android中软键盘设置
- android中关闭软键盘
- Android中软键盘监听
- Android 中自定义软键盘
- Android中软键盘监听
- android中关闭软键盘
- android软键盘的使用
- 两台主机布置lamp(php-fpm)
- 学习PHP注意事项
- Java并发编程--深入理解Semaphore
- 网络编程案例---有道字典应用
- ORB特征点提取
- Android中软键盘使用总结(一)
- Kotlin中的面向对象(二)
- js中的时间与毫秒数互相转换
- Nginx配置详解
- 程序员如何选择未来的职业路线
- 2017年四川省赛記錄與反思
- 征信良好的第一关键是:不逾期!
- 适配器模式3
- Linux sed命令详解