I have a piece of code that hides software keyboard when focus changed on entry control when I click in Entry, but when I click in Entry keyboard shows for a split second and dissapers. What I want to achieve is to avoid that behaviour. So I would like to click in Entry and keyboard should stay hidden. My current code is below.
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.Click = (sender, evt) =>
{
new Handler().Post(delegate
{
var imm = (InputMethodManager)Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
//WPISYWANIE NA EMULATORZE
if (!GlobalController.ShowSoftwareKeyboard)
{
var resultDebug = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
}
//#if !DEBUG
// var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
//#endif
});
};
Control.FocusChange = (sender, evt) =>
{
new Handler().Post(delegate
{
var imm = (InputMethodManager)Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
//WPISYWANIE NA EMULATORZE
if (!GlobalController.ShowSoftwareKeyboard)
{
var resultDebug = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
}
//#if !DEBUG
// var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
//#endif
});
};
//Control.TextChanged = (sender, evt) =>
//{
// new Handler().Post(delegate
// {
// var imm = (InputMethodManager)Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
// //WPISYWANIE NA EMULATORZE
// if (!GlobalController.ShowSoftwareKeyboard)
// {
// var resultDebug = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
// }
// //#if !DEBUG
// // var result = imm.HideSoftInputFromWindow(Control.WindowToken, 0);
// //#endif
// });
//};
}
}
CodePudding user response:
You can create a Custom Renderer to hide the keyboard when Entry gets the focused.
Step 1: Create an class: MyHideKeyboard to extend the Entry in shared project.
public class MyHideKeyboard : Entry
{
}
Step 2: Override the Custom Renderer in the Android platform,below is the code snippets for your reference:
[assembly: ExportRenderer(typeof(MyHideKeyboard), typeof(MyEntryRenderer))]
namespace AppLogin.Droid
{
public class MyEntryRenderer : EntryRenderer
{
public MyEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
EditText editText = Control as EditText;
editText.InputType = Android.Text.InputTypes.Null;
}
}
}
}
Step 3: Use it in the xaml like below:
<AppLogin:MyHideKeyboard x:Name="myEntry" Placeholder="..."> </AppLogin:MyHideKeyboard>
CodePudding user response:
Alexander May thanks for your response! I've done some changes according to your answer and there is a little progress, because now when I click in Entry keyboard doesn't show, but it appears when I start typing some text and it never hide, it's still on the screen. Is there way to make keyboard hidden while typing text in Entry control?
My current XAML code looks like below:
<custom:NoKeyboardEntry x:Name="customKeyboard" Text="{Binding Phone}" Keyboard="Telephone" Placeholder="Numer telefonu" >
<custom:NoKeyboardEntry.Behaviors>
<local:PhoneNumberMaskBehavior x:Name="PhoneMask" />
<local:EntryLengthValidator MaxLength="11"/>
</custom:NoKeyboardEntry.Behaviors>
</custom:NoKeyboardEntry>
And here is my current backend code:
[assembly: ExportRenderer(typeof(NoKeyboardEntry), typeof(CustomNoKeyboardEntry))]
namespace MonterShop.Droid
{
class CustomNoKeyboardEntry : EntryRenderer
{
public CustomNoKeyboardEntry(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
EditText editText = Control as EditText;
editText.InputType = Android.Text.InputTypes.Null;
}
}
}
}