screen scrolling up when clicking on any EditText

2019-07-07 03:45发布

问题:

I have one screen with 4 EditText like menuName, DishName,Tag and Price.
I need to set my tag Edittext at top(+56dip) on clicking on it. Right now it's working fine.

When i click on Tag EditText then it's shift up like what i want.

Where i stuck

Shifting layout up is working fine but if i click on other EditText then it will also shifting up which i don't want. I just wan't scroll Screen up when i click on Tag EditText.

Here is my java code.

    final View activityRootView = findViewById(R.id.RelativeAdd);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - activityRootView.getHeight();
                    Log.i("TEST",
                            "GLOBAL LAYOUT " + activityRootView.getHeight());
                    Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        performScrollingUp();
                        Log.i("TEsT", "Keyboard visible");
} else {
    Log.i("TEsT", "Keyboard not visible");
    performScrollingDown();
    txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
});      
     protected void performScrollingUp() {
    if (!isTagUp) {
        txtDishTags.setCursorVisible(true);
        txtDishTags.setFocusable(true);
        txtDishTags.setFocusableInTouchMode(true);
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(txtDishTags, 0);

        firstRelativeLayout.setVisibility(View.GONE);
        secondRelativeLayout.setVisibility(View.GONE);
        firstLinearLayout.setVisibility(View.GONE);
        secondLinearLayout.setVisibility(View.GONE);

        rlp = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                android.app.ActionBar.LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
        tagRelativeLayout.setLayoutParams(rlp);
        isTagUp = true;
        final float scale = getResources().getDisplayMetrics().density;
        int width = (int) (60 * scale + 0.5f);
        int height = (int) (44 * scale + 0.5f);
        rlp2 = new RelativeLayout.LayoutParams(width, height);
        rlp2.addRule(RelativeLayout.BELOW, R.id.tempRelativeLayout);
        rlp2.setMargins(24, 0, 0, 0);
        thirdRelativLayout.setLayoutParams(rlp2);
        lbltag.setGravity(Gravity.CENTER);

    }

}

xml file code for understanding.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFEFEF" 
android:orientation="vertical">

<ViewStub
    android:id="@+id/vsHeader2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inflatedId="@+id/header"
    android:layout="@layout/copyofheader" />

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">

    <RelativeLayout
        android:id="@+id/myRelativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/hotelName"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="Name on menu (optional)"
                android:inputType="textCapWords"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
                <requestFocus />
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/firstRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="12dip" >

            <TextView
                android:id="@+id/labelName"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="NAME"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/firstRelativeLayout"
            android:layout_marginTop="-2dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/dishName"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="(require)"
                android:inputType="textCapWords|textPersonName"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/secondRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/firstRelativeLayout"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labelDish"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="DISH"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/linearLayout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/secondRelativeLayout"
            android:layout_marginTop="-2dip"
             >

            <AutoCompleteTextView
                android:id="@+id/txtTags"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:minHeight="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:hint="eg: Breakfast, spicy, italian "
                android:inputType="textNoSuggestions|textMultiLine"
                android:imeOptions="actionNext"
                android:paddingLeft="70dip"     
                android:paddingTop="2dip"  
                android:paddingBottom="2dip"            
                android:dropDownHeight="fill_parent"
                android:dropDownWidth="match_parent"
                android:textColor="#333333"
                android:singleLine="false"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </AutoCompleteTextView>

            <RelativeLayout
                android:id="@+id/progressLayout"
                android:layout_width="23dip"
                android:layout_height="23dip"
                android:layout_alignRight="@+id/txtTags"
                android:layout_centerVertical="true"
                android:layout_marginRight="4dip"
                android:layout_marginTop="10dip"
                android:layout_below="@+id/linearLayout2"
                android:visibility="gone" >

                <ProgressBar
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </RelativeLayout>
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/thirdRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/secondRelativeLayout"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labeltags"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="TAGS"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayout3"
            android:layout_marginTop="-2dip"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/menuPrice"
                android:layout_width="fill_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:background="@drawable/borderforloginedittext"
                android:ems="10"
                android:hint="(optional) "
                android:inputType="number"
                android:paddingLeft="70dp"
                android:textColor="#333333"
                android:textColorHint="#999999"
                android:textSize="16sp" >
            </EditText>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/fourthRelativeLayout"
            android:layout_width="60dip"
            android:layout_height="44dip"
            android:layout_below="@+id/linearLayout3"
            android:layout_marginLeft="12dip"
            android:layout_marginTop="-2dip" >

            <TextView
                android:id="@+id/labelprice"
                android:layout_width="60dip"
                android:layout_height="44dip"
                android:gravity="center"
                android:text="PRICE"
                android:textColorHint="#999999"
                android:textSize="10sp"
                android:visibility="visible" >
            </TextView>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/fourthRelativeLayout"
            android:layout_marginTop="5dip" >

            <Button
                android:id="@+id/btnAddSecondaryHotel"
                android:layout_width="match_parent"
                android:layout_height="44dip"
                android:layout_marginLeft="12dip"
                android:layout_marginRight="12dip"
                android:layout_marginTop="12dip"
                android:layout_weight="0.5"
                android:background="@color/disabledButtonColor"
                android:text="Let&apos;s go rate this dish!"
                android:textColor="@color/white"
                android:textSize="18sp" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="57dip"
    android:layout_height="57dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="30dip"
    android:layout_marginTop="100dip"
    android:src="@drawable/dishoomit" />

I try hard but din't get anything. Please give me any reference or hint.

回答1:

In your AndroidManifest.xml that can solve this problem

    <activity 
        android:name="com.example.mainActivity"
        android:windowSoftInputMode="adjustPan">
    </activity>


回答2:

Your code only checks for the heightDiff being over 100 before you execute performScrollingUp()and your comment indicates that would probably occur when a keyboard pops up. Well when you click on any editText field a keyboard would pop up...

The Fix:

Try changing your first block of code to this:

final View activityRootView = findViewById(R.id.RelativeAdd);
EditText tagEditText = findViewById(R.id.txtTags);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - activityRootView.getHeight();
                    Log.i("TEST",
                            "GLOBAL LAYOUT " + activityRootView.getHeight());
                    Log.i("TEST", "GLOBAL LAYOUT rel"+ relativeLayoutHeight);
                    if (heightDiff > 100 && tagEditText.isFocused()) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        performScrollingUp();
                        Log.i("TEsT", "Keyboard visible");
} else {
    Log.i("TEsT", "Keyboard not visible");
    performScrollingDown();
    txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
}
}
}); 

This will make sure that it only scrolls up when the tagEditText was the one clicked and not the others. Right now you are just listening for an overall layout change with your listener. Let me know if this works.



回答3:

I'm not sure if this works. But I would at least do it like this:

if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
    if(!isTagUp)
        performScrollingUp();
    Log.i("TEsT", "Keyboard visible");
    isTagUp = true; // Keyboard is up
} else {
    Log.i("TEsT", "Keyboard not visible");

    if(isTagUp)
    {
        performScrollingDown();
        txtDishTags.setDropDownHeight(ViewGroup.LayoutParams.FILL_PARENT);
    }
    isTagUp = false; // Keyboard is down again
}

This way the function doesn't even get called. Probably won't solve it, but give it a try..