关闭软键盘

时间:2015-07-09 18:49:32

标签: c# keyboard xamarin.forms

我有一个页面,其中的想法是将其与外部键盘一起使用。当页面加载时,我将焦点设置在Entry控件上,我想隐藏软键盘。

这是我想要这样做的课程:

internal class RedactContent : ContentPage
{
    StackLayout stack = new StackLayout();
    Entry entry;

    internal RedactContent()
    {

        entry = new Entry();
        Content = new StackLayout
        {
            Children = {                
                entry,
                //more code
            }
        };
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        entry.Focus();
        // Hide Keyboard
    }
}

我该怎么做?

3 个答案:

答案 0 :(得分:0)

可以使用CloseKeyboard方法创建CustomEntry。为此,您需要为每个平台编写自定义渲染器。见http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/

在Android上,您的CustomEntry类可能如下所示:

.wrapper {
    height: 70px;
    width: 100%;
    background: red;
    overflow: hidden;
}

.wrapper a {
    /*NOT AFFECTING THE VERTICAL ALIGN*/
    display: block;
    float: left;
    height: 70px;
    width: 50%;
    color: #fff;
    text-align: center;
    /*AFFECTING VERTICAL ALIGN*/
    line-height: 70px;
}

答案 1 :(得分: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>

local:必须在xaml xmlns中定义一个名称空间:local =&#34; clr-namespace:MobileClients.Droid.Core; assembly = MobileClients.Droid&#34;派生的Entry类(在本例中为KBLessEntry)所在的位置

就是这样

答案 2 :(得分:0)

View view = ActivityCreateAccount.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) 
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}