在myapp中遇到了一个非常简单的问题。我有一个自定义对话框,其中包含 EditText ,每当软键盘打开时,我想在对话框布局上显示标题/另一个布局(参见带有三个文本视图的图片)。如果他点击完成。 hidethesoftkeyboard以及标题。
ettagmsg = (EditText) dialog.findViewById(R.id.etFlyTagName);
弹出标题
LinearLayout layheader = (LinearLayout)findViewById(R.layout.header_buttons);
答案 0 :(得分:1)
你可能想要添加这个监听器!
ettagmsg.setOnFocusChangeListener(new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.hasFocus()){
layheader.setVisibility(View.VISIBLE);
}else{
layheader.setVisibility(View.GONE);
//hide soft input here
}
}
}
希望我有用!
答案 1 :(得分:1)
还没有真正测试过这个,但这里有一个很好用的代码片段:http://felhr85.net/2014/05/04/catch-soft-keyboard-showhidden-events-in-android/
tl; dr:因为弹出软键盘需要一些视图变平(高度变小),你可以用它来检查软键盘是否被隐藏/显示。
答案 2 :(得分:0)
使用Listener(您的对话框)实例化它,并在onStart / onStop或类似的回调期间将其从视图中附加和分离。请记住,您要将其附加到对话视图。
另外,您可能需要调整DP_KEYBOARD_THRESHOLD
值
public class KeyboardObserver implements ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnPreDrawListener {
private static final int DP_KEYBOARD_THRESHOLD = 60;
private int keyboardThreshold;
private int currentHeight;
private View view;
private final KeyboardListener listener;
private boolean isKeyboardShown = false;
public KeyboardObserver(KeyboardListener listener) {
this.listener = listener;
}
public void attachToView(View view) {
keyboardThreshold = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DP_KEYBOARD_THRESHOLD, view.getResources().getDisplayMetrics());
this.view = view;
currentHeight = view.getHeight();
view.getViewTreeObserver().addOnGlobalLayoutListener(this);
if (currentHeight <= 0) {
view.getViewTreeObserver().addOnPreDrawListener(this);
}
}
public void detachFromView() {
if (view != null) view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
@Override
public void onGlobalLayout() {
int newHeight = view.getHeight();
if (currentHeight > 0) {
int diff = newHeight - currentHeight;
if (diff < -keyboardThreshold) {
Log.d(this, "onGlobalLayout. keyboard is show. height diff = " + -diff);
// keyboard is show
isKeyboardShown = true;
if (listener != null)
listener.onKeyboardShow(-diff);
} else if (diff > keyboardThreshold) {
Log.d(this, "onGlobalLayout.keyboard is hide. height diff = " + diff);
// keyboard is hide
isKeyboardShown = false;
if (listener != null)
listener.onKeyboardHide(diff);
} else {
Log.v(this, "onGlobalLayout. height diff = " + diff);
}
}
currentHeight = newHeight;
}
public boolean isKeyboardShown() {
return isKeyboardShown;
}
@Override
public boolean onPreDraw() {
currentHeight = view.getHeight();
view.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
public interface KeyboardListener {
public void onKeyboardShow(int height);
public void onKeyboardHide(int height);
}
}