隐藏软键盘以进行EditText焦点/触摸 [英] Hide soft keyboard for editText focus/touch
问题描述
当EditText接收焦点或触摸事件时,我试图隐藏软键盘,使其不显示.我希望EditText是可编辑的-EditText中将有文本,因此我希望EditText响应触摸事件,以便定位光标,但是我将拥有专用于编辑的按钮,而不是使用软键盘.我无法正常工作-似乎在focuschange和touch事件上都弹出了软键盘.
I am trying to hide the soft keyboard from showing when an EditText either receives focus or a touch event. I want the EditText to be editable - there will be text in the EditText, so I want the EditText to respond to a touch event so the cursor is positioned, but I will have dedicated buttons for editing rather than using the soft keyboard. I can't get this to work - the soft keyboard seems to pop up on both focuschange and touch events.
我将代码简化为以下内容:
I have reduced the code to the following:
AndroidMainifest.xml:
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:
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:
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答案的变体来隐藏键盘,但是没有任何效果.任何帮助将不胜感激.
I have tried variations of many stackoverflow answers to hide the keyboard, but nothing works. Any help would be appreciated.
推荐答案
我最终得到了一个棘手的解决方案,它可以工作,但是我不喜欢它.
I ended up with a tricky solution, it works, but i don't like it.
我有一个简单的布局,其中包含EditText
和Button
.当您按下按钮时,我在光标所在的EditText
上添加了"*".您可以用手指操纵光标,并且软键盘永远不会打开,因为当EditText获得焦点时,我会隐藏键盘.
I have a simple layout with a EditText
and a Button
. When you press the button I add a "*" to the EditText
were the cursor is. You can manipulate the cursor with the finger and the soft keyboard never opens because when the EditText gets focus I hide the keyboard.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Here"/>
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click Me!" />
</LinearLayout>
和Activity
中的代码:
public class MainActivity : Activity, View.IOnTouchListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
EditText edt = FindViewById<EditText>(Resource.Id.myEditText);
edt.SetOnTouchListener(this);
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate { edt.Text = edt.Text.Insert(edt.SelectionStart, "*"); };
}
public bool OnTouch(View v, MotionEvent e)
{
// Pass the event to the edit text to have the blinking cursor.
v.OnTouchEvent(e);
// Hide the input.
var imm = ((InputMethodManager)v.Context.GetSystemService(Context.InputMethodService));
imm?.HideSoftInputFromWindow(v.WindowToken, HideSoftInputFlags.None);
return true;
}
}
最终结果
这篇关于隐藏软键盘以进行EditText焦点/触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!