Android/Java: EditText focus moves whole layout, a

2019-09-06 06:25发布

问题:

This phase is supposed to take all basic information and add so-called "Generals", as many as the user wants, but when I click on en "Enter your phone number" EditText, or any of the generals' EditTexts, and the virtual keyboard appears, the whole layout gets pulled up so that the focused EditText can be visible.

Problem is that I need to have the top ribbon containing "Confirm" button available at all occasions. I'm aware of the fact the user can fix that by pressing the Back button, but that'd be extremely unpractical. I need to know how to keep the ribbon up there after everything else gets pulled up by the mentioned EditTexts.

Screenshot of the layout before mentioned EditTexts are tapped: https://www.mediafire.com/convkey/2da9/4pa1balkda4y4d46g.jpg

Screenshot of the layout after mentioned EditTexts are tapped: https://www.mediafire.com/convkey/3f9e/so8qq8vud6m3h996g.jpg

Here is layout's XML. "topRibbonLL" LinearLayout should always keep its place at the top, while everything else should be pulled up by tapping the mentioned EditTexts.

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

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

        <TextView
            android:id="@+id/firstDataTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:text="Step 3: First data"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/confirmButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="Confirm"
            android:onClick="newFileStep3ConfirmPressed" />

    </LinearLayout>

    <CheckBox
        android:id="@+id/shareableCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Other people can share my file" />

    <TextView
        android:id="@+id/shareableDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:text="Description of the shareableCheckBox effect."
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ScrollView
        android:id="@+id/dataScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

            <EditText
                android:id="@+id/dataFileName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:focusable="true"
                android:focusableInTouchMode="true" >
            </EditText>

            <EditText
                android:id="@+id/dataFullName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <EditText
                android:id="@+id/dataAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPostalAddress"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <EditText
                android:id="@+id/dataPhoneNumber"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="phone"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Generals:"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <LinearLayout
                android:id="@+id/generalsContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/newGeneralButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Add new general"
                    android:onClick="newGeneralButtonPressed" />

            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

Necessary EditTexts are re-set-up after the layout is opened, so that they'd have grey text etc.

private void setupNewCardEditText(final EditText editText, final String text) {
    editText.setTextColor(R.integer.tempTextColor);
    editText.setText(text);
    editText.setGravity(Gravity.CENTER);
    editText.setFocusable(true);
    editText.setFocusableInTouchMode(true);

    editText.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                if (editText.getTextColors().getDefaultColor() == R.integer.tempTextColor) {
                    editText.setTextColor(0xFF000000);
                    editText.setText("");
                }
            } else {
                if (editText.getText().toString().equals("")) {
                    editText.setTextColor(R.integer.tempTextColor);
                    editText.setText(text);
                }
            }
        }

    });
}

回答1:

I solved my problem. I used low-priority threading with a while loop to re-position the ribbon whenever it's not on top and visible. I also had to change the root layout to RelativeLayout to I could have control of the coordinates.