Push up content except some view when keyboard sho

2019-02-08 12:09发布

I have a layout that contains 5 EditText and a Button and a TextView at bottom. Now when I press an EditText then the keyboard will shown and all my View is push up.

Now I don't want to push my TextView and Button to above keyboard, just only want to push up all EditText inside ScrollView to above keyboard.

<?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>

enter image description here

I have an idea is. When I will listener when keyboard show and hide. When keyboard show I will set the bottom margin of ScrollView = keyboard height, when keyboard hide I will set this margin = 0.
Is there any way easier to handle my case? Any help or suggestion would be great appreciated.

UPDATE
If I use windowSoftInputMode=adjustPan => not all EditText is push up to above keyboard
If I use windowSoftInputMode=adjustResize => Button, TextView and all EditText is push up to above keyboard

8条回答
该账号已被封号
2楼-- · 2019-02-08 12:37

Give id to all editText,button and textview:-

<?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>

In your Manifest:-

android:windowSoftInputMode="adjustResize|stateHidden"

In your Activity:-

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);
    }

}

Here i am checking whether keyboard is visible or not. And if it is visible then hiding the button and textview.

查看更多
The star\"
3楼-- · 2019-02-08 12:38

Won't work- there is no reliable API to detect when the keyboard is shown. You'll see "solutions" on this site, but they have false positives and negatives. But it seems like the best thing for you is to set the softInputMode to adjustPan. This will make the OS scroll the entire screen by the minimum amount needed to make the cursor visible above the keyboard. (the entire app going above the keyboard is due to the mode adjustResize).

查看更多
登录 后发表回答