Xamarin.Android:密钥侦听器不起作用 [英] Xamarin.Android: Key Listener not working

查看:32
本文介绍了Xamarin.Android:密钥侦听器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OnCreate 片段方法中,我从布局中获取我的 EditText 并调用 SetOnKeyListener:

In the OnCreate fragment method, I get my EditText from the layout and call SetOnKeyListener:

Aaa = view.FindViewById<EditText>(Resource.Id.aaaText);
Aaa.SetOnKeyListener(new LocationKeyListener());

布局声明:

<EditText 
            android:id="@+id/aaaText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/LocationEditTextStyle"
            android:hint="AAA"
            app:MvxBind="Text Aaa.Value"/>

EditText 样式:

<style name="LoginEditTextTheme" parent="Theme.AppCompat">
        <item name="colorControlNormal">@color/accentColor</item>
        <item name="colorControlActivated">@color/secondaryTextColor</item>
    </style>


    <style name="LocationEditTextStyle" parent="Widget.AppCompat.EditText">
        <item name="android:textAllCaps">true</item>
        <item name="android:inputType">textCapCharacters</item>
        <item name="android:maxLength">3</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:textSize">@dimen/location_text_size</item>
        <item name="android:theme">@style/LocationEditTextTheme</item>
    </style>

LocationKeyListener:

问题是我在 EditText 中写入时没有调用 LocationKeyListener.OnKey 方法.

The problem is that the LocationKeyListener.OnKey method is not called, when I write in EditText.

更新

带有编辑文本的我的片段:

My Fragment with Edit texts:

public abstract class BaseActionFragment<T> : BaseFragment<T> where T : BaseViewModel
    {
        protected EditText Aaa, Bbb, Ccc, Ddd, Eee;
        protected EditText test;


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = base.OnCreateView(inflater, container, savedInstanceState);

            Aaa = view.FindViewById<EditText>(Resource.Id.aaaText);
            Bbb = view.FindViewById<EditText>(Resource.Id.bbbText);
            Ccc = view.FindViewById<EditText>(Resource.Id.cccText);
            Ddd = view.FindViewById<EditText>(Resource.Id.dddText);
            Eee = view.FindViewById<EditText>(Resource.Id.eeeText);

            Aaa.AddLocationTextWatcher(Aaa, Bbb);
            Bbb.AddLocationTextWatcher(Aaa, Ccc);
            Ccc.AddLocationTextWatcher(Bbb, Ddd);
            Ddd.AddLocationTextWatcher(Ccc, Eee);
            Eee.AddLocationTextWatcher(Ddd, Aaa);

            test = view.FindViewById<EditText>(Resource.Id.testText);

            test.SetOnKeyListener(new LocationKeyListener());
            test.KeyPress += (sender, args) =>
            {

            };

            return view;
        }
    }

这是我的主容器视图:

 [Activity(
        Theme = "@style/AppTheme")]
    public class MainContainerActivity : BaseActivity<MainContainerViewModel>, IDrawerActivity
    {
        protected override int ActivityLayoutId => Resource.Layout.activity_main_container;

        public DrawerLayout DrawerLayout { get; private set; }
        protected MvxActionBarDrawerToggle DrawerToggle { get; private set; }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            DrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);

            SetupDrawerLayout();
            DrawerToggle.SyncState();

            if (bundle == null)
                ViewModel.ShowMenuCommand.Execute();
        }

        private void SetupDrawerLayout()
        {
            SupportActionBar?.SetDisplayHomeAsUpEnabled(true);

            DrawerToggle = new MvxActionBarDrawerToggle(
                this,                           // host Activity
                DrawerLayout,                   // DrawerLayout object
                Toolbar,                        // nav drawer icon to replace 'Up' caret
                Resource.String.drawer_open,    // "open drawer" description
                Resource.String.drawer_close    // "close drawer" description
            );

            DrawerToggle.DrawerOpened += (sender, e) => HideSoftKeyboard();
            DrawerLayout.AddDrawerListener(DrawerToggle);
        }

        public override void OnBackPressed()
        {
            if (DrawerLayout != null && DrawerLayout.IsDrawerOpen(GravityCompat.Start))
                DrawerLayout.CloseDrawers();
            else
                base.OnBackPressed();
        }

        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            DrawerToggle.OnConfigurationChanged(newConfig);
        }


        public void HideSoftKeyboard()
        {
            if (CurrentFocus != null)
            {
                var inputMethodManager = (InputMethodManager)GetSystemService(InputMethodService);
                inputMethodManager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);

                CurrentFocus.ClearFocus();
            }
        }
    }

也许这是因为所有这些都在 Fragment 中

Perhaps this is due to the fact that all this is in the Fragment

推荐答案

虽然这是一个老话题,但我遇到了同样的问题,请注意 View.IOnKeyListener 不支持 SoftKeyboard(虚拟键盘)键输入.仅通过 OnKey 事件执行硬件键盘输入.

Although this is an old topic, but I ran into the same issue, just be aware that View.IOnKeyListener does not support SoftKeyboard (virtual keyboard) key inputs. Only hardware keyboard input is performed via OnKey event.

根据您的用例,如果您想收到通知,例如按下任何软键盘按钮(如 Keycode.NavigateNext 按钮)而不是使用 EditorAction 事件.在 Xamarin.Forms 中定义自定义条目渲染器并在 OnElementChanged 事件中通过以下方式监听事件:

Depending on your usecase, if you want to get notified if E.g. any softkeyboard button is pressed (like Keycode.NavigateNext button) than use EditorAction event. In Xamarin.Forms E.g. define a custom entry renderer and in the OnElementChanged event listen on the event via:

this.EditText.EditorAction += EditTextOnEditorAction;

处理您感兴趣的任何关键操作:

Than handle any key action which your are interested in:

private void EditTextOnEditorAction(object sender, TextView.EditorActionEventArgs e)
{
    if (e.ActionId == ImeAction.Next)
    {
        var formsNextSearch = FocusSearchNextElement();
        if (formsNextSearch != null)
        {
            EditText.ClearFocus();                   
        }
    }
}

Android 键盘输入文档

这篇关于Xamarin.Android:密钥侦听器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆