简体   繁体   English

在应用简历上隐藏软键盘

[英]Hide soft keyboard on app resume

I have an Android app, written in c# using Xamarin. 我有一个Android应用程序,使用Xamarin用c#编写。 I have reduced the app to a LinearLayout containing a TextView and a Google admod AdView for a banner ad. 我已将应用程序缩减为包含TextView和横向广告的Google admod AdView的LinearLayout。

I don't want the soft keyboard to appear in the app - it's not required. 我不希望软键盘出现在应用程序中 - 这不是必需的。 If I start the app, the TextView and the banner add appear. 如果我启动应用程序,则会出现TextView和横幅添加。 If I then switch to another app, then switch back to the first app, the soft keyboard pops up as soon as the first app is resumed. 如果我然后切换到另一个应用程序,然后切换回第一个应用程序,一旦第一个应用程序恢复,软键盘就会弹出。 This only happens if the AdView is present. 只有AdView存在时才会出现这种情况。 If I remove the AdView from the app, the soft keyboard does not pop up. 如果我从应用中删除AdView,则不会弹出软键盘。

The keyboard does not pup up when the app is first started and the AdView is present - only if the app is paused and resumed. 当应用程序首次启动且AdView存在时,键盘不会发出声音 - 仅在应用程序暂停并恢复时才会出现。

I have the following OnResume override: 我有以下OnResume覆盖:

protected override void OnResume()
{
    base.OnResume();
    InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService);
    im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None); 
}                                  

but the keyboard still pops up on resume if I include the AdView. 但如果我加入AdView,键盘仍会弹出简历。

I tried hiding the keyboard in OnResume for the AdView specifically: 我试图在OnResume中专门为AdView隐藏键盘:

im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);

but that didn't help either. 但这也没有帮助。

I have tried every suggestion I could find in many, many other stackoverflow questions. 我已经尝试了许多其他stackoverflow问题中可以找到的建议。 I am able to prevent the keyboard popping up in every circumstance except when the app is resumed when the AdView is present. 我可以防止键盘在任何情况下弹出,除非在AdView存在时恢复应用程序。

I'm stumped. 我很难过。 Any ideas? 有任何想法吗?

More details.... 更多细节....

First, a correction. 首先是纠正。 I have an EditText on the LinearLayout, not a TextView. 我在LinearLayout上有一个EditText,而不是TextView。 I don't want the soft keyboard, but I do want the EditText to be editable (in the full version of the app I provide buttons to add/delete/change text in the EditText). 我不想要软键盘,但我确实希望EditText可以编辑(在应用程序的完整版本中我提供了在EditText中添加/删除/更改文本的按钮)。

I have reduced the app to a single LinearLayour containing an EditText and an AdView. 我已将应用程序缩减为包含EditText和AdView的单个LinearLayour。 If I load an ad to the AdView and run the app, switch to another app, then switch back, the soft keyboard pops up. 如果我将广告加载到AdView并运行应用,切换到另一个应用,然后切换回来,弹出软键盘。 I don't want that. 我不希望这样。 If, however, I don't load an ad to the AdView (but still have the AdView in the axml file), then run and switch etc., no soft keyboard pops up. 但是,如果我没有将广告加载到AdView(但仍然在axml文件中包含AdView),然后运行并切换等,则不会弹出软键盘。

Source is as follows: 来源如下:

AndroidMainifest.xml AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="App1.App1" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application android:label="App1">
        <meta-data android:name="com.google.android.gms.version" 
               android:value="@integer/google_play_services_version" />
        <activity android:name="com.google.android.gms.ads.AdActivity" 
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
              android:theme="@android:style/Theme.Translucent" 
              android:windowSoftInputMode="stateAlwaysHidden" />
    </application>
</manifest>

Main.axml: Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

  <EditText xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/editText" />

  <com.google.android.gms.ads.AdView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/adView"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  </com.google.android.gms.ads.AdView>

</LinearLayout>

MainActivity.cs: MainActivity.cs:

using Android.App;
using Android.Content;
using Android.Views;
using Android.Views.InputMethods;
using Android.OS;
using Android.Widget;
using Android.Gms.Ads;

namespace App1
{
    [Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            AdView adView = FindViewById<AdView>(Resource.Id.adView);
            var requestbuilder = new AdRequest.Builder();
            adView.LoadAd(requestbuilder.Build());
        }


        protected override void OnResume()
        {
            base.OnResume();

            InputMethodManager im = (InputMethodManager)GetSystemService(Context.InputMethodService);

            im.HideSoftInputFromWindow(Window.DecorView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);

            LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.layout);
            im.HideSoftInputFromWindow(layout.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);

            EditText editText = FindViewById<EditText>(Resource.Id.editText);
            im.HideSoftInputFromWindow(editText.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);

            AdView adView = FindViewById<AdView>(Resource.Id.adView);
            im.HideSoftInputFromWindow(adView.WindowToken, Android.Views.InputMethods.HideSoftInputFlags.None);

        }
    }
}

The code as shown above demonstrates my problem. 上面显示的代码演示了我的问题。 Deploy the app to a phone, run the app, switch to another app, then switch back again, and the soft keyboard pops up on resuming the first app. 将应用程序部署到手机,运行应用程序,切换到另一个应用程序,然后再切换回来,并在恢复第一个应用程序时弹出软键盘。 But... comment out the following line of code in MainActivity.cs: 但是......在MainActivity.cs中注释掉以下代码行:

adView.LoadAd(requestbuilder.Build());

and repeat the deploy etc., and the soft keyboard doesn't pop up. 并重复部署等,软键盘不会弹出。

As I said, I'm stumped. 正如我所说,我很难过。 I tried adding a handler for focuschange to the EditText to pop the keyboard down, but as far as I can tell that's only called when the EditText loses focus. 我尝试将一个处理程序for focuschange添加到EditText以弹出键盘,但据我所知,只有当EditText失去焦点时才会调用它。

The problem is the EditText , it's getting auto focus when activity is created. 问题是EditText ,它在创建活动时获得自动焦点。 To remove that default behavior add this two lines to your root element. 要删除该默认行为,请将这两行添加到根元素中。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM