[英]Hide soft keyboard on app resume
我有一個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);
但這也沒有幫助。
我已經嘗試了許多其他stackoverflow問題中可以找到的建議。 我可以防止鍵盤在任何情況下彈出,除非在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);
}
}
}
上面顯示的代碼演示了我的問題。 將應用程序部署到手機,運行應用程序,切換到另一個應用程序,然后再切換回來,並在恢復第一個應用程序時彈出軟鍵盤。 但是......在MainActivity.cs中注釋掉以下代碼行:
adView.LoadAd(requestbuilder.Build());
並重復部署等,軟鍵盤不會彈出。
正如我所說,我很難過。 我嘗試將一個處理程序for 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">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.