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

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

How to disable scrolling ViewPager while keyboard is shown?

问题

我正在开发一个应用程序,并在其中使用ViewPager。它可以滚动片段,其中一个片段包含文本输入字段。问题是:当键盘显示时,我仍然可以滚动片段。有人遇到过相同的问题吗?

这是我的主活动代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:id="@+id/activity_main"
  8. tools:context=".activities.MainActivity">
  9. <androidx.viewpager.widget.ViewPager
  10. android:id="@+id/view_pager"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_above="@id/navigation"
  14. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  15. <com.google.android.material.bottomnavigation.BottomNavigationView
  16. android:id="@+id/navigation"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_alignParentBottom="true"
  20. android:background="?android:attr/windowBackground"
  21. app:itemBackground="@color/bgBottomNavigation"
  22. android:foreground="?attr/selectableItemBackground"
  23. app:itemIconTint="@android:color/white"
  24. app:itemTextColor="@android:color/white"
  25. app:menu="@menu/navigation" />
  26. </RelativeLayout>

这是片段代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".fragments.Login">
  6. <ImageView
  7. android:id="@+id/loginLogo"
  8. android:layout_width="match_parent"
  9. android:layout_height="300dp"
  10. android:src="@mipmap/logo_foreground"
  11. tools:ignore="ContentDescription" />
  12. <EditText
  13. android:id="@+id/loginUsername"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:ems="15"
  17. android:inputType="textPersonName"
  18. android:layout_margin="10dp"
  19. android:layout_below="@+id/loginLogo"
  20. android:layout_centerHorizontal="true"/>
  21. <EditText
  22. android:id="@+id/loginPassword"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:ems="15"
  26. android:inputType="textPassword"
  27. android:layout_margin="10dp"
  28. android:layout_below="@+id/loginUsername"
  29. android:layout_centerHorizontal="true"/>
  30. <Button
  31. android:id="@+id/button"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_below="@id/loginPassword"
  35. android:layout_centerHorizontal="true"
  36. android:layout_margin="10dp"
  37. android:text="Войти" />
  38. </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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. android:id=&quot;@+id/activity_main&quot;
  8. tools:context=&quot;.activities.MainActivity&quot;&gt;
  9. &lt;androidx.viewpager.widget.ViewPager
  10. android:id=&quot;@+id/view_pager&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;match_parent&quot;
  13. android:layout_above=&quot;@id/navigation&quot;
  14. app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;
  15. &lt;com.google.android.material.bottomnavigation.BottomNavigationView
  16. android:id=&quot;@+id/navigation&quot;
  17. android:layout_width=&quot;match_parent&quot;
  18. android:layout_height=&quot;wrap_content&quot;
  19. android:layout_alignParentBottom=&quot;true&quot;
  20. android:background=&quot;?android:attr/windowBackground&quot;
  21. app:itemBackground=&quot;@color/bgBottomNavigation&quot;
  22. android:foreground=&quot;?attr/selectableItemBackground&quot;
  23. app:itemIconTint=&quot;@android:color/white&quot;
  24. app:itemTextColor=&quot;@android:color/white&quot;
  25. app:menu=&quot;@menu/navigation&quot; /&gt;
  26. &lt;/RelativeLayout&gt;

And this is the fragment code:

  1. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  2. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  3. android:layout_width=&quot;match_parent&quot;
  4. android:layout_height=&quot;match_parent&quot;
  5. tools:context=&quot;.fragments.Login&quot;&gt;
  6. &lt;ImageView
  7. android:id=&quot;@+id/loginLogo&quot;
  8. android:layout_width=&quot;match_parent&quot;
  9. android:layout_height=&quot;300dp&quot;
  10. android:src=&quot;@mipmap/logo_foreground&quot;
  11. tools:ignore=&quot;ContentDescription&quot; /&gt;
  12. &lt;EditText
  13. android:id=&quot;@+id/loginUsername&quot;
  14. android:layout_width=&quot;wrap_content&quot;
  15. android:layout_height=&quot;wrap_content&quot;
  16. android:ems=&quot;15&quot;
  17. android:inputType=&quot;textPersonName&quot;
  18. android:layout_margin=&quot;10dp&quot;
  19. android:layout_below=&quot;@+id/loginLogo&quot;
  20. android:layout_centerHorizontal=&quot;true&quot;/&gt;
  21. &lt;EditText
  22. android:id=&quot;@+id/loginPassword&quot;
  23. android:layout_width=&quot;wrap_content&quot;
  24. android:layout_height=&quot;wrap_content&quot;
  25. android:ems=&quot;15&quot;
  26. android:inputType=&quot;textPassword&quot;
  27. android:layout_margin=&quot;10dp&quot;
  28. android:layout_below=&quot;@+id/loginUsername&quot;
  29. android:layout_centerHorizontal=&quot;true&quot;/&gt;
  30. &lt;Button
  31. android:id=&quot;@+id/button&quot;
  32. android:layout_width=&quot;wrap_content&quot;
  33. android:layout_height=&quot;wrap_content&quot;
  34. android:layout_below=&quot;@id/loginPassword&quot;
  35. android:layout_centerHorizontal=&quot;true&quot;
  36. android:layout_margin=&quot;10dp&quot;
  37. android:text=&quot;Войти&quot; /&gt;

</RelativeLayout>

答案1

得分: 0

好的,以下是翻译好的部分:

问题的解决方法如下:我创建了一个小函数来隐藏键盘

  1. public static void hideKeyboard(Activity activity) {
  2. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  3. // 找到当前获得焦点的视图,以便我们可以从中获取正确的窗口标记。
  4. View view = activity.getCurrentFocus();
  5. // 如果当前没有视图获得焦点,就创建一个新的视图,这样我们就可以从中获取窗口标记。
  6. if (view == null) {
  7. view = new View(activity);
  8. }
  9. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  10. }

然后在 ViewPager 的监听器中使用:

  1. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  2. @Override
  3. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  4. hideKeyboard(activity);
  5. }
  6. @Override
  7. public void onPageSelected(int position) {
  8. if (prevMenuItem != null)
  9. prevMenuItem.setChecked(false);
  10. else
  11. navigation.getMenu().getItem(0).setChecked(false);
  12. navigation.getMenu().getItem(position).setChecked(true);
  13. prevMenuItem = navigation.getMenu().getItem(position);
  14. }
  15. @Override
  16. public void onPageScrollStateChanged(int state) {
  17. }
  18. });
英文:

Well, the problem was solved the next way: I just created a little function to hide the keyboard

  1. public static void hideKeyboard(Activity activity) {
  2. InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  3. //Find the currently focused view, so we can grab the correct window token from it.
  4. View view = activity.getCurrentFocus();
  5. //If no view currently has focus, create a new one, just so we can grab a window token from it
  6. if (view == null) {
  7. view = new View(activity);
  8. }
  9. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  10. }

And then used the listener of ViewPager:

  1. viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  2. @Override
  3. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  4. hideKeyboard(activity);
  5. }
  6. @Override
  7. public void onPageSelected(int position) {
  8. if (prevMenuItem != null)
  9. prevMenuItem.setChecked(false);
  10. else
  11. navigation.getMenu().getItem(0).setChecked(false);
  12. navigation.getMenu().getItem(position).setChecked(true);
  13. prevMenuItem = navigation.getMenu().getItem(position);
  14. }
  15. @Override
  16. public void onPageScrollStateChanged(int state) {
  17. }
  18. });

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:

确定