ip地址控件写的很多,但是一调试有许多BUG,于是改良了一下。
初始化控件
private void initData(){
String ip = SharedPreferencesUtils.getValue(getBaseContext(), "sServerIp", "127.0.0.1");
int sServerPort = SharedPreferencesUtils.getValue(getBaseContext(), "sServerPort", 80);
String IP[] = ip.split("\\.");
firstIP.setText(IP[0]);
secondIP.setText(IP[1]);
thirdIP.setText(IP[2]);
fourthIP.setText(IP[3]);
port.setText(String.valueOf(sServerPort));
}
地址控件监听
public void setIPEditTextListener() {
//设置第一个字段的事件监听
firstIP.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Log.i("test", s.toString());
int index = s.length();
if (null != s && index > 0) {
if (index > 3) {//如果位数大于3,则直接下一个控件获取光标
secondIP.setFocusable(true);
secondIP.requestFocus();
} else {//如果位数不大于3,则判断是否大于255,
if (Integer.parseInt(s.toString()) > 255) {
Toast.makeText(MainActivity.this, "IP大小在0-255之间",
Toast.LENGTH_LONG).show();
return;
}
sFirstIP = s.toString().trim();
}
} else {
sFirstIP = "";//这一步必须写,否则无法更换最后一位数字
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
firstIP.removeTextChangedListener(this);
firstIP.setText(sFirstIP);
firstIP.setSelection(firstIP.length());
firstIP.addTextChangedListener(this);
}
});
//设置第二个IP字段的事件监听
secondIP.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
int index = s.length();
if (null != s && index > 0) {
if (index > 3) {
thirdIP.setFocusable(true);
thirdIP.requestFocus();
} else {
if (Integer.parseInt(s.toString()) > 255) {
Toast.makeText(MainActivity.this, "IP大小在0-255之间",
Toast.LENGTH_LONG).show();
return;
}
sSecondIP = s.toString().trim();
}
} else {
sSecondIP = "";
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
secondIP.removeTextChangedListener(this);
secondIP.setText(sSecondIP);
secondIP.setSelection(secondIP.length());
secondIP.addTextChangedListener(this);
}
});
//设置第三个IP字段的事件监听
thirdIP.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
int index = s.length();
if (null != s && index > 0) {
if (index > 3) {
fourthIP.setFocusable(true);
fourthIP.requestFocus();
} else {
if (Integer.parseInt(s.toString()) > 255) {
Toast.makeText(MainActivity.this, "IP大小在0-255之间",
Toast.LENGTH_LONG).show();
return;
}
sThirdIP = s.toString().trim();
}
} else {
sThirdIP = "";
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
thirdIP.removeTextChangedListener(this);
thirdIP.setText(sThirdIP);
thirdIP.setSelection(thirdIP.length());
thirdIP.addTextChangedListener(this);
}
});
//设置第四个IP字段的事件监听
fourthIP.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
int index = s.length();
if (null != s && index > 0) {
if (index > 3) {
port.setFocusable(true);
port.requestFocus();
} else {//如果
if (Integer.parseInt(s.toString()) > 255) {
Toast.makeText(MainActivity.this, "请输入合法的ip地址", Toast.LENGTH_LONG)
.show();
return;
}
sFourthIP = s.toString().trim();
}
} else {
sFourthIP = "";
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
fourthIP.removeTextChangedListener(this);
fourthIP.setText(sFourthIP);//最终的取值
fourthIP.setSelection(fourthIP.length());//设置全选
fourthIP.addTextChangedListener(this);
}
});
}
下面是布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/activity_ip_setting_title_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="IP设置"
android:textSize="30sp"
android:gravity="center"
android:textColor="@color/white"
android:background="#499bf7"/>
<LinearLayout
android:id="@+id/layout"
android:layout_below="@+id/activity_ip_setting_title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:gravity="center|left"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="IP地址"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:background="@drawable/login_input_bg">
<EditText
android:id="@+id/firstIPfield"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="@null"
android:maxLength="4"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."/>
<EditText
android:id="@+id/secondIPfield"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="@null"
android:maxLength="4"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."/>
<EditText
android:id="@+id/thirdIPfield"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="@null"
android:maxLength="4"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."/>
<EditText
android:id="@+id/fourthIPfield"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="@null"
android:maxLength="4"
android:inputType="number"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="端口"
android:textSize="18sp"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/activity_ip_setting_input_port"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:background="@drawable/login_input_bg"
android:inputType="number"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_below="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="保存"/>
</RelativeLayout>