点击编辑框,软键盘右下角有不同的图标
常用属性 | 说明 |
---|---|
actionUnspecified | 切换到下一项按钮(默认) |
actionNone | 同上 |
actionGo | GO按钮 |
actionSearch | 搜索按钮 |
actionSend | 发送按钮 |
actionNext | 下一个按钮 |
actionDone | 完成按钮 |
1 //singleLine和inputType属性至少设置一个,imeOptions才能起作用
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:singleLine="true"
android:inputType="text"/>
//以上属性也可在代码中设置
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setSingleLine(true);
2 //设置监听
EditText editText = (EditText) contentView.findViewById(R.id.editText);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
... //点击软键盘上搜索按钮后的处理逻辑
return true; //类似拦截事件
}
return false;
}
});
windowSoftInputMode弹出设置
属性 | 说明 |
---|---|
adjustNothing | 不调整,直接覆盖 |
adjustPan | 如果焦点位置距离屏幕底部距离小于软键盘高度,则把整个布局顶上去到焦点位置,不压缩多余空间,否则同adjustNothing |
adjustResize | 运行调整窗口大小,以便输入方法不会覆盖其内容 |
adjustUnspecified | 由系统决定(默认配置) |
windowSoftInputMode显示方式
属性 | 说明 |
---|---|
stateAlwaysHidden | 即使EditText获取焦点依旧隐藏 |
stateAlwaysVisible | 打开页面立即显示 |
stateHidden | 默认隐藏,EditText获取焦点才显示 |
stateUnchanged | |
stateUnspecified | |
stateVisible |
//AndroidMainfest.xml中的Activity标签下配置
android:windowSoftInputMode ="stateHidden | adjustResize">
//java中配置
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
如何解决ListView高度小于0时出现的UI问题
解决方法:如果高度小于等于0,那么将ListView设定为不可见
public class TxrjListView extends ListView {
public int count = 0;
public TxrjListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh{
super.onSizeChanged(w, h, oldw, oldh);
Log.e("txrjsms " + count++, "=>TxrjListView.onSizeChanged called! w=" + w + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh);
// 高度小于等于0不可见,高度大于0可见。
setVisibility(h <= 0 ? View.INVISIBLE : View.VISIBLE);
}
}
底部控件或其他自定义控件随着软键盘的弹出,被顶上去
解决方案
//方法一,设置windowSoftInputMode属性
android:windowSoftInputMode="adjustResize|stateHidden"
//方法二,动态设置控件高度
//rootView为RelativeLayout子类,button等LinearLayout子类,直接设置可能会报类型转换错误。解决方法:给button等控件包裹一层RelativeLayout,其他情况同理
rootView = (RelativeLayout) getView().findViewById(R.id.root_view);
rootView.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 (bottom - oldBottom < -1) {
//软键盘弹上去了,动态设置高度为0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,0);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
liBottom.setLayoutParams(params);
} else if (bottom - oldBottom > 1) {
//软键盘弹下去了,恢复原先控件高度
//如关闭软键盘,按钮消失。可自定义hanlder,延时设置按钮高度
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int) DpPxUtil.dp2px(context, bottomHight));
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
liBottom.setLayoutParams(params);
}
}
});
点击EditText 不让其弹出键盘
mEditText.setInputType(InputType.TYPE_NULL); //不让其获取焦点
待更新