Android IP地址控件
来源:互联网 发布:rapidgator是什么软件 编辑:程序博客网 时间:2024/05/21 10:59
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>
阅读全文
0 0
- Android IP地址控件
- Silverlight IP地址控件
- IP地址控件小技巧
- IP地址控件小技巧
- C#实现IP地址控件
- javascript ip地址 输入控件
- IP地址控件的使用
- mfc关于IP地址控件
- Qt版IP地址控件
- IP控件获取IP地址的方法
- android 获取IP地址
- ANDROID 获取IP地址
- android 获取ip地址
- android获取IP地址
- android模拟器IP地址
- android 获取IP地址
- android 获取IP地址
- Android 获取IP地址
- 11周 项目1
- Unity For循环另外一种写法
- MongoDB范式化与反范式化
- urllib问题
- 图片上传预览
- Android IP地址控件
- 计算机等级考试--二级C语言1
- java学习笔记(四)集合
- 面向对象的特征--继承
- 第十一周【项目4
- linux调试器的实现---有关寄存器操作的实现
- Leetcode: 712. Minimum ASCII Delete Sum for Two Strings
- ART异常处理机制(1)
- maven(记录)