原博客网址:http://blog.csdn.net/anobodykey/article/details/11096395
先是布局文件,如下图所示:
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- >
- <EditText
- android:id="@+id/local_gw_edit_1"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:textColor="#000"
- android:textSize="15sp"
- android:layout_marginTop="25dp"
- android:gravity="bottom"
- android:singleLine="true"
- android:inputType="number"
- android:maxLength="3"
- android:background="@drawable/login_editbox"
- android:hint="@string/default_gw_hint"/>
- <TextView
- android:id="@+id/dot_1"
- android:layout_toRightOf="@id/local_gw_edit_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/dot_tv"
- android:layout_marginTop="25dp"
- android:textColor="#000"
- android:textSize="20sp"
- android:textStyle="bold"
- android:singleLine="true"
- android:layout_alignBottom="@id/local_gw_edit_1"
- />
- <EditText
- android:id="@+id/local_gw_edit_2"
- android:layout_toRightOf="@id/dot_1"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:textColor="#000"
- android:textSize="15sp"
- android:layout_marginTop="25dp"
- android:gravity="bottom"
- android:singleLine="true"
- android:inputType="number"
- android:maxLength="3"
- android:background="@drawable/login_editbox"
- android:hint="@string/default_gw_hint"/>
- <TextView
- android:id="@+id/dot_2"
- android:layout_toRightOf="@id/local_gw_edit_2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/dot_tv"
- android:layout_marginTop="25dp"
- android:textColor="#000"
- android:textSize="20sp"
- android:textStyle="bold"
- android:singleLine="true"
- android:layout_alignBottom="@id/local_gw_edit_1"
- />
- <EditText
- android:id="@+id/local_gw_edit_3"
- android:layout_toRightOf="@id/dot_2"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:textColor="#000"
- android:textSize="15sp"
- android:layout_marginTop="25dp"
- android:gravity="bottom"
- android:singleLine="true"
- android:inputType="number"
- android:maxLength="3"
- android:background="@drawable/login_editbox"
- android:hint="@string/default_gw_hint"/>
- <TextView
- android:id="@+id/dot_3"
- android:layout_toRightOf="@id/local_gw_edit_3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/dot_tv"
- android:layout_marginTop="25dp"
- android:textColor="#000"
- android:textSize="20sp"
- android:textStyle="bold"
- android:singleLine="true"
- android:layout_alignBottom="@id/local_gw_edit_1"
- />
- <EditText
- android:id="@+id/local_gw_edit_4"
- android:layout_toRightOf="@id/dot_3"
- android:layout_width="50dp"
- android:layout_height="wrap_content"
- android:textColor="#000"
- android:textSize="15sp"
- android:layout_marginTop="25dp"
- android:gravity="bottom"
- android:singleLine="true"
- android:inputType="number"
- android:maxLength="3"
- android:background="@drawable/login_editbox"
- android:hint="@string/default_gw_hint"/>
- </RelativeLayout>
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:layout_marginBottom="20dp"
- android:id="@+id/btn_layout"
- >
- <Button
- android:id="@+id/register_btn"
- android:layout_width="90dp"
- android:layout_height="40dp"
- android:layout_marginRight="20dp"
- android:layout_alignParentRight="true"
- android:text="@string/btn_ok"
- android:background="@drawable/btn_style_green"
- android:textColor="#ffffff"
- android:textSize="18sp"
- android:onClick="onOKClick"
- />
- </RelativeLayout>
对于ip地址是分为4个EditText来实现,每一个EditText的最大输入长度均为3,输入类型都是数字。
在Activity中主要是运用TextWatcher来实现对EditText内容的监测,监测输入内容的长度,当前面的EditText的输入长度为3时,后面的EditText自动获取焦点;当后一EditText的内容为0时,前一个EditText自动获取焦点,原理就是这样,
代码如下
- class MyTextWatcher implements TextWatcher
- {
- public EditText mEditText;
- public MyTextWatcher(EditText mEditText) {
- super();
- this.mEditText = mEditText;
- }
- @Override
- public void afterTextChanged(Editable s) {
- // TODO Auto-generated method stub
- if(s.length() == 3)
- {
- if(this.mEditText == gwEdit[0])
- {
- gwEdit[1].requestFocus();
- }
- else if(this.mEditText == gwEdit[1])
- {
- gwEdit[2].requestFocus();
- }
- else if(this.mEditText == gwEdit[2])
- {
- gwEdit[3].requestFocus();
- }
- }
- else if(s.length() == 0)
- {
- if(this.mEditText == gwEdit[3])
- {
- gwEdit[2].requestFocus();
- }
- else if(this.mEditText == gwEdit[2])
- {
- gwEdit[1].requestFocus();
- }
- else if(this.mEditText == gwEdit[1])
- {
- gwEdit[0].requestFocus();
- }
- }
- }
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- // TODO Auto-generated method stub
- }
- }
给每一个EditText加上TextWatcher监听器就可以了
- private MyTextWatcher[] mTextWatcher = new MyTextWatcher[4];
- for(int i = 0; i < GW_NUMBER; i++)
- {
- gwEdit[i] = (EditText)findViewById(mIDs[i]);
- mTextWatcher[i] = new MyTextWatcher(gwEdit[i]);
- gwEdit[i].addTextChangedListener(mTextWatcher[i]);
- }
至此完工。。。