Android切换焦点使键盘消失

时间:2015-04-27 10:41:49

标签: android android-view

我想在按下EditText按钮时在两个KEYCODE_DEL视图之间切换。

我的代码有效,它确实关注了之前的EditText,但是当它执行此操作时,键盘消失,光标位于EditText中的实际文本之前。

为了使其更加生动,当焦点切换时,EditText看起来像这样:

|23其中|是光标位置,键盘是隐藏的。

这是我的代码:

if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){
        super.dispatchKeyEvent(event);
        focusPreviousET();
        return true;
    }
else{
    super.dispatchKeyEvent(event);
    check();
    return true;
 }

private void focusPreviousET() {

    String sId = getId(getCurrentFocus());

    Identification id = new Identification(sId);

    etX = getETFromResource(id);

    if(isEmpty(etX)){
        etX.setEnabled(false);
        id.previousId();
        etX = getETFromResource(id);
        etX.requestFocus();
    }
}
public void check(){
       etX  = (EditText) getCurrentFocus();
    if(etX.getText().toString().length()==2){
        focusNextFreeET();
    }
}

check()方法也不起作用,我也不知道为什么......方法focusNextFreeET()适用于其他情况,但不适用于此。

有什么建议吗?

编辑

忘记提及,当EditText填充22,31,10之类的数字时,光标实际上位于末尾,当光标位于前面时,光标位于开始时,像01,02等......在这两种情况下,键盘都是隐藏的。

1 个答案:

答案 0 :(得分:0)

关注下一项以打开键盘后使用这些行

public class MvxCardView : CardView, IMvxBindingContextOwner
    {
        private object _cachedDataContext;
        private bool _isAttachedToWindow;
        private readonly int _templateId;
        private readonly IMvxAndroidBindingContext _bindingContext;

        public MvxCardView(Context context, IAttributeSet attrs)
            : this(MvxAttributeHelpers.ReadTemplateId(context, attrs), context, attrs)
        {
        }

        public MvxCardView(int templateId, Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
            _templateId = templateId;

            if (!(context is IMvxLayoutInflater))
            {
                throw Mvx.Exception("The owning Context for a MvxCardView must implement LayoutInflater");
            }

            _bindingContext = new MvxAndroidBindingContext(context, (IMvxLayoutInflater)context);
            this.DelayBind(() =>
            {
                if (Content == null && _templateId != 0)
                {
                    Mvx.Trace("DataContext is {0}", DataContext == null ? "Null" : DataContext.ToString());
                    Content = _bindingContext.BindingInflate(_templateId, this);
                }
            });
        }


        protected MvxCardView(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }

        protected IMvxAndroidBindingContext AndroidBindingContext
        {
            get { return _bindingContext; }
        }

        public IMvxBindingContext BindingContext
        {
            get { return _bindingContext; }
            set { throw new NotImplementedException("BindingContext is readonly in the list item"); }
        }

        protected View Content { get; set; }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.ClearAllBindings();
                _cachedDataContext = null;
            }

            base.Dispose(disposing);
        }

        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();
            _isAttachedToWindow = true;
            if (_cachedDataContext != null
                && DataContext == null)
            {
                DataContext = _cachedDataContext;
            }
        }

        protected override void OnDetachedFromWindow()
        {
            _cachedDataContext = DataContext;
            DataContext = null;
            base.OnDetachedFromWindow();
            _isAttachedToWindow = false;
        }

        [MvxSetToNullAfterBinding]
        public object DataContext
        {
            get { return _bindingContext.DataContext; }
            set
            {
                if (_isAttachedToWindow)
                {
                    _bindingContext.DataContext = value;
                }
                else
                {
                    _cachedDataContext = value;
                    if (_bindingContext.DataContext != null)
                    {
                        _bindingContext.DataContext = null;
                    }
                }
            }
        }
    }