gpt4 book ai didi

android - 如何让滑动抽屉远离虚拟键盘的顶部

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:28 26 4
gpt4 key购买 nike

我整天都在为这个问题挠头。在我的一项 Activity (也是唯一一项)中,当我调用虚拟键盘时,滑动抽屉 Handlebars 出现在它上方。我设法通过将 android:windowSoftInputMode="adjustPan"放入我的 Manafest.xml 文件中的每个 Activity (包括有问题的 Activity )来解决我应用程序中所有其他 Activity 的这个问题。据我所知, Activity 中的所有对象都没有焦点(如果有的话,我不知道如何找到它)。我已经通过使用 this.getCurrentFocus() 检查了焦点,然后在返回的 View 上执行 view.clearFocus() (如果有的话)。到目前为止,据我所知,它还没有返回 View 。

有什么想法吗?

最佳答案

这是我的解决方法.. 在包含 EditText 的 Activity 中,我有一个扩展 EditText 的子类。在这个子类中,我覆盖了检查键盘是否打开的 onMeasure() 方法。

    public static class MyEditText extends EditText {
MyActivity context;

public void setContext(MyActivity context) {
this.context = context;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity)getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
// assume all soft keyboards are at least 128 pixels high
if (diff>128)
context.showHandle(false);
else
context.showHandle(true);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

然后在 Activity 中,如果键盘打开,抽屉 handle 设置为 1px x 1px 透明图像,如果键盘隐藏,则显示真正的 handle :

private void showHandle(boolean show) {
ImageView drawer_handle = (ImageView) findViewById(R.drawable.handle);
if (show)
drawer_handle.setImageResource(R.drawable.handle);
else
drawer_handle.setImageResource(R.drawable.handle_blank);
}

最后,确保在 MyActivityonCreate() 中调用了 setContext()

MyEditText met = (MyEditText) findViewById(R.id.edit_text);
met.setContext(this);

关于android - 如何让滑动抽屉远离虚拟键盘的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4515158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com