当edittext获得焦点时,将光标位置设置为0

时间:2014-03-27 12:27:38

标签: android android-edittext

当用户选择EditText时,我希望光标位置从0开始。此EditText的目的是让您更容易在HH中输入格式。

这是我尝试过的焦点更改监听器:

txtStartHH.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                txtStartHH.setSelection(0);
        }
    });

以下是我的文本观察程序代码,可能相关也可能不相关:

txtStartHH.addTextChangedListener(new TextWatcher()
    {
        boolean formatting;
        int mStart;
        int mEnd;

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            this.mStart = -1;
            mStart = start + count;
            mEnd = Math.min(mStart + count, s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!formatting)
            {
                formatting = true;

                if (s.toString().length() == 3)
                {
                    if (mStart >= 0)
                    {
                        if (s.toString().substring(mStart - 1, mStart).matches("[0-2]+") && mStart == 1)
                        {
                            if (Character.getNumericValue(s.charAt(0)) > 1 
                                    && Character.getNumericValue(s.charAt(mEnd)) > 3)
                            {
                                s.replace(mEnd, mEnd + 1, "3");
                                s.replace(1, 2, "");
                            }
                            else
                                s.replace(mStart, mEnd, "");
                        }
                        else if (s.toString().substring(mStart, mEnd).matches("[0-9]+") && mStart == 2)
                        {
                            if (Character.getNumericValue(s.charAt(1)) > 3
                                    && Character.getNumericValue(s.charAt(0)) > 1)
                            {
                                s.replace(0, 1, "1");
                                s.replace(2, 3, "");
                            }
                            else                                
                                s.replace(mStart, mEnd, "");
                        }
                        else
                        {
                            // remove the character that has just been entered (i.e, cancels the insertion)
                            s.replace(mStart - 1, mStart, "");
                        }
                    }

                    if (mStart > 2)
                    {
                        s.replace(mStart - 1, mStart, "");
                    }
                }
                else
                {                   
                    if (mStart == mEnd && s.toString().length() < 2)
                    {
                        if (mStart == 1)
                        {
                            // user has just backspaced the second char
                            s.insert(mStart, "0");
                            mStart--;
                            txtStartHH.setSelection(mStart);
                        }
                        if (mStart == 0)
                        {
                            // user has just backspaced the first char
                            s.insert(mStart, "0");
                            mStart--;
                            txtStartHH.setSelection(mStart);
                        }
                    }
                }

                formatting = false;

                if (mStart > 1 && !jumpingTimes)
                {
                    txtStartMM.requestFocus();
                    txtStartMM.setSelection(0);
                }

            }
        }
    });

然而,当EditText获得焦点时,光标出现在按下EditText的位置,而不是在开始时。这里有什么问题 - 我可以使用一种不同的方法吗?

1 个答案:

答案 0 :(得分:0)

当用户将手指拉到屏幕上时,不是改变OnFocusChangeListener被触发的时间(也就是android将光标移动到按下的位置之前),而是设置光标。

您可以使用setOnTouchListener()方法并检查操作ACTION_UP来实现此目的。 例如:

txtStartHH.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){
            if (hasFocus) {
                txtStartHH.setSelection(0, 0);
            }
            return true;
        }
        return false;
    }
});