我有一個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);
}
}
}
的代碼如上所示演示我的親blem。將應用程序部署到手機,運行應用程序,切換到另一個應用程序,然後再切換回來,並在恢復第一個應用程序時彈出軟鍵盤。但是......註釋掉下面的代碼行MainActivity.cs:
adView.LoadAd(requestbuilder.Build());
和重複部署等,而軟鍵盤不會彈出。
正如我所說,我很難過。我試着將focuschange的處理程序添加到EditText中以彈出鍵盤,但據我所知,只有在EditText失去焦點時纔會調用它。
這是有效的!謝謝。我只有一個要求...我不想讓鍵盤彈出來,如果我觸摸editText,但我想要處理觸摸事件。 editText中會有文本,我希望能夠對其進行修改,所以我希望光標對觸摸做出響應,但我想使用專用按鈕進行編輯,而不是軟鍵盤。有任何想法嗎? – JeffR
感謝另一個完全不同的問題。我建議你爲它開個新的問題。我很樂意提供幫助。請標記我的答案是正確的。謝謝。 – jzeferino
謝謝。標記爲答案。新問題在這裏http://stackoverflow.com/questions/38956548/hide-soft-keyboard-for-edittext-focus-touch – JeffR