当软输入键盘可见时,我想向上滚动我的布局。我在我的 xml 中的适当位置定义了滚动视图,但是当键盘可见时,它隐藏了布局的某些部分,如按钮。我在 stackoveflow Link上读到,当活动为 FULL_SCREEN 时,滚动视图不起作用。如果是真的,那么当软输入可见时如何向上滚动布局。
问问题
4188 次
2 回答
1
使用此自定义相对布局来检测您的 xml 中的软键盘
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
/**
* RelativeLayout that can detect when the soft keyboard is shown and hidden.
*
*/
public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {
public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@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;
if (listener != null) {
listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
然后将 RelativeLayoutThatDetectsSoftKeyboard.Listener 实现到您的活动类
RelativeLayoutThatDetectsSoftKeyboard mainLayout = (RelativeLayoutThatDetectsSoftKeyboard)V.findViewById(R.id.dealerSearchView);
mainLayout.setListener(this);
@Override
public void onSoftKeyboardShown(boolean isShowing) {
if(isShowing) {
} else {
}
}
基于键盘可见性使用布局参数上下移动布局
于 2013-05-15T09:41:44.653 回答
0
你必须改变你的清单文件
在您的活动标签中
android:windowSoftInputMode="adjustPan" 添加这个。
看到这个http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
于 2013-05-15T09:15:27.000 回答