Rob*_*Rob 6 android scrollview android-edittext
我的应用程序设计使我对每个表单视图都有这种布局:视图底部的浮动按钮。我使用的是ConstraintLayout动态地设置按钮的高度总是具有相同的左/右页边距无论屏幕的宽度,所以我最终这个布局:
<ConstraintLayout>
<ScrollView>
<EditText/>
<EditText/>
<!-- ... -->
<ConstraintLayout /> (1)
</ScrollView>
<Button/>
</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
(1) 底部按钮高度的清晰视图,以便不隐藏滚动视图中将被按钮隐藏的底部视图
基本上,这就是它的样子:
现在我遇到的问题是当我点击底部的编辑文本时,例如这里的第四个:
编辑文本在键盘上向上移动,而不是浮动按钮,它经常被它隐藏。我知道我必须在编辑文本的onFocusChanged()方法中做一些事情,但我不知道是什么......
1-当焦点更改为 true 时,您可以将编辑文本移动到滚动视图的顶部:
这是代码:
java代码:
LinearLayout containerOfScrollViewViews= findViewById(R.id.containerOfScrollViewViews);
for (int i = 0; i < containerOfScrollViewViews.getChildCount(); i++) {
containerOfScrollViewViews.getChildAt(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
int[] xy = new int[2];
v.getLocationInWindow(xy);
if (hasFocus) ((ScrollView)findViewById(R.id.scrollView))
.smoothScrollBy(xy[0], xy[1]);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
app:layout_constraintTop_toBottomOf="parent"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@id/btn">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:hint="A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="D"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="E"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="F"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="H"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="J"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="K"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="Z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
2 - 或者您可以将按钮附加到滚动视图的底部,这样当键盘出现时,滚动视图和按钮都位于滚动视图上方
3-您可以聆听键盘可见性的变化并隐藏/显示您的按钮
这是此解决方案的链接: SoftKeyboard open and close Listener in an Activity in Android?
| 归档时间: |
|
| 查看次数: |
2375 次 |
| 最近记录: |