Android中在EditText输入ip地址

本文介绍了一种通过四个EditText组件实现分段输入IP地址的方法,并详细讲解了如何使用TextWatcher监听器来自动切换输入框。

原博客网址:http://blog.csdn.net/anobodykey/article/details/11096395


先是布局文件,如下图所示:


[html]  view plain  copy
  1. <RelativeLayout  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:gravity="center_horizontal"  
  5.     >  
  6.     <EditText   
  7.         android:id="@+id/local_gw_edit_1"  
  8.         android:layout_width="50dp"  
  9.         android:layout_height="wrap_content"  
  10.         android:textColor="#000"  
  11.         android:textSize="15sp"  
  12.         android:layout_marginTop="25dp"  
  13.         android:gravity="bottom"  
  14.         android:singleLine="true"  
  15.         android:inputType="number"  
  16.         android:maxLength="3"  
  17.         android:background="@drawable/login_editbox"  
  18.         android:hint="@string/default_gw_hint"/>  
  19.     <TextView  
  20.         android:id="@+id/dot_1"  
  21.         android:layout_toRightOf="@id/local_gw_edit_1"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="@string/dot_tv"  
  25.         android:layout_marginTop="25dp"  
  26.         android:textColor="#000"  
  27.         android:textSize="20sp"  
  28.         android:textStyle="bold"  
  29.         android:singleLine="true"  
  30.         android:layout_alignBottom="@id/local_gw_edit_1"  
  31.         />  
  32.     <EditText   
  33.         android:id="@+id/local_gw_edit_2"  
  34.         android:layout_toRightOf="@id/dot_1"  
  35.         android:layout_width="50dp"  
  36.         android:layout_height="wrap_content"  
  37.         android:textColor="#000"  
  38.         android:textSize="15sp"  
  39.         android:layout_marginTop="25dp"  
  40.         android:gravity="bottom"  
  41.         android:singleLine="true"  
  42.         android:inputType="number"  
  43.         android:maxLength="3"  
  44.         android:background="@drawable/login_editbox"  
  45.         android:hint="@string/default_gw_hint"/>  
  46.     <TextView  
  47.         android:id="@+id/dot_2"  
  48.         android:layout_toRightOf="@id/local_gw_edit_2"  
  49.         android:layout_width="wrap_content"  
  50.         android:layout_height="wrap_content"  
  51.         android:text="@string/dot_tv"  
  52.         android:layout_marginTop="25dp"  
  53.         android:textColor="#000"  
  54.         android:textSize="20sp"  
  55.         android:textStyle="bold"  
  56.         android:singleLine="true"  
  57.         android:layout_alignBottom="@id/local_gw_edit_1"  
  58.         />  
  59.     <EditText   
  60.         android:id="@+id/local_gw_edit_3"  
  61.         android:layout_toRightOf="@id/dot_2"  
  62.         android:layout_width="50dp"  
  63.         android:layout_height="wrap_content"  
  64.         android:textColor="#000"  
  65.         android:textSize="15sp"  
  66.         android:layout_marginTop="25dp"  
  67.         android:gravity="bottom"  
  68.         android:singleLine="true"  
  69.         android:inputType="number"  
  70.         android:maxLength="3"  
  71.         android:background="@drawable/login_editbox"  
  72.         android:hint="@string/default_gw_hint"/>  
  73.     <TextView  
  74.         android:id="@+id/dot_3"  
  75.         android:layout_toRightOf="@id/local_gw_edit_3"  
  76.         android:layout_width="wrap_content"  
  77.         android:layout_height="wrap_content"  
  78.         android:text="@string/dot_tv"  
  79.         android:layout_marginTop="25dp"  
  80.         android:textColor="#000"  
  81.         android:textSize="20sp"  
  82.         android:textStyle="bold"  
  83.         android:singleLine="true"  
  84.         android:layout_alignBottom="@id/local_gw_edit_1"  
  85.         />  
  86.     <EditText   
  87.         android:id="@+id/local_gw_edit_4"  
  88.         android:layout_toRightOf="@id/dot_3"  
  89.         android:layout_width="50dp"  
  90.         android:layout_height="wrap_content"  
  91.         android:textColor="#000"  
  92.         android:textSize="15sp"  
  93.         android:layout_marginTop="25dp"  
  94.         android:gravity="bottom"  
  95.         android:singleLine="true"  
  96.         android:inputType="number"  
  97.         android:maxLength="3"  
  98.         android:background="@drawable/login_editbox"  
  99.         android:hint="@string/default_gw_hint"/>  
  100. </RelativeLayout>  
  101. <RelativeLayout   
  102.     android:layout_width="fill_parent"  
  103.     android:layout_height="wrap_content"  
  104.     android:layout_marginTop="20dp"  
  105.     android:layout_marginBottom="20dp"  
  106.     android:id="@+id/btn_layout"  
  107.     >  
  108.      
  109.     <Button  
  110.         android:id="@+id/register_btn"  
  111.         android:layout_width="90dp"  
  112.         android:layout_height="40dp"  
  113.         android:layout_marginRight="20dp"  
  114.         android:layout_alignParentRight="true"  
  115.         android:text="@string/btn_ok"  
  116.         android:background="@drawable/btn_style_green"  
  117.         android:textColor="#ffffff"  
  118.         android:textSize="18sp"  
  119.         android:onClick="onOKClick"  
  120.         />  
  121.        
  122. </RelativeLayout>  

对于ip地址是分为4个EditText来实现,每一个EditText的最大输入长度均为3,输入类型都是数字。

在Activity中主要是运用TextWatcher来实现对EditText内容的监测,监测输入内容的长度,当前面的EditText的输入长度为3时,后面的EditText自动获取焦点;当后一EditText的内容为0时,前一个EditText自动获取焦点,原理就是这样,

代码如下

[java]  view plain  copy
  1. class MyTextWatcher implements TextWatcher  
  2. {  
  3.     public EditText mEditText;  
  4.       
  5.     public MyTextWatcher(EditText mEditText) {  
  6.         super();  
  7.         this.mEditText = mEditText;  
  8.     }  
  9.   
  10.     @Override  
  11.     public void afterTextChanged(Editable s) {  
  12.         // TODO Auto-generated method stub  
  13.         if(s.length() == 3)  
  14.         {  
  15.             if(this.mEditText == gwEdit[0])  
  16.             {  
  17.                 gwEdit[1].requestFocus();  
  18.             }  
  19.             else if(this.mEditText == gwEdit[1])  
  20.             {  
  21.                 gwEdit[2].requestFocus();  
  22.             }  
  23.             else if(this.mEditText == gwEdit[2])  
  24.             {  
  25.                 gwEdit[3].requestFocus();  
  26.             }  
  27.         }  
  28.         else if(s.length() == 0)  
  29.         {  
  30.             if(this.mEditText == gwEdit[3])  
  31.             {  
  32.                 gwEdit[2].requestFocus();  
  33.             }  
  34.             else if(this.mEditText == gwEdit[2])  
  35.             {  
  36.                 gwEdit[1].requestFocus();  
  37.             }  
  38.             else if(this.mEditText == gwEdit[1])  
  39.             {  
  40.                 gwEdit[0].requestFocus();  
  41.             }  
  42.         }  
  43.           
  44.     }  
  45.   
  46.     @Override  
  47.     public void beforeTextChanged(CharSequence s, int start, int count,  
  48.             int after) {  
  49.         // TODO Auto-generated method stub  
  50.           
  51.     }  
  52.   
  53.     @Override  
  54.     public void onTextChanged(CharSequence s, int start, int before,  
  55.             int count) {  
  56.         // TODO Auto-generated method stub  
  57.           
  58.     }  
  59.       
  60. }  

给每一个EditText加上TextWatcher监听器就可以了

[java]  view plain  copy
  1. private MyTextWatcher[] mTextWatcher = new MyTextWatcher[4];  
  2. for(int i = 0; i < GW_NUMBER; i++)  
  3. {  
  4.     gwEdit[i] = (EditText)findViewById(mIDs[i]);  
  5.     mTextWatcher[i] = new MyTextWatcher(gwEdit[i]);  
  6.     gwEdit[i].addTextChangedListener(mTextWatcher[i]);  
  7. }  

至此完工。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值