Android 中获取EditText控件的焦点以及监听他的内容让焦点自动跳转到下一个EditText控件

       最近在弄EditText控件,监听他的输入内容,在输入3个字符的时候就自动将焦点跳到另一个EditText文本框里面,找了好久,才弄到代码:

xml 文件:

   <EditText
                android:id="@+id/edittext1"
                android:focusable="true"
                android:digits="@string/alphabet_and_number"
                android:inputType="number"
                android:layout_width="35dp"
                android:textSize="18dp"
                android:enabled="true"
                android:paddingLeft="2dp"
                android:textColor="@color/actionBarColorDark"
                android:background="@drawable/shape_three"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textview"
                android:layout_alignStart="@+id/textview" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textview3"
                android:text="设置服务器ip和端口号:"
                android:textSize="18dp"
                android:textColor="@color/actionBarColorDark"
                android:layout_below="@id/edittext1"
                android:layout_alignStart="@+id/textview" />

            <EditText
                android:layout_width="35dp"
                android:id="@+id/edittext2"
                android:textSize="18dp"
                android:focusable="true"
                android:enabled="true"
                android:paddingLeft="2dp"
                android:textColor="@color/actionBarColorDark"
                android:digits="@string/alphabet_and_number"
                android:inputType="number"
                android:background="@drawable/shape_three"
                android:layout_height="wrap_content"
                android:layout_below="@id/textview3"
                android:layout_alignStart="@+id/textview3" />
java代码:

     让当前的activity 或者实现TextWatcher接口,重写三个方法,代码如下:

FragFour extends Fragment implements TextWatcher{
        editText1.addTextChangedListener(this);//给edittext设置光标改变的监听
        editText2.addTextChangedListener(this);//给edittext设置光标改变的监听

    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * are about to be replaced by new text with length <code>after</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     *
     * @param s
     * @param start
     * @param count
     * @param after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    /**
     * This method is called to notify you that, within <code>s</code>,
     * the <code>count</code> characters beginning at <code>start</code>
     * have just replaced old text that had length <code>before</code>.
     * It is an error to attempt to make changes to <code>s</code> from
     * this callback.
     *
     * @param s
     * @param start
     * @param before
     * @param count
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    /**
     * This method is called to notify you that, somewhere within
     * <code>s</code>, the text has been changed.
     * It is legitimate to make further changes to <code>s</code> from
     * this callback, but be careful not to get yourself into an infinite
     * loop, because any changes you make will cause this method to be
     * called again recursively.
     * (You are not told where the change took place because other
     * afterTextChanged() methods may already have made other changes
     * and invalidated the offsets.  But if you need to know here,
     * you can use {@link Spannable#setSpan} in {@link #onTextChanged}
     * to mark your place and then look up from here where the span
     * ended up.
     *
     * @param s
     */
    @Override
    public void afterTextChanged(Editable s) {
        Log.i("TAG","edittext1获取到焦点了1");
        if(editText1.getText().length() ==3){
            editText2.requestFocus();
        } if(editText2.getText().length() ==3){
            editText3.requestFocus();
        }if(editText3.getText().length() ==3) {
            editText4.requestFocus();
        }
    }
}
  

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值