gpt4 book ai didi

使用软键盘时不隐藏字段的Android底部操作栏

转载 作者:搜寻专家 更新时间:2023-11-01 07:57:18 25 4
gpt4 key购买 nike

我使用以下方式禁用了正常的顶部操作栏:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

& 我想使用底部操作栏来完成/取消操作,例如这个日历应用程序: enter image description here

但是当我尝试向 scrollView 中可用的 editTexts 写入内容时,底部操作栏隐藏了字段,并且我希望它像下面的日历应用程序一样可见: enter image description here

那么,我怎样才能实现类似的行为呢?(这样底部操作栏在打开软键盘时就不会隐藏任何字段),

我正在使用这样的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="vertical"
android:weightSum="1">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:id="@+id/formScrollView">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- all form fields goes here -->
</LinearLayout>

</ScrollView>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="@dimen/done_button_padding"
android:id="@+id/happeningDoneLayout">

<Button
android:id="@+id/doneButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="@string/done"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/cancelButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="@string/cancel"
android:layout_alignParentBottom="true"/>
</FrameLayout>

</RelativeLayout>

最佳答案

最简单的做法是防止在虚拟键盘出现时调整布局大小:

    <activity
android:name=".ShareFromDriveActivity_"
android:hardwareAccelerated="true"
android:label="@string/titleSharingCalendar"
android:launchMode="standard"
android:parentActivityName=".AppWidgetConfigure_"
android:screenOrientation="sensor"
android:theme="@style/Theme.Materialamberpurple"
android:windowSoftInputMode="stateHidden|adjustPan" >
<intent-filter>
<action android:name="de.kashban.android.picturecalendar.INTENT_ACTION_SHARE_FROM_DRIVE" />
</intent-filter>
</activity>

重要的一行是android:windowSoftInputMode="stateHidden|adjustPan"stateHidden 确保在启动 Activity 时键盘不会打开,即使 EditText 具有焦点也是如此。

adjustPan 是您正在寻找的:布局将不再调整大小(包括下方按钮),但键盘将覆盖布局。仍然可以将它们滚动到可见部分,但是当键盘出现时,它们不可见。

来源:Android Guides

也许仅此设置就可以帮助您解决问题。

如果这还不够并且您需要按钮真正消失,请尝试使用此方法:

// Detect soft keyboard visibility changes
final SoftKeyboardStateHelper softKeyboardStateHelper =
new SoftKeyboardStateHelper(lyt_share_from_drive_main);
softKeyboardStateHelper.addSoftKeyboardStateListener(this);

SoftKeyboardStateHelper 是 Artem Zinnatullin 的一个类,用于检测软键盘的状态变化:

/**
*
*/
package de.kashban.android.picturecalendar.util.local;

/**
* @author Artem Zinnatullin
* http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android/9108219#9108219
* Usage: final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);
* softKeyboardStateHelper.addSoftKeyboardStateListener(...);
*/
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

import java.util.LinkedList;
import java.util.List;

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {

public interface SoftKeyboardStateListener {
void onSoftKeyboardOpened(int keyboardHeightInPx);
void onSoftKeyboardClosed();
}

private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
private final View activityRootView;
private int lastSoftKeyboardHeightInPx;
private boolean isSoftKeyboardOpened;

public SoftKeyboardStateHelper(View activityRootView) {
this(activityRootView, false);
}

public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
this.activityRootView = activityRootView;
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}

@Override
public void onGlobalLayout() {
final Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);

final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
isSoftKeyboardOpened = true;
notifyOnSoftKeyboardOpened(heightDiff);
} else if (isSoftKeyboardOpened && heightDiff < 100) {
isSoftKeyboardOpened = false;
notifyOnSoftKeyboardClosed();
}
}

public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
}

public boolean isSoftKeyboardOpened() {
return isSoftKeyboardOpened;
}

/**
* Default value is zero (0)
* @return last saved keyboard height in px
*/
public int getLastSoftKeyboardHeightInPx() {
return lastSoftKeyboardHeightInPx;
}

public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
listeners.add(listener);
}

public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
listeners.remove(listener);
}

private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardOpened(keyboardHeightInPx);
}
}
}

private void notifyOnSoftKeyboardClosed() {
for (SoftKeyboardStateListener listener : listeners) {
if (listener != null) {
listener.onSoftKeyboardClosed();
}
}
}
}

在您的 Activity 中实现接口(interface) SoftKeyboardStateListener 并覆盖这些方法:

@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardOpened() called with keyboard height " + keyboardHeightInPx);
rdgVisibility.setVisibility(View.GONE);
if (tvPermissionLabel != null)
tvPermissionLabel.setVisibility(View.GONE);
lyt_ShareDriveOkCancel.setVisibility(View.GONE);
cbShareWithDev.setVisibility(View.GONE);

}

@Override
public void onSoftKeyboardClosed() {
if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardClosed() called.");
rdgVisibility.setVisibility(View.VISIBLE);
if (tvPermissionLabel != null)
tvPermissionLabel.setVisibility(View.VISIBLE);
lyt_ShareDriveOkCancel.setVisibility(View.VISIBLE);
cbShareWithDev.setVisibility(View.VISIBLE);
}

在这两种方法中,相应地更改下方按钮的可见性。完成。

这是它在我的应用中的样子:

Full Dialog Screenshot

键盘关闭,完整布局可见

Most Controls hidden Screenshot

键盘已打开,除 EditText 外的所有控件都消失了。原因是 EditText 可能跨越多行,在小屏幕上,完整布局太杂乱了。

关于使用软键盘时不隐藏字段的Android底部操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466325/

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