Keyboard placement obscures view below EditText would like to keep visible

喜欢而已 提交于 2019-12-23 22:57:45

问题


I have an activity that is basically a long form of entry fields. On each row, I want to show a TextView to serve as hint text just below each EditText and I want the TextView to remain visible at all times when the user is entering data. Unfortunately, the soft keyboard obscures the hint text and always positions itself immediately below the EditText. Is there any technique that will allow the TextView below the EditText to also be visible when the soft keyboard appears and the contents are adjusted (via windowSoftInputMode=adjustResize|adjustPan), without having the user scroll ?


回答1:


Vishavjeet got me on the right track in suggesting I scrolldown to reveal the view that may be overlapped by the keyboard. Below is a function similar to what I used to solve the problem. It can be called when the EditText above the TextView receives focus:

    //       View targetView; // View that may be hidden by keyboard
    //       ScrollView scrollContainerView; // Scrollview containing hiddenView
    //
    void assureViewVisible (View targetView, ScrollView, scrollContainerView) {        
        Window rootWindow = activity.getWindow();
        Rect rMyView = new Rect();
        View rootview = rootWindow.getDecorView();
        rootview.getWindowVisibleDisplayFrame(rMyView); // Area not taken up by keyboard

        int subTextPos[] = new int[2];
        targetView.getLocationInWindow(subTextPos); // Get position of targetView
        int subTextHt = targetView.getHeight();     // Get bottom of target view

        if ((subTextPos[1]+subTextHt) > rMyView.bottom) { // Is targetView at all obscured?
           int scrollBy = (subTextPos[1]+subTextHt) - rMyView.bottom + 10; // add a small bottom margin
           mMeasurementViewScrollView.smoothScrollBy(0, scrollBy); // Scroll to subtext
        }
   }



回答2:


EDIT: By understanding the problem more deeply, I think that you should add scroll programatically when user clicks on the Edittext. Here is the code to do that:

private final void focusOnView()
{ 
 new Handler().post(new Runnable() 
 { 
 @Override
 public void run() 
 { 
 your_scrollview.scrollTo(0, your_EditBox.getBottom()); 
 }}); 
 }

From my personal experience I think there is not such way to do that. The thing you can do is place the hint textview toRightOf the editext. Or Use modern Approach by using a Hint Placeholder on Edittext:

In XML, it's simply android:hint="someText"

Programatically you can use edittext.setHint(int);

pass R.string.somestring in above method.



来源:https://stackoverflow.com/questions/33505374/keyboard-placement-obscures-view-below-edittext-would-like-to-keep-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!