隐藏用于editText焦点/触摸的软键盘

时间:2016-08-15 14:01:16

标签: c# android xamarin android-edittext android-softkeyboard

我试图在EditText接收焦点或触摸事件时隐藏软键盘。我希望EditText可以编辑 - 在EditText中会有文本,所以我希望EditText响应触摸事件以便定位光标,但是我将使用专用按钮进行编辑而不是使用软键盘。我不能让它工作 - 软键盘似乎在焦点变换和触摸事件上弹出。

我已将代码缩减为以下内容:

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"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true">

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

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

            LinearLayout layout = FindViewById<LinearLayout>(Resource.Id.layout);
            layout.FocusChange += FocusChange;

            EditText editText = FindViewById<EditText>(Resource.Id.editText);
            editText.FocusChange += FocusChange;
            editText.Touch += Touch;

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


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

            AdView adView = FindViewById<AdView>(Resource.Id.adView);
            if (adView != null)
                adView.Resume();
        }


        private void FocusChange(object sender, View.FocusChangeEventArgs e)
        {
            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);
        }



        private void Touch(object sender, View.TouchEventArgs e)
        {
            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);

            (sender as EditText).OnTouchEvent(e.Event);
        }
    }
}

我尝试了许多stackoverflow答案的变体来隐藏键盘,但没有任何效果。任何帮助将不胜感激。

0 个答案:

没有答案