【问题标题】:Show Android SnackBar above keyboard?在键盘上方显示 Android SnackBar?
【发布时间】:2016-01-13 07:33:36
【问题描述】:

是否可以在键盘上方显示Android Snackbar(在 Y 坐标中,而不是分层)?如果显示键盘,则 Snackbar 当前会被隐藏,这是不受欢迎的行为。

【问题讨论】:

  • 因为我们无法弄清楚这一点,所以我们现在只要显示一个snackbar(我们用于错误)就隐藏键盘
  • 如果您有键盘,通常使用普通的旧 Toast 可以获得最佳体验。这将显示在键盘上,您无需隐藏键盘(这可能会使用户感到困惑)

标签: android android-snackbar


【解决方案1】:

设置

android:windowSoftInputMode="adjustResize"

AndroidManifest.xml 中包含您的小吃店的活动

【讨论】:

  • 你如何把它放在一个活动中?
  • 它作为相关活动的属性进入 AndroidManifest.xml 文件。
  • 这个解决方案的问题是它还会调整整个屏幕,这不是我们想要的。我们只希望调整快餐栏,而不是调整整个屏幕。
  • 我不知道这是否有帮助,但我发现使用 DrawerLayuot 会导致键盘上方不显示小吃栏。如果我从活动中删除 DrawerLayout,它会按原样显示。
  • @fobbymaster,如果你说“不是我们想要的东西”,你为什么要把它标记为答案?
【解决方案2】:

如果您将布局嵌套在 ScrollView 中,则小吃栏将出现在键盘顶部。这是因为视图将调整大小以仅占用键盘上方的可用空间。当然,如果需要,您的 View 也可以随时滚动,无论是否显示键盘。

【讨论】:

  • 将其与 android:fillViewport="true" 一起使用以确保 ScrollView 填满整个屏幕应该是公认的答案
【解决方案3】:

这是我的小吃店代码,它的工作方式与您的需要类似+ relativeLayout 是我传递的父主布局 id。

        snackbar=snackbar.make(relativeLayout,"Image is Saved to "+Savedfile.getPath().toString(),Snackbar.LENGTH_INDEFINITE)
                .setAction("OK", new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        snackbar.dismiss();
                        snackbar=null;

                        System.gc();
                        finish();
                    }
                });
        snackbar.show();

【讨论】:

    【解决方案4】:

    您可以在显示Snackbar 时隐藏键盘。

    InputMethodManager imm = (InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    

    【讨论】:

    • 如果您的快餐栏是对文本编辑的输入的响应,这对用户非常不友好,因为它可能最终导致点击其他任何内容并且不愉快地看到(闪烁)。更好的是用scrollviewer嵌套
    【解决方案5】:

    以下作品:

    1. 确保您从中调用Snackbar 的活动/片段包含ScrolView
    2. 确保将快餐栏视图设置为 findViewById(android.R.id.content)

    Snackbar.make(getActivity().findViewById(android.R.id.content), "hello world").show();

    1. 或者,如果您希望能够滑开Snackbar,请确保ScrolView 嵌套在CoordinatorLayout 中。

    Snackbar.make(getActivity().findViewById(android.R.id.my_coordinator_layout), "hello world").show();

    【讨论】:

    • 我喜欢第二种解决方案。简短、清晰且没有任何技巧。
    【解决方案6】:

    我已经解决了这样的问题:

    创建一个类:

    public class ShowSnackBar {
        public static void show(Context context, String message, View view) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
        }
    }
    

    您可以从应用程序中的任何活动访问此类

    用法:

    ShowSnackBar.show(LoginOtpActivity.this,"Please enter Email ID /Mobile",linear_container);
    

    【讨论】:

      【解决方案7】:

      这是我的代码,不需要从方法中设置视图,你可以从 DecoreView 中获取:

      public static void longSnack(final String text) {
          hideKeyboard();
          Snackbar snackbar = Snackbar.make(context.getWindow().getDecorView(), text, Snackbar.LENGTH_LONG);
          snackbar.show();
      }
      
      public static void shortSnack(final String text) {
          hideKeyboard();
          Snackbar snackbar = Snackbar.make(context.getWindow().getDecorView(), text, Snackbar.LENGTH_SHORT);
          snackbar.show();
      }
      
      private static void hideKeyboard() {
          InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE);
          if (inputMethodManager != null) {
              inputMethodManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getWindowToken(), 0);
          }
      }
      

      【讨论】:

      【解决方案8】:

      我自己也遇到了这个问题,并且能够很容易地解决它。

      之前,我设置了android:fitsSystemWindows="true"

      解决方案是将其设置为 false,或者直接删除该行。 现在,您无需隐藏键盘即可获得所需的结果。

      这是我的布局文件供参考:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.coordinatorlayout.widget.CoordinatorLayout 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"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/relative"
          android:orientation="vertical"
          android:fitsSystemWindows="false">
          <LinearLayout
              android:id="@+id/linear"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="@dimen/default_gap"
              android:orientation="horizontal">
              <EditText
                  android:id="@+id/input"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginStart="15dp"
                  android:layout_weight="1"
                  android:hint="@string/input_a_number"
                  android:ems="10"
                  android:imeOptions="actionDone|flagNoPersonalizedLearning"
                  android:inputType="numberSigned|numberDecimal"
                  android:maxLines="1"
                  android:maxLength="30"
                  android:singleLine="true"
                  android:importantForAutofill="no"
                  android:focusable="true"
                  android:focusableInTouchMode="true"
                  android:focusedByDefault="true"
                  tools:ignore="LabelFor"
                  tools:targetApi="o" />
              <Spinner
                  android:id="@+id/spinner"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:minWidth="180dp"
                  android:layout_marginEnd="@dimen/default_gap"
                  android:layout_weight="1" />
          </LinearLayout>
      
          <ListView
              android:id="@+id/listview"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@id/linear"
              android:layout_marginEnd="10dp"
              android:layout_marginStart="5dp"
              android:layout_marginTop="5dp" />
      
          <com.google.android.material.floatingactionbutton.FloatingActionButton
              android:id="@+id/fab"
              android:visibility="gone"
              android:layout_alignParentBottom="true"
              android:layout_alignParentEnd="true"
              android:layout_margin="16dp"
              app:elevation="4dp"
              app:srcCompat="@drawable/jade"
              app:backgroundTint="@color/jade_fab"
              android:layout_gravity="bottom|end"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
      
      </androidx.coordinatorlayout.widget.CoordinatorLayout>
      

      【讨论】:

        【解决方案9】:

        将此属性添加到您要在其中显示小吃店的相应活动的清单文件中:

        android:windowSoftInputMode="adjustResize"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-08
          • 2018-04-08
          • 2020-05-04
          • 1970-01-01
          • 2018-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多