android js 模拟键盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<HTML>
<HEAD>
    <TITLE>键盘隐藏</TITLE>
    <script src="http://www.u160.com/Js/jquery-1.4.4.min.js" type="text/javascript"></script>
    <style>
        input{ height:30px; line-height:30px; width:160px; text-indent:0.4em}
    </style>
</HEAD>
<BODY style="background:#eee;">
    <input class="t1 imUn" type="text" value="123" />
    <input class="t2 imUn" type="text" value="456" />
    <hr />
    <input class="t" type="text" value="888" />
    <span>删除</span>
    <div id="log" style="background:#fff;padding:5px;"></div>
    <script>
 
        /*
        $("#t").bind("focus",function(){
            //app.closeKey();
            setTimeout(function(){
                app.closeKey();
            },10)
        });
        */
        var kpObj=null;
        function kpInput(v){
            if(kpObj){
                var o=kpObj;
                var val=o.val();
                ((!val.replace("-","").length)||(kpObj[0].selectionStart==0 )) &&(v==".")&&(v="0.");//.替换成0.   val.indexOf(".")<0 &&
                kpiget();
                if(v=="-"||v=="0."){//只出现在最前方 或光标最前方
                    (!val.length)&&o.val(v)&& kpiset(-v.length);
                    (val.length)&&(val.indexOf(v)<0 && kpObj[0].selectionStart==0) && o.val(v+o.val()) && kpiset(-v.length);
                }else
                    if(!isNaN(v)||v=="."){
                       //o.val(val+""+v);
                       kpInsert(v);
                    }else{
                       if(v=="DEL"){
                            o.val(val.substr(0,val.length-1));
                       }else
                            if(v=="完成"){
                                o.blur();
                            }
                    }
                var l=o.val().length;
                o.val()=="00" &&    o.val(0);//解决00开始
                o.val(o.val().replace(".","$#$").replace(/\./g,"").replace("$#$","."));//.只出现一次
                kpiset(l-o.val().length)               
            }
 
        }
         
        function kpInsert(t){
            var i=kpObj[0].selectionStart;
            var v=kpObj.val();
            kpObj.val(v.substr(0,i)+t+v.substr(i,v.length-i));
            kpi=i+1;
        }
 
        var kpi=0;
        function kpiget(){
            kpi=kpObj[0].selectionStart;
        }
 
        function kpiset(val){
            kpObj[0].selectionStart=kpi-val;
            kpObj[0].selectionEnd=kpi-val; 
            kpi=kpi-val;
        }
         
        var isbs=false
        function kpBackSpace(){
            if(isbs) return;
            isbs=true;
            var o=kpObj[0];
            if(o){
                var i=o.selectionStart;//非IE
                $(o).val($(o).val().substr(0,i-1)+$(o).val().substr(i,$(o).val().length-i));
                o.selectionStart=i-1;
                o.selectionEnd=i-1;
            }
            isbs=false;
        }
         
        function log(txt){
            $("#log").html($("#log").html()+"<br />"+txt);
        }
         
        $(".imUn").live("focus",function(){
            kpObj=$(this);
            app.keybHide();
        }).live("blur",function(){
            objKeypad=null;
            app.keybShow();
            var v=$(this).val()
            .replace(/[^\d.-]/g,"")//去掉非.-数字
            .replace(/^[0\.-]{1,}$/g,"0");//-0.000 替换成0
            v.length && (v=v.substr(0,1)+v.substr(1).replace(/-/g,"")); //去除不在最前面的-
 
            v!=""
            &&(v=Number(v.replace(".","$#$").replace(/\./g,"").replace("$#$",".")));
            $(this).val(v);
             
        })
         
        $("span").click(function(){
        $(".t2").remove();
        }
             
        );
    </script>
</BODY>
</HTML>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<HTML>
<HEAD>
    <TITLE>键盘隐藏</TITLE>
    <script src="http://www.u160.com/Js/jquery-1.4.4.min.js" type="text/javascript"></script>
    <style>
        input{ height:30px; line-height:30px; width:160px; text-indent:0.4em}
    </style>
</HEAD>
<BODY style="background:#eee;">
    <input class="t1 imUn" type="text" value="123" />
    <input class="t2 imUn" type="text" value="456" readonly="readonly" />
    <hr />
    <input class="t" type="text" value="888" />
    <div id="log" style="background:#fff;padding:5px;"></div>
    <div style="height:30px; line-height:30px; background#fefefe; position:absolute;bottom:0;">bottom</div>
    <script>
 
 
         
        var keypad={
            obj:null,
            ki:0,//光标位置
            kiGet:function(){
                this.ki=this.obj[0].selectionStart;
            },
            kiSet:function(i){
                this.ki=this.ki-i;
                this.obj[0].selectionStart=this.ki;
                this.obj[0].selectionEnd=this.ki;  
            },
            insert:function(t){
                var i=this.obj[0].selectionStart;
                var v=this.obj.val();
                this.obj.val(v.substr(0,i)+t+v.substr(i,v.length-i));
                this.ki=i+1;
            },
            back:function(){
                var o=this.obj[0];
                if(o){
                    var i=o.selectionStart;//非IE
                    $(o).val($(o).val().substr(0,i-1)+$(o).val().substr(i,$(o).val().length-i));
                    o.selectionStart=i-1;
                    o.selectionEnd=i-1;
                }
            },
            input:function(v){
                var o=this.obj;
                if(o){
                    log("1");
                    var val=o.val();
                    ((!val.replace("-","").length)||(o[0].selectionStart==0 )) &&(v==".")&&(v="0.");//.替换成0.   val.indexOf(".")<0 &&
                    log("2");
                    this.kiGet();
                    if(v=="-"||v=="0."){//只出现在最前方 或光标最前方
                        log("3");
                        (!val.length)&&o.val(v)&& this.kiSet(-v.length);
                        (val.length)&&(val.indexOf(v)<0 && o[0].selectionStart==0) && o.val(v+o.val()) && this.kiSet(-v.length);
                    }else
                        if(!isNaN(v)||v=="."){
                           log("04");
                           this.insert(v);
                           log("05");
                        }else{
                           if(v=="DEL"){
                                o.val(val.substr(0,val.length-1));
                           }else
                                if(v=="完成"){
                                    o.blur();
                                }
                        }
                    log("6");
                    var l=o.val().length;
                    o.val()=="00" &&    o.val(0);//解决00开始
                    o.val(o.val().replace(".","$#$").replace(/\./g,"").replace("$#$","."));//.只出现一次
                    this.kiSet(l-o.val().length)               
                }
            }
        }
 
        function log(txt){
            android&&android.webLog(txt);
            //$("#log").html($("#log").html()+txt);//+"<br />"
        }
         
        $(".imUn").live("focus",function(){
            keypad.obj=$(this);
            android.keybHide();
        }).live("blur",function(){
            log("blur");
            keypad.obj=null;
            android.keybShow();
            var v=$(this).val()
            .replace(/[^\d.-]/g,"")//去掉非.-数字
            .replace(/^[0\.-]{1,}$/g,"0");//-0.000 替换成0
            v.length && (v=v.substr(0,1)+v.substr(1).replace(/-/g,"")); //去除不在最前面的-
 
            v!=""
            &&(v=Number(v.replace(".","$#$").replace(/\./g,"").replace("$#$",".")));
            $(this).val(v);
             
        })
         
         
        $(function(){
            alert(0;)
        })
         
         
 
    </script>
</BODY>
</HTML>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<HTML>
<HEAD>
    <TITLE>键盘隐藏</TITLE>
    <script src="http://www.u160.com/Js/jquery-1.4.4.min.js" type="text/javascript"></script>
    <style>
        input[type=text]{ height:30px; line-height:30px; width:160px; text-indent:0.4em}
        input[type=button]{ height:30px; line-height:30px;}
    </style>
</HEAD>
<BODY style="background:#eee;">
    <input class="t1 imUn" type="text" value="123" />
    <input class="tt" type="button" value="处理" />
    <script>
        $(".tt").click(function(){
         
            //$(".t1").val($(".t1").val().replace(/[^\d.-]/g,""));
             
            //去掉结尾
            //$(".t1").val($(".t1").val().replace(/\.$/g,""));
             
            //去开头的0
            //$(".t1").val($(".t1").val().replace(/^0*./g,"")); //n0.替换掉0
             
            //$(".t1").val($(".t1").val().replace(/^\./g,"0."));
             
             
            //alert((/^[0\.-]{1,}$/g).test($(".t1").val()));
             
 
             
            var v=$(".t1").val()
            .replace(/[^\d.-]/g,"")//去掉非.-数字
            .replace(/^[0\.-]{1,}$/g,"0");//-0.000 替换成0
            v.length && (v=v.substr(0,1)+v.substr(1).replace(/-/g,"")); //去除不在最前面的-
 
            v!=""
            &&(v=Number(v.replace(".","$#$").replace(/\./g,"").replace("$#$",".")));
            $(".t1").val(v);
             
            /*
                        v!="0"
            &&(
                v=v
                .replace(/\.$/g,"")//去掉末尾的.
                .replace(/^0{1,}\./g,".")//先替换n0.的
                .replace(/^0{1,}/g,"")//再替换开头为n个0(不然 0. 变成 .开头) 
                .replace(/^\./g,"0.")//修复.为0.          
            )
             
             
            var v=$(".t1").val();
            v=v.replace(v.substr(1),v.replace(/-/g,""))
            $(".t1").val(v);
            */
             
             
        });
    </script>
</BODY>
</HTML>

  

posted @   skyhap  阅读(222)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
点击右上角即可分享
微信分享提示