显示软键盘时向上移动布局
我试图在编辑文本获得焦点后出现软键盘时调整布局。 现在,如果我有许多编辑文本并且键盘出现,则最后的编辑文本将被隐藏,并且无法向上滚动。
这是我的布局是如何建立起来的:
模板:
<LinearLayout>
<LinearLayout>
// header 1
</LinearLayout>
<LinearLayout>
// header 1
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical">
// where I inflate view_1
</LinearLayout>
<LinearLayout>
// footer
</LinearLayout>
</LinearLayout>
查看(view_1):
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout>
// ...
</LinearLayout>
<LinearLayout>
// ...
</LinearLayout>
<LinearLayout>
<TextView/>
<EditText/>
<TextView/>
<EditText/>
<TextView/>
<EditText/>
<TextView/>
<EditText/>
</LinearLayout>
</LinearLayout>
</ScrollView>
我已经尝试了android:windowSoftInputMode
(在manifest.xml和编程上)的各种组合。 我试图在滚动视图上设置android:isScrollContainer="false"
,但没有任何结果。
我也试过这个答案,在我的滚动视图中放置一个GlobalLayoutListener,但是当键盘出现时不会调用onGlobalLayout。 而isKeyboardShown
总是假的。
我找到的最佳解决方案是在manifest.xml文件的activity <>标记中添加adjustpan
属性。
<activity
android:name="MyActivity"
android:windowSoftInputMode="adjustPan"/>
这是一个迟到的答案,但对于任何仍在寻找替代解决方案的人来说,这可能会有帮助。 我创建了一个自定义的ViewTreeObserver.OnGlobalLayoutListener
,如果您正在寻找一种方法来控制View
的位置,并且您希望确保在显示软键盘时可以看到View
的位置,则该ViewTreeObserver.OnGlobalLayoutListener
可能适合您的用例。 这是解决方案的要点。
OnGlobalLayoutListener
通过在显示键盘时将视图平滑地移动到软键盘边界上方,并在键盘被解除时返回到视图的起始位置,来动态改变视图的translationY
属性。 如果您对使用有任何疑问,请告知我。
我结束了我的方式。
我创建了一个实现OnFocusChangeListener的类来处理我的所有EditText:
public class EditTextFocusChangeListener implements OnFocusChangeListener {
private ScrollView scrollView;
public EditTextFocusChangeListener(ScrollView scrollView) {
this.scrollView = scrollView;
}
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus) {
int left = view.getLeft();
int top = view.getTop();
int bottom = view.getBottom();
int keyboardHeight = scrollView.getHeight() / 3;
// if the bottom of edit text is greater than scroll view height divide by 3,
// it means that the keyboard is visible
if (bottom > keyboardHeight) {
// increase scroll view with padding
scrollView.setPadding(0, 0, 0, keyboardHeight);
// scroll to the edit text position
scrollView.scrollTo(left, top);
}
}
}
}
然后在活动中,我为每个编辑文本设置了侦听器:
EditTextFocusChangeListener listener = new EditTextFocusChangeListener(mainScrollView);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnFocusChangeListener(listener);
editText2 = (EditText) findViewById(R.id.editText2);
editText2.setOnFocusChangeListener(listener);
...
editTextN = (EditText) findViewById(R.id.editTextN);
editTextN.setOnFocusChangeListener(listener);
对于最后一个编辑文本,我设置了EditorAction listerner来处理软键盘上的“完成”按钮 - 隐藏键盘并将滚动视图恢复到原始位置:
editTextN.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch(result) {
// user taped on keyboard DONE button
case EditorInfo.IME_ACTION_DONE:
// put the scroll view back to its original position
mainScrollView.setPadding(0, 0, 0, 0);
// hide keyboard
((InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
// remove focus from any edit text
LinearLayout scrollViewLL = (LinearLayout) mainScrollView.getChildAt(0);
scrollViewLL.requestFocus();
break;
}
return false;
}
});
最后,一种处理当用户触摸编辑文本外部以隐藏键盘并将滚动视图恢复到其原始位置(在web上发现并稍微改变以适合我的需要)的方式:
public void setupUI(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// put the scroll view back to its original position
if (v instanceof ScrollView) {
v.setPadding(0, 0, 0, 0);
LinearLayout scrollViewLL = (LinearLayout) ((ScrollView) v).getChildAt(0);
scrollViewLL.requestFocus();
}
hideKeyboard();
return false;
}
});
}
// If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
链接地址: http://www.djcxy.com/p/93429.html