【问题标题】:Android keyboard hides input in web viewAndroid 键盘在 Web 视图中隐藏输入
【发布时间】:2020-04-08 22:27:28
【问题描述】:

在 Android 网络视图中,当触摸文本/密码字段时,键盘会出现但会覆盖该字段,迫使用户滚动查看他们正在输入的内容。

有没有办法解决这个问题或在网页视图中自动对焦? (如在 iOS 中所见)。

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dedalos.amb">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen" android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
    </application>

</manifest>

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/splash_background"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/ambWebViewLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/splash_background" />

</LinearLayout>

我也在此处粘贴样式和颜色:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>

    <color name="splash_background">#e11020</color>
</resources>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#e11020</item>
        <item name="colorPrimaryDark">#e11020</item>
        <item name="colorAccent">#e11020</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentNavigation">true</item>

    </style>

    <!-- SplashTheme -->
    <style name="SplashTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/splash_theme</item>
    </style>

</resources>

【问题讨论】:

标签: android webview keyboard field show-hide


【解决方案1】:

尝试添加以下属性:

android:fitsSystemWindows="true"

在 .xml 布局的根 LinearLayout 中。

【讨论】:

  • 导航栏也是彩色的! ? 我不能用android:fitsSystemWindows="true" 只要我不明白如何让状态栏和导航栏再次透明?
  • 您可以使用 CoordinatorLayout 作为您的活动根视图,然后 setFitsSystemWindows(boolean) 将起作用。您可以查看以下帖子以获取详细说明。 link@Stefano。
  • @Stefano 如果您不想更改布局,也可以尝试其他解决方案。他们可以在这个答案中找到here
  • 不幸的是,我的应用使用 CoordinatorLayout 而不是 LinearLayout 崩溃:-/
【解决方案2】:

您可能想查看这篇文章: https://developer.android.com/training/keyboard-input/visibility

也许这会对你有所帮助。

android:windowSoftInputMode="adjustResize"

【讨论】:

  • 您好,非常感谢您的帮助!我试图将它插入到 .MainActivity 中,不幸的是它不起作用:-( 键盘出现并覆盖了文本字段
【解决方案3】:

当应用使用全屏模式时,Android 键盘会隐藏。

如果你使用这个问题可以解决

&lt;item name="android:windowFullscreen"&gt;false&lt;/item&gt; 在你的风格中。

然后使用&lt;activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize"&gt;

在您的清单中。

【讨论】:

  • 在全屏上很好的解决方案这不起作用。但是当我禁用全屏时,无需更改清单即可。谢谢
【解决方案4】:

我通过以下答案解决了这个问题

android:fitsSystemWindows="true" 在根 LinearLayout @animeshk

&lt;item name="android:windowFullscreen"&gt;false&lt;/item&gt; 主题风格 @Nawin

就我而言,它在不添加android:windowSoftInputMode="adjustResize" 的情况下运行良好

【讨论】:

    【解决方案5】:

    您可以尝试以下属性:

    • 在您注册活动的清单文件中:

      <activity android:name="yourActivity"
         android:windowSoftInputMode="stateHidden"/>
      

    【讨论】:

      【解决方案6】:

      有两种选择:

      备选方案 1:

      对于单行焦点然后使用这个:

      webView.requestFocus(View.FOCUS_DOWN);
      

      备选方案 2:

      webView.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View view, MotionEvent event) {
              switch (event.getAction()) {
                  case MotionEvent.ACTION_DOWN:
                  case MotionEvent.ACTION_UP:
                      view.requestFocusFromTouch();  
                      break;
              }               
              return false;
          }
      
      });
      

      希望对你有帮助……!

      【讨论】:

        【解决方案7】:

        试试这个。

        webView.getSettings().setJavaScriptEnabled(true);
        
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                view.loadUrl("javascript:document.getElementsByName('[your view name without square brackets]')[0].focus();");
                super.onPageFinished(view, url);
            }
        });
        
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        

        【讨论】:

          猜你喜欢
          • 2014-03-08
          • 2014-04-07
          • 2014-11-06
          • 1970-01-01
          • 1970-01-01
          • 2017-02-14
          • 1970-01-01
          • 2012-10-30
          • 1970-01-01
          相关资源
          最近更新 更多