简体   繁体   English

软键盘和编辑文本问题

[英]Issue with Soft Keyboard and Edit Text

I have an Edit Text at the bottom of my app. 我的应用程序底部有一个“编辑文本”。 I want to achieve that always when it gets a Touch Event, the Soft Keyboard appears scrolling the whole window to the top, so that my EditText remains visible. 我想始终在获得触摸事件时实现该功能,软键盘会出现,将整个窗口滚动到顶部,这样我的EditText仍然可见。

The problem is that sometimes the window does not scroll at all and the Edit Text remains hidden behind the Soft Keyboard. 问题在于,有时窗口根本无法滚动,并且“编辑文本”仍然隐藏在软键盘后面。 The weird thing is that, in those moments, if I press anything or interact with other elements in the window, apparently it refreshes or something like that and then it scrolls correctly to the top, allowing me to see the Edit Text. 奇怪的是,在那一刻,如果我按任何东西或与窗口中的其他元素进行交互,显然它会刷新或类似的东西,然后正确滚动到顶部,让我看到“编辑文本”。

I have try different things and this is my end code: 我尝试了不同的方法,这是我的最终代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="@android:color/black">

<my.Custom_Edit_Text
    android:id="@+id/my_custom_edit_text"
    style="@style/my_custom_edit_text_Style"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:maxLength="@integer/data_title_max_length"
    android:text="@string/my_custom_edit_text_default" 
    android:selectAllOnFocus="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:cursorVisible="true"
    android:inputType="text"/>

This is my Custom_Edit_Text code, where I handle the onTouch event: 这是我的Custom_Edit_Text代码,我在其中处理onTouch事件:

setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                requestFocus();
            }
            return false;
        }
    });

I have also set this attribute in the Manifest file: 我还在清单文件中设置了此属性:

android:windowSoftInputMode="stateHidden|adjustPan" 

use this 用这个

android:configChanges="keyboardHidden|orientation"

and

android:windowSoftInputMode="adjustPan"

Let me know using both this are helpful to you or not and also use scrollview in the layout where your virtual keyboard is creating problem it also help a bit 让我知道同时使用这对您是否有帮助,还可以在您的虚拟键盘造成问题的布局中使用scrollview,这也有一定帮助

put your whole layout which is distroting in a scrollview 将整个布局放到scrollview中

This post gave me the clue to solve my problem https://stackoverflow.com/a/5989385/1382250 这篇文章给了我解决问题的线索https://stackoverflow.com/a/5989385/1382250

I just tried to change my Edit Text for a Text View and now it works perfectly (scrolls the window in all cases). 我只是尝试更改“文本视图”的“编辑文本”,现在它可以正常工作(在所有情况下均可滚动窗口)。

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

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