英文:
How to disable scrolling ViewPager while keyboard is shown?
问题
我正在开发一个应用程序,并在其中使用ViewPager。它可以滚动片段,其中一个片段包含文本输入字段。问题是:当键盘显示时,我仍然可以滚动片段。有人遇到过相同的问题吗?
这是我的主活动代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
tools:context=".activities.MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />
</RelativeLayout>
这是片段代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.Login">
<ImageView
android:id="@+id/loginLogo"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/logo_foreground"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/loginUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:layout_margin="10dp"
android:layout_below="@+id/loginLogo"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPassword"
android:layout_margin="10dp"
android:layout_below="@+id/loginUsername"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Войти" />
</RelativeLayout>
英文:
I'm developing an app and use ViewPager inside it. It scrolls fragments, one of which contains the text input field. So the problem is the following: while the keyboard is showing I still can scroll fragments. have anybody met the same problem?
This is my Main Activity code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
tools:context=".activities.MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/navigation"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />
</RelativeLayout>
And this is the fragment code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.Login">
<ImageView
android:id="@+id/loginLogo"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@mipmap/logo_foreground"
tools:ignore="ContentDescription" />
<EditText
android:id="@+id/loginUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:layout_margin="10dp"
android:layout_below="@+id/loginLogo"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPassword"
android:layout_margin="10dp"
android:layout_below="@+id/loginUsername"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/loginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Войти" />
</RelativeLayout>
答案1
得分: 0
好的,以下是翻译好的部分:
问题的解决方法如下:我创建了一个小函数来隐藏键盘
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
// 找到当前获得焦点的视图,以便我们可以从中获取正确的窗口标记。
View view = activity.getCurrentFocus();
// 如果当前没有视图获得焦点,就创建一个新的视图,这样我们就可以从中获取窗口标记。
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
然后在 ViewPager 的监听器中使用:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
hideKeyboard(activity);
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null)
prevMenuItem.setChecked(false);
else
navigation.getMenu().getItem(0).setChecked(false);
navigation.getMenu().getItem(position).setChecked(true);
prevMenuItem = navigation.getMenu().getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
英文:
Well, the problem was solved the next way: I just created a little function to hide the keyboard
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
And then used the listener of ViewPager:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
hideKeyboard(activity);
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null)
prevMenuItem.setChecked(false);
else
navigation.getMenu().getItem(0).setChecked(false);
navigation.getMenu().getItem(position).setChecked(true);
prevMenuItem = navigation.getMenu().getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论