如何在键盘显示时禁用滚动 ViewPager?

huangapple go评论79阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/activity_main&quot;
    tools:context=&quot;.activities.MainActivity&quot;&gt;

    &lt;androidx.viewpager.widget.ViewPager
        android:id=&quot;@+id/view_pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_above=&quot;@id/navigation&quot;
        app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;

    &lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:id=&quot;@+id/navigation&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_alignParentBottom=&quot;true&quot;
        android:background=&quot;?android:attr/windowBackground&quot;
        app:itemBackground=&quot;@color/bgBottomNavigation&quot;
        android:foreground=&quot;?attr/selectableItemBackground&quot;
        app:itemIconTint=&quot;@android:color/white&quot;
        app:itemTextColor=&quot;@android:color/white&quot;
        app:menu=&quot;@menu/navigation&quot; /&gt;

&lt;/RelativeLayout&gt;

And this is the fragment code:

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.fragments.Login&quot;&gt;

&lt;ImageView
    android:id=&quot;@+id/loginLogo&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;300dp&quot;
    android:src=&quot;@mipmap/logo_foreground&quot;
    tools:ignore=&quot;ContentDescription&quot; /&gt;

&lt;EditText
    android:id=&quot;@+id/loginUsername&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:ems=&quot;15&quot;
    android:inputType=&quot;textPersonName&quot;
    android:layout_margin=&quot;10dp&quot;
    android:layout_below=&quot;@+id/loginLogo&quot;
    android:layout_centerHorizontal=&quot;true&quot;/&gt;

&lt;EditText
    android:id=&quot;@+id/loginPassword&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:ems=&quot;15&quot;
    android:inputType=&quot;textPassword&quot;
    android:layout_margin=&quot;10dp&quot;
    android:layout_below=&quot;@+id/loginUsername&quot;
    android:layout_centerHorizontal=&quot;true&quot;/&gt;

&lt;Button
    android:id=&quot;@+id/button&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_below=&quot;@id/loginPassword&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    android:layout_margin=&quot;10dp&quot;
    android:text=&quot;Войти&quot; /&gt;

</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) {

        }
    });

huangapple
  • 本文由 发表于 2020年4月3日 22:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61014021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定