C# 隐藏编辑文本焦点/触摸的软键盘

C# 隐藏编辑文本焦点/触摸的软键盘,c#,android,xamarin,android-edittext,android-softkeyboard,C#,Android,Xamarin,Android Edittext,Android Softkeyboard,当EditText接收到焦点或触摸事件时,我试图隐藏软键盘以防显示。我希望EditText是可编辑的-EditText中会有文本,因此我希望EditText响应触摸事件,以便定位光标,但我将使用专用按钮进行编辑,而不是使用软键盘。我不能让它工作-软键盘似乎会在focuschange和touch事件中弹出 我已将代码缩减为以下内容: AndroidMainifest.xml: <?xml version="1.0" encoding="utf-8"?> <manifest xml

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

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

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);
        }
    }
}
名称空间App1
{
[活动(Label=“App1”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
LinearLayout布局=FindViewById(Resource.Id.layout);
layout.FocusChange+=FocusChange;
EditText EditText=findviewbyd(Resource.Id.EditText);
editText.FocusChange+=FocusChange;
editText.Touch+=触摸;
AdView AdView=findviewbyd(Resource.Id.AdView);
var requestbuilder=new AdRequest.Builder();
LoadAd(requestbuilder.Build());
}
受保护的覆盖void OnResume()
{
base.OnResume();
AdView AdView=findviewbyd(Resource.Id.AdView);
if(adView!=null)
adView.Resume();
}
私有void FocusChange(对象发送方,View.FocusChangeEventArgs e)
{
InputMethodManager im=(InputMethodManager)GetSystemService(Context.InputMethodService);
im.HideSoftInputFromWindow(Window.DecorView.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
LinearLayout布局=FindViewById(Resource.Id.layout);
im.HideSoftInputFromWindow(layout.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
EditText EditText=findviewbyd(Resource.Id.EditText);
im.HideSoftInputFromWindow(editText.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
}
私有void Touch(对象发送者,View.TouchEventArgs e)
{
InputMethodManager im=(InputMethodManager)GetSystemService(Context.InputMethodService);
im.HideSoftInputFromWindow(Window.DecorView.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
LinearLayout布局=FindViewById(Resource.Id.layout);
im.HideSoftInputFromWindow(layout.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
EditText EditText=findviewbyd(Resource.Id.EditText);
im.HideSoftInputFromWindow(editText.WindowToken,Android.Views.InputMethods.HideSoftInputFlags.None);
(发送者为EditText)。OnTouchEvent(如事件);
}
}
}

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

我最终得到了一个棘手的解决方案,它是可行的,但我不喜欢它

我有一个带有
EditText
按钮的简单布局。当您按下按钮时,我会在
EditText
中添加一个“*”作为光标所在位置。您可以用手指操纵光标,软键盘永远不会打开,因为当EditText获得焦点时,我会隐藏键盘


以及
活动中的代码

public类main活动:活动,View.IOnTouchListener
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
EditText edt=FindViewById(Resource.Id.myEditText);
edt.SetOnTouchListener(本);
Button Button=FindViewById(Resource.Id.myButton);
按钮。单击+=委托{edt.Text=edt.Text.Insert(edt.SelectionStart,“*”);};
}
公共bool OnTouch(视图v,运动事件e)
{
//将事件传递给编辑文本以使光标闪烁。
v、 火山口(e);
//隐藏输入。
var imm=((InputMethodManager)v.Context.GetSystemService(Context.InputMethodService));
imm?.HideSoftInputFromWindow(v.WindowToken,HideSoftInputFlags.None);
返回true;
}
}
最终结果


我刚刚在emulator中测试了我的第一个解决方案,它成功了。但在设备中它失败了。我用一个有效的解决方案更新了我的答案。在这里试试吧:再次感谢——我真的很感谢你的帮助。这是可行的,但是如果我添加了一个AdView,那么我就会遇到与相同的问题。如果我在你的测试代码中添加一个AdView,并实现你在上面链接中给出的解决方案,当我切换应用程序并恢复时,软键盘弹出。我开始怀疑我想要的行为是否相互排斥…dropbox上的zip文件在此将该应用部署到手机,运行该应用,切换到另一个应用,然后切换回,软键盘弹出。注释掉onCreate中的行“adView.LoadAd(requestbuilder.Build());”,软键盘不会弹出-但我想要横幅广告,我不明白。你回答我的问题做得很好-我就是不能让答案一起工作。再次感谢。因为你的问题不在问题的范围内,我的答案将写在这里。我在布局根目录中添加了
android:genderantfocusability=“beforesandroid:focusableInTouchMode=“true”
,并在活动开始暂停时从编辑文本中删除了焦点:
protected override void onpause(){base.onpause();edt.ClearFocus();}
请查看我的github,我在那里添加了一个新项目。哦,顺便说一句,你的谷歌广告导入在