简体   繁体   English

单击搜索输入时,Android 键盘出现和消失

[英]Android keyboard appears and disappears when clicking search input

I am using Google Maps Js API and my input search bar works fine when I search for a place.我正在使用Google Maps Js API ,当我搜索一个地方时,我的输入搜索栏工作正常。 When i use it via iPhone devices it works fine too, but when I use it via Android devices then the keyboard appears and disappears instantly .当我通过 iPhone 设备使用它时,它也工作正常,但是当我通过Android 设备使用它时,键盘会立即出现和消失 I have already found some solutions about the android keyboard losing focus when i press the search bar, but I can't understand how or where I should implement the solution.我已经找到了一些关于当我按下搜索栏时 android 键盘失去焦点的解决方案,但我不明白我应该如何或在哪里实施该解决方案。 I have the following part of code:我有以下部分代码:

Here is my input and onclick event and my search bar:这是我的输入和 onclick 事件以及我的搜索栏:

<div class="col-sm-12" style="padding-right: 0px; margin-bottom: 10px;">
  <span>Closest points</span>
  <input id="locationTextField" type="text" size="40" float: left;">
  <button class="btn pointsok" onclick='gotocustomlocation()' style="float: left; margin-left: 2px; width: 33px; padding-left: 6px; padding-right: 6px;">OK</button>
</div>

Maybe something above autocomplete.getPlace could fix the problem ?也许autocomplete.getPlace上面的东西可以解决这个问题?

<script>
            function gotocustomlocation() {
                var place = autocomplete.getPlace();
                var lat = place.geometry.location.lat();
                var lng = place.geometry.location.lng();
                console.log(lat);
                console.log(lng);
                jQuery.ajax({
                    type: 'GET',
                    url: "<?php echo JURI::base() . "index.php?option=com_ajax&module=points&format=json&method=getClosestAreas&lat=";?>"  + lat + "&lng=" + lng,
                    success:function(resp){
                        if (typeof map !== 'undefined') {
                            jQuery('#prefectures option[value="noval"]').attr("selected",true);
                            jQuery('#subPrefectures').find('option').remove();
                            jQuery('#subPrefectures').append(jQuery("<option></option>").attr("value","noval").text('Choose Area')); 
                            jQuery('#subPrefectures').prop('disabled', 'disabled');                            
                            kmlLayer.forEach(function(l) {
                                l.setMap(null);
                            });                            
                            var latLng = new google.maps.LatLng(lat, lng);
                            map.setZoom(11);
                            map.panTo(latLng);                            
                        }
                        var distances = JSON.parse(resp.data);
                        var htmlcontent = "Closest Spot<br />";
                        for (var i=0; i<10; i++) {
                            var uri =  "<?php echo JURI::base(); ?>" + "weather/" + distances[i].uri;
                            htmlcontent += "<span style='display:block;float:left;white-space:nowrap;'> <a href='"+uri+"'>"+distances[i].alias+"</a>";
                            if (i < 10) {
                                htmlcontent += " | </span>";
                            } else {
                                htmlcontent += "</span>";
                            }                            
                        }
                        jQuery("#pointsareas").html(htmlcontent);
                    },
                    error:function(){
                    }
                });  
            }
            //google.maps.event.addDomListener(window, 'load', init);
        </script>

Mind that google.maps.event.addDomListener(window, 'load', init);请注意google.maps.event.addDomListener(window, 'load', init); is not affecting the search bar.不影响搜索栏。

Should it be a problem in JS, or it could be a wrong @media resizing in CSS ?它应该是 JS 中的问题,还是可能是CSS 中错误的@media 调整大小

The problem is when the soft keyboard appears, it triggers a window resize.问题是当软键盘出现时,它会触发窗口大小调整。 When you are working with a responsive website, as I am, you are likely to have code that handles resizing.当您使用响应式网站时,就像我一样,您可能拥有处理调整大小的代码。 When the keyboard appears, a window resize occurs and the keyboard disappears due to custom resize code当键盘出现时,发生窗口调整大小,键盘消失由于自定义调整大小代码

If you rewrite resize events to be orientationchange events.如果您将调整大小事件重写为orientationchange 事件。 While this isn't an ideal situation, if an app is the only target, then it should be fine.虽然这不是一个理想的情况,但如果一个应用程序是唯一的目标,那么它应该没问题。

To clarify, change from (using jQuery):为了澄清,从(使用jQuery)更改:

$(window).resize (function ()
{
    // Code on resize
});

Change to (using jQuery):更改为(使用 jQuery):

$(window).bind ('orientationchange', function ()
{
    // Code on screen rotation
});

Also, An alternative solution suggested by chrisdew here suggests此外,另一种解决方案建议通过chrisdew这里建议

"adding a listener to the input's focus event which disables reacting to resize events for 0.5s.". “向输入的焦点事件添加一个侦听器,该事件禁用对调整大小事件的响应 0.5 秒。”。 This should also work if you want to have a 0.5s delay whenever an input is focused.如果您希望在输入焦点时有 0.5 秒的延迟,这也应该有效。

I had the same problem and @amit's solution helped.我遇到了同样的问题,@amit 的解决方案有所帮助。 The problem is, that clicking on the searchbar somehow triggers the "orientationchange" or "resize" event, maybe the keyboard which shows triggers it.问题是,点击搜索栏会以某种方式触发“orientationchange”或“resize”事件,可能是显示的键盘触发了它。 I don't know.我不知道。

After the event is triggered, the searchbar gets moved to the mobile container, even though it is already in this container.事件触发后,搜索栏会移动到移动容器中,即使它已经在此容器中。 So it gets unlinked from its container and linked to it again.所以它从它的容器中取消链接并再次链接到它。 This causes the searchbar to loose focus, so the browser hides the keyboard.这会导致搜索栏失去焦点,因此浏览器会隐藏键盘。

Problem Code:问题代码:

$(window).on("orientationchange resize", function() { breakpointChecks(); });

function breakpointChecks()
{
        ifMaxWidth(767, function() {
            $searchForm.appendTo($mobileSearchContainer);
        });

        ifMinWidth(768, function() {
            $searchForm.appendTo($desktopSearchContainer);
        });
}

Solution:解决方案:

Add a variable which stores the current mode (desktop or mobile) and only trigger the move code if we are not already on this mode.添加一个存储当前模式(桌面或移动)的变量,如果我们尚未处于此模式,则仅触发移动代码。

var currentMode = "";
$(window).on("orientationchange resize", function() { breakpointChecks(); });

function breakpointChecks()
{
    if (currentMode != "mobile") {
        ifMaxWidth(767, function() {
            currentMode = "mobile";
            $searchForm.appendTo($mobileSearchContainer);
            $loginRegisterBox.appendTo($headerContainerMobile);
        });
    }

    if (currentMode != "desktop") {
        ifMinWidth(768, function() {
            currentMode = "desktop";
            $searchForm.appendTo($desktopSearchContainer);
            $loginRegisterBox.appendTo($desktopLoginRegisterContainer);
        });
    }
}

Have you enabled Google Maps and Google Maps SDK on Google developer console for this project?您是否为此项目在 Google 开发者控制台上启用了 Google Maps 和 Google Maps SDK? Please cross check and enable if they are not enabled.如果未启用,请交叉检查并启用。 You will have to enable both for this.您必须为此启用两者。 I was also getting the same error and the problem was not in code actually.我也遇到了同样的错误,问题实际上不在代码中。

The solution on this was to strip the code out, and re ordering any div that might cause the error.对此的解决方案是剥离代码,并重新排序可能导致错误的任何div So to fix this, since the implementation of the search bar was in the sidebar, I had to play again with all elements of my template, re-ordering them and find a position (to be more specific the appropriate width / height / padding rules) that is not overcoming the Bootstrap's full width of rows and columns.所以为了解决这个问题,由于搜索栏的实现是在侧边栏中,我不得不再次使用我模板的所有元素,重新排序它们并找到一个位置(更具体地说,适当的width / height / padding规则) 没有克服 Bootstrap 的行和列的全宽。 That's pretty much of it without using any JavaScript to detect and change the orientation.几乎没有使用任何JavaScript来检测和更改方向。 Only pure HTML re ordering.仅纯HTML重新排序。

We had the same problem, in our case it was CSS media query rule for landscape.我们遇到了同样的问题,在我们的例子中,它是横向的 CSS 媒体查询规则。

To fix such issue, try go from beginning, disable all CSS and get simple input field in a body, than look what styles/blocks cause reflow.要解决此类问题,请尝试从头开始,禁用所有 CSS 并在正文中获取简单的输入字段,而不是查看哪些样式/块会导致回流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM