Android IP地址控件

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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章