android IP輸入框

來源:互聯網
上載者:User

本文提供一種IP輸入框的實現方法,會過濾非IP字元的輸入,同時過濾非法IP的輸入,同時提供確定和取消事件的處理介面。具體實現如下:

[java] 
        private static String tmp = ""; 
 
        // IP 輸入框 
    public static void showIPInputDialog(final Context context, final String title, final String initIp, final onIPInputDialogProcess listener){ 
        tmp = ""; 
        final EditText ip = new EditText(context); 
        ip.setText(initIp); 
        ip.setInputType( InputType.TYPE_CLASS_NUMBER | InputType.TYPE_CLASS_TEXT ); 
        ip.addTextChangedListener(new TextWatcher(){ 
 
            public void afterTextChanged(Editable s) { 
                String str = s.toString(); 
                if( str.length() == 0 || str.equals(tmp)) return; 
                tmp = checkIp(str); 
                ip.setText(tmp); 
                ip.setSelection(tmp.length()); 
            } 
 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
            } 
 
            public void onTextChanged(CharSequence s, int start, int before, int count) { 
            }}); 
 
        new AlertDialog.Builder(context) 
        .setTitle(title) 
        .setView(ip) 
        .setPositiveButton(R.string.dialog_bt_ok, new OnClickListener(){ 
            public void onClick(DialogInterface dialog, int which) { 
                String ipText = ip.getText().toString(); 
                if( ipText.split("\\.").length == 4 ){ 
                    if( listener != null ) listener.onIPInputConfirm(ip.getText().toString()); 
                }else{ 
                    showIPInputDialog(context, title, ipText, listener); 
                } 
            }}) 
        .setNegativeButton(R.string.dialog_bt_cancel, new OnClickListener(){ 
            public void onClick(DialogInterface dialog, int which) { 
                if( listener != null ) listener.onIPInputCancel(); 
            }}) 
        .show(); 
    } 
 
    private static String checkIp(String ip){ 
        String validStr = ".0123456789"; 
        StringBuffer sb = new StringBuffer(); 
        int i = 0; 
 
        for( i = 0; i < ip.length(); i++){ 
            if( validStr.indexOf(ip.charAt(i)) >= 0 ) sb.append(ip.charAt(i)); 
        } 
        if( sb.toString().length() == 0 ) return ""; 
 
        String newIP = ""; 
        String[] arrIp = sb.toString().split("\\."); 
        for( i = 0; i < arrIp.length && i < 4 ; i++){ 
            if( arrIp.equals("")) break; 
            if( i > 0 && i < 4 ) newIP += "."; 
            if( Integer.parseInt(arrIp[i]) > 255 ){ 
                newIP += String.format("%d", Integer.parseInt(arrIp[i])/10); 
            }else{ 
                newIP += arrIp[i]; 
            } 
        } 
        if( i < 4 && sb.toString().endsWith("-")) newIP += "."; 
        return newIP; 
    } 
 
    interface onIPInputDialogProcess{ 
        void onIPInputConfirm(String ip); 
        void onIPInputCancel(); 
    } 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.