Xamarin.Android:关键侦听器不起作用

时间:2018-09-24 06:57:01

标签: android xamarin xamarin.android onkeylistener

在OnCreate片段方法中,我从布局中获取EditText并调用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:

enter image description here

问题是当我在EditText中编写时,没有调用LocationKeyListener.OnKey方法。

更新

带有编辑文本的片段:

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

也许这是由于所有这些都在片段中

1 个答案:

答案 0 :(得分:1)

尽管这是一个老话题,但是我遇到了同样的问题,只是要知道View.IOnKeyListener不支持SoftKeyboard(虚拟键盘)键输入。通过OnKey事件仅执行硬件键盘输入。

根据您的用例,如果您想获得通知,例如与使用EditorAction事件相比,按下任何软键盘按钮(例如Keycode.NavigateNext按钮)。在Xamarin.Forms中定义一个自定义条目渲染器,并在OnElementChanged事件中通过以下方式监听该事件:

this.EditText.EditorAction += EditTextOnEditorAction;

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

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

Android Keyboard-Input docu