当显示键盘时,向上推除除某些视图以外的内

时间:2017-01-11 04:26:15

标签: android view android-edittext android-softkeyboard

我的布局在底部包含5 EditTextButton以及TextView。现在,当我按下EditText时,键盘会显示,全部我的View会被推升。

现在我不想将TextViewButton推到键盘上方,只想推高所有 EditText { {1}}到键盘上方。

ScrollView

enter image description here

我有个主意。当键盘显示和隐藏时我会倾听。当键盘显示我将设置ScrollView的底部边距=键盘高度,当键盘隐藏时我将设置此边距= 0 有没有办法更轻松地处理我的案子?任何帮助或建议都将非常感激。

更新
如果我使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#ff0" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="EditText 1" /> <EditText android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="EditText 2" /> <EditText android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="EditText 3" /> <EditText android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="EditText 4" /> <EditText android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="EditText 5" android:inputType="textNoSuggestions" /> </LinearLayout> </ScrollView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="Button" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText" /> </LinearLayout> =&gt;并非所有windowSoftInputMode=adjustPan都被推到键盘上方 如果我使用EditText =&gt; windowSoftInputMode=adjustResizeButton和所有TextView都会升级到键盘上方

8 个答案:

答案 0 :(得分:5)

无法工作 - 没有可靠的API来检测键盘的显示时间。您将看到&#34;解决方案&#34;在这个网站上,但他们有误报和否定。但似乎最好的办法是将softInputMode设置为adjustPan。这将使操作系统滚动整个屏幕所需的最小量,使光标在键盘上方可见。 (整个应用程序位于键盘上方是由于adjustResize模式)。

答案 1 :(得分:5)

  1. 在所有Nexus移动设备和平板电脑设备上进行测试。它工作正常。我正在使用你的布局xml代码,并且只在视图中添加了id。

  2. 我在我的生产应用程序中使用以下库,这是非常有前景的。要实现相同并在app gradle中添加以下行: 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.0.1' Here is the link for the same

  3. 有关其余代码,请参阅以下要点:Manifest code, Activity code, Activity layout code

答案 2 :(得分:3)

使用Relavtive Layout作为父级,两个线性布局作为子级,并将scrollview放在第一个LinearLayout中,而将textview放在另一个中。

使用此代码。经过测试,将按预期工作。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 1"

                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 2"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 3"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 4"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 5"
                android:inputType="textNoSuggestions"
                />

        </LinearLayout>

    </ScrollView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/linear">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button"
            />
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="I don't want to push this TextView and Button above keyboard"
            />
    </LinearLayout>
</RelativeLayout>

答案 3 :(得分:2)

您必须在android:windowSoftInputMode="adjustPan"中添加Manifest,它就像Pro一样:

<activity android:name=".your_activity_name"
        android:windowSoftInputMode="adjustPan">
    ..............

</activity>

答案 4 :(得分:2)

请尝试添加此布局,

 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#ff0"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <EditText
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="EditText 1"

            />

        <EditText
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="EditText 2"
            />

        <EditText
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="EditText 3"
            />

        <EditText
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="EditText 4"
            />

        <EditText
            android:layout_marginTop="30dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="EditText 5"
            android:inputType="textNoSuggestions"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Button"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="any text"
        />
    </LinearLayout>
  </ScrollView>


</LinearLayout>

答案 5 :(得分:2)

为所有editText,button和textview提供id: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 1"
                android:id="@+id/et1"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 2"
                android:id="@+id/et2"/>

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 3"
                android:id="@+id/et3"/>

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 4"
                android:id="@+id/et4"/>

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 5"
                android:inputType="textNoSuggestions"
                android:id="@+id/et5"/>

        </LinearLayout>

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button"
        android:id="@+id/btn"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText"
        android:id="@+id/tv"/>
</LinearLayout>

在你的清单中: -

android:windowSoftInputMode="adjustResize|stateHidden"

在您的活动中: -

public class Main2Activity extends AppCompatActivity {
    EditText e1,e2,e3,e4,e5;
    Button button;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        e1=(EditText)findViewById(R.id.et1);
        e2=(EditText)findViewById(R.id.et2);
        e3=(EditText)findViewById(R.id.et3);
        e4=(EditText)findViewById(R.id.et4);
        e5=(EditText)findViewById(R.id.et5);
        textView=(TextView)findViewById(R.id.tv);
        button=(Button)findViewById(R.id.btn);
        e1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hideKeyBoard();
            }
        });
        e2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hideKeyBoard();
            }
        });
        e3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hideKeyBoard();
            }
        });
        e4.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hideKeyBoard();
            }
        });
        e5.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hideKeyBoard();
            }
        });

    }
    private void hideKeyBoard(){
        final View activityRootView = findViewById(R.id.activity_main);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > dpToPx(Main2Activity.this, 200)) { // if more than 200 dp, it's probably a keyboard...
                    // ... do something here
                    button.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);
                }
                else {
                    button.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.VISIBLE);
                }
            }
        });
    }
    public static float dpToPx(Context context, float valueInDp) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }

}

我在这里检查键盘是否可见。如果它可见,则隐藏按钮和textview。

答案 6 :(得分:2)

首先改变布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="EditText 1"

                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="EditText 2" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="EditText 3" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="EditText 4" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:hint="EditText 5"
                android:inputType="textNoSuggestions" />

        </LinearLayout>

    </ScrollView>


    <RelativeLayout
        android:id="@+id/rlBtm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn"
            android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText"
            android:textColor="#000" />

    </RelativeLayout>

</LinearLayout>

然后添加类

public class KeyboardUtil {

    public static void setKeyboardVisibilityListener(Activity activity, final KeyboardVisibilityListener keyboardVisibilityListener) {
        final View contentView = activity.findViewById(android.R.id.content);
        contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            private int mPreviousHeight;

            @Override
            public void onGlobalLayout() {
                int newHeight = contentView.getHeight();
                if (mPreviousHeight != 0) {
                    if (mPreviousHeight > newHeight) {
                        // Height decreased: keyboard was shown
                        keyboardVisibilityListener.onKeyboardVisibilityChanged(true);
                    } else if (mPreviousHeight < newHeight) {
                        // Height increased: keyboard was hidden
                        keyboardVisibilityListener.onKeyboardVisibilityChanged(false);
                    } else {
                        // No change
                    }
                }
                mPreviousHeight = newHeight;
            }
        });
    }
}

来自onCreate的活动

KeyboardUtil.setKeyboardVisibilityListener(this, new KeyboardVisibilityListener() {
            @Override
            public void onKeyboardVisibilityChanged(boolean keyboardVisible) {
                rlBtm.setVisibility(keyboardVisible ? View.GONE : View.VISIBLE);
            }
        });

找到rlBtm的ID。

答案 7 :(得分:1)

你可以使用下面的代码(但是在这种情况下你的按钮和你的textview是屏幕的结尾。否则你也可以用线性布局管理它。如果你想要按钮和textview必须显示然后请问。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
    android:id="@+id/sw1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="100dp"
            android:background="#ff009988"
            android:padding="10dp"
            android:textColor="#ffffff" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginBottom="10dp"
            android:background="#ff009988"
            android:padding="10dp"
            android:textColor="#ffffff" />
 <RelativeLayout
    android:id="@+id/btmbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, " />
</RelativeLayout>
    </LinearLayout>
</ScrollView>

相关问题