Xamarin.Android: Key Listener not working

只谈情不闲聊 提交于 2020-01-05 03:37:04

问题


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());

Layout declaration:

<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 styles:

<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:

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

UPDATE

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;
        }
    }

Here is My Main Container 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();
            }
        }
    }

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

来源:https://stackoverflow.com/questions/52474063/xamarin-android-key-listener-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!