隐藏应用程序简历上的软键盘

问题描述:

我有一个Android应用程序,使用Xamarin编写的C#文件。我已将应用程序简化为包含TextView和Google admod AdView的横幅广告的LinearLayout。隐藏应用程序简历上的软键盘

我不希望软键盘出现在应用程序中 - 这不是必需的。如果我启动应用程序,则会出现TextView和横幅添加。如果我然后切换到另一个应用程序,然后切换回第一个应用程序,一旦第一个应用程序恢复,弹出软键盘。只有AdView存在时才会发生这种情况。如果我从应用中删除AdView,软键盘不会弹出。

当应用程序首次启动并且存在AdView时,键盘不会启动 - 只有当应用程序暂停并恢复时。

我有以下的onResume覆盖:

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

但键盘仍然在恢复弹出,如果我有AdView的。

我试图躲在了的onResume键盘AdView中明确:

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

但这并没有帮助。

我已经尝试过在很多很多其他*问题中找到的每个建议。除了在AdView存在时恢复应用程序的情况之外,我可以防止在任何情况下弹出键盘。

我很难过。有任何想法吗?

更多详细信息....

首先是纠正。我在LinearLayout上有一个EditText,而不是TextView。我不想要软键盘,但我确实希望EditText是可编辑的(在应用程序的完整版本中,我提供用于在EditText中添加/删除/更改文本的按钮)。

我已将应用程序缩减为包含EditText和AdView的单个LinearLayour。如果我将广告加载到AdView并运行应用程序,切换到另一个应用程序,然后切换回来,弹出软键盘。我不想那样。但是,如果我没有将广告加载到AdView(但在axml文件中仍然有AdView),然后运行并切换等,则不会弹出软键盘。

源如下:

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:

<?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:

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); 

     } 
    } 
} 

的代码如上所示演示我的亲blem。将应用程序部署到手机,运行应用程序,切换到另一个应用程序,然后再切换回来,并在恢复第一个应用程序时弹出软键盘。但是......注释掉下面的代码行MainActivity.cs:

adView.LoadAd(requestbuilder.Build()); 

和重复部署等,而软键盘不会弹出。

正如我所说,我很难过。我试着将focuschange的处理程序添加到EditText中以弹出键盘,但据我所知,只有在EditText失去焦点时才会调用它。

问题是EditText,它创建活动时会自动调焦。要移除该默认行为,请将这两行添加到根元素。

<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"> 
+0

这是有效的!谢谢。我只有一个要求...我不想让键盘弹出来,如果我触摸editText,但我想要处理触摸事件。 editText中会有文本,我希望能够对其进行修改,所以我希望光标对触摸做出响应,但我想使用专用按钮进行编辑,而不是软键盘。有任何想法吗? – JeffR

+0

感谢另一个完全不同的问题。我建议你为它开个新的问题。我很乐意提供帮助。请标记我的答案是正确的。谢谢。 – jzeferino

+0

谢谢。标记为答案。新问题在这里http://*.com/questions/38956548/hide-soft-keyboard-for-edittext-focus-touch – JeffR