最近在搞Android项目,之前并没有系统的去学过这方面的编程,只能边看书边撸代码。在项目的开发的过程中,需要一个IP控件,后面了解到Android中并没有这样的控件,于是网上搜索,发现得到的结果并不符合我的需求,于是我根据网上已有的完善了一下这个自定义控件,记录在此,方便下次再用,也希望能给你们留下一点帮助。
一、IP控件布局
IP控件的布局很简单,四个EditText和三个TextView组合而成的,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="200dp" android:layout_gravity="left" android:layout_height="30dp" android:background="#f2f2f2" android:focusable="true" android:focusableInTouchMode="true"> <EditText android:id="@+id/Fist_Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_gravity="center" android:gravity="center" android:inputType="numberDecimal" android:background="#FFFFFF" android:textColor="#4f4f4f" android:textCursorDrawable="@drawable/cursor_color"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="bottom" android:textStyle="bold" android:text="." android:background="#FFFFFF"/> <EditText android:id="@+id/Second_Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_gravity="center" android:gravity="center" android:inputType="numberDecimal" android:background="#FFFFFF" android:textColor="#4f4f4f" android:textCursorDrawable="@drawable/cursor_color"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="bottom" android:textStyle="bold" android:text="." android:background="#FFFFFF"/> <EditText android:id="@+id/Third_Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:inputType="numberDecimal" android:background="#FFFFFF" android:textColor="#4f4f4f" android:textCursorDrawable="@drawable/cursor_color"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="bottom" android:textStyle="bold" android:text="." android:background="#FFFFFF"/> <EditText android:id="@+id/Four_Text" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:inputType="numberDecimal" android:background="#FFFFFF" android:textColor="#4f4f4f" android:textCursorDrawable="@drawable/cursor_color"/> </LinearLayout>
二、IP逻辑实现
我们平时常用的ip控件的每个文本框内容范围都是0~255,而且当其中一个文本框内容已经是三位数或者输入“.”时,则会自动跳转到下一个文本框中。另外当我们删除文本内容,当文本内容为空时,则会自动跳转到上一个文本框中。所以这是我们实现ip控件需要注意的地方。
下面是具体实现代码
public class IPEditText extends LinearLayout { private EditText firIPEdit; private EditText secIPEdit; private EditText thirIPEdit; private EditText fourIPEdit; private String firstIP =""; private String secondIP=""; private String thirdIP=""; private String fourthIP=""; public IPEditText(Context context, AttributeSet attributeSet) { super(context, attributeSet); View view = LayoutInflater.from(context).inflate(R.layout.ip_text_layout, this); firIPEdit = (EditText) findViewById(R.id.Fist_Text); secIPEdit = (EditText) findViewById(R.id.Second_Text); thirIPEdit = (EditText) findViewById(R.id.Third_Text); fourIPEdit = (EditText) findViewById(R.id.Four_Text); setIPEditTextListener(context); } public void setIPEditTextListener(final Context context) { //设置第一个IP字段的事件监听 firIPEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().trim().equals(".")){ firstIP = ""; return; } if(s.length() > 2 || s.toString().trim().contains(".")){ if(s.toString().trim().contains(".")){ firstIP = s.toString().trim().substring(0, s.length() - 1); }else { firstIP = s.toString().trim(); } if(Integer.parseInt(firstIP) > 255){ firstIP = "255"; } secIPEdit.setFocusable(true); secIPEdit.requestFocus(); }else { firstIP = s.toString().trim(); } } @Override public void afterTextChanged(Editable s) { firIPEdit.removeTextChangedListener(this); firIPEdit.setText(firstIP); firIPEdit.setSelection(firIPEdit.length()); firIPEdit.addTextChangedListener(this); } }); //设置第二个IP字段的事件监听 secIPEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //若长度为0,返回到上一个文本编辑框 if(s.toString().length() == 0){ firIPEdit.setFocusable(true); firIPEdit.requestFocus(); } if(s.toString().trim().equals(".")){ secondIP = ""; return; } if(s.length() > 2 || s.toString().trim().contains(".")){ if(s.toString().trim().contains(".")){ secondIP = s.toString().trim().substring(0, s.length() - 1); }else { secondIP = s.toString().trim(); } if(Integer.parseInt(secondIP) > 255){ secondIP = "255"; } thirIPEdit.setFocusable(true); thirIPEdit.requestFocus(); }else { secondIP = s.toString().trim(); } } @Override public void afterTextChanged(Editable s) { secIPEdit.removeTextChangedListener(this); secIPEdit.setText(secondIP); secIPEdit.setSelection(secondIP.length()); secIPEdit.addTextChangedListener(this); } }); //设置第三个IP字段的事件监听 thirIPEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().length() == 0){ secIPEdit.setFocusable(true); secIPEdit.requestFocus(); } if(s.toString().trim().equals(".")){ thirdIP = ""; return; } if(s.length() > 2 || s.toString().trim().contains(".")){ if(s.toString().trim().contains(".")){ thirdIP = s.toString().trim().substring(0, s.length() - 1); }else { thirdIP = s.toString().trim(); } if(Integer.parseInt(thirdIP) > 255){ thirdIP = "255"; } fourIPEdit.setFocusable(true); fourIPEdit.requestFocus(); }else { thirdIP = s.toString().trim(); } } @Override public void afterTextChanged(Editable s) { thirIPEdit.removeTextChangedListener(this); thirIPEdit.setText(thirdIP); thirIPEdit.setSelection(thirdIP.length()); thirIPEdit.addTextChangedListener(this); } }); //设置第四个IP字段的事件监听 fourIPEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().length() == 0){ thirIPEdit.setFocusable(true); thirIPEdit.requestFocus(); } if(s.toString().trim().equals(".")){ fourthIP = ""; return; } if(s.length() > 2 || s.toString().trim().contains(".")){ if(s.toString().trim().contains(".")){ fourthIP = s.toString().trim().substring(0, s.length() - 1); }else { fourthIP = s.toString().trim(); } if(Integer.parseInt(fourthIP) > 255){ fourthIP = "255"; } }else { fourthIP = s.toString().trim(); } } @Override public void afterTextChanged(Editable s) { fourIPEdit.removeTextChangedListener(this); fourIPEdit.setText(fourthIP); fourIPEdit.setSelection(fourthIP.length()); fourIPEdit.addTextChangedListener(this); } }); } /** * 返回整个ip地址 * @return */ public String getIpText(){ if(TextUtils.isEmpty(firstIP) || TextUtils.isEmpty(secondIP) || TextUtils.isEmpty(thirdIP) || TextUtils.isEmpty(fourthIP)){ return null; } return firstIP + "." + secondIP + "." + thirdIP + "." + fourthIP; } /** * 本地读取的ip地址显示至界面 * @param ipText */ public void setIpText(String ipText){ if(TextUtils.isEmpty(ipText) || ipText == null){ return; } String[] temp = null; temp = ipText.split("\."); if(temp != null){ firIPEdit.setText(temp[0]); secIPEdit.setText(temp[1]); thirIPEdit.setText(temp[2]); fourIPEdit.setText(temp[3]); } } }
三、控件演示