[英]Hide Soft Keyboard from anywhere
隱藏軟鍵盤很痛苦。 我使用一些基於具有焦點的EditText的方法,但是在我當前的應用中,鍵盤會在加載新片段的某個位置不斷彈出。
我的幫助器類中有一個方法,但對我不起作用:
//Hide keyboard
public static void hideSoftKeyboard(Activity activity) {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
我想要的是一種輔助方法,我可以從任何地方調用以隱藏軟鍵盤。 這是可能的,還是我總是需要找到重點突出的EditText?
做這樣的事情通過該活動的任何edittext id ..它將適用於該活動
public static void hideSoftKeyboard(Activity activity, EditText editText) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
嘗試使用該代碼(在ru網絡段Habra Habr中找到了它)
public void showSoftInputOnFocusCompat(boolean isShow) {
showSoftInputOnFocus = isShow;
if (Build.VERSION.SDK_INT >= 21) {
setShowSoftInputOnFocus(showSoftInputOnFocus);
} else {
try {
final Method method = EditText.class.getMethod("setShowSoftInputOnFocus", boolean.class);
method.setAccessible(true);
method.invoke(this, showSoftInputOnFocus);
} catch (Exception e) {
// ignore
}
}
}
在您的AndroidManifest.xml中:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
當用戶輸入新的Activity時,此設置將隱藏軟鍵盤(即使EditText控件獲得了焦點)。 僅當用戶單擊編輯框控件時,才會顯示軟鍵盤。
試試這個
public static void hideAllKeyboard(Activity activity)
{
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
用這個
public void hideSoftKeyboard(Activity context)
{
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
}
對我來說,這種方法:
* First create a derivated class from Entry
public class KBLessEntry : Entry
{
public KBLessEntry() : base()
{
}
}
* Then create a custom platform EntryRender
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms;
using MobileClients.Droid.Core;
using Android.Views.InputMethods;
using System;
using System.ComponentModel;
[assembly: ExportRenderer(typeof(KBLessEntry), typeof(KBLessEntryRender))]
namespace MobileClients.Droid.Core
{
public class KBLessEntryRender : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
Control.InputType = 0;
try
{
// Hide keyboard
InputMethodManager inputMethodManager = this.Control.Context.GetSystemService(Android.Content.Context.InputMethodService) as InputMethodManager;
if (inputMethodManager != null)
{
inputMethodManager.HideSoftInputFromWindow(this.Control.WindowToken, HideSoftInputFlags.None);
}
}
catch(Exception Ex)
{
}
}
}
}
在XAML中
<local:KBLessEntry x:Name="TxtCode" FontSize="18" Placeholder="Código producto" TextColor="Black" HorizontalOptions="FillAndExpand"></local:KBLessEntry>
本地:必須定義為在您的xaml xmlns中有一個命名空間:local =“ clr-namespace:MobileClients.Droid.Core; assembly = MobileClients.Droid”
就是這樣
嘗試調用此方法: setShowSoftInputOnFocus(false);
就我而言,它運作良好:
public class CustomKeyboardField extends android.support.v7.widget.AppCompatEditText {
public CustomKeyboardField(Context context) {
super(context);
init();
}
public CustomKeyboardField(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomKeyboardField(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setShowSoftInputOnFocus(false);
setCursorVisible(false);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.