简体   繁体   中英

Change margins when keyboard popup

I got this code below is my main.xml

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:paddingTop="10dp"
        android:text="@string/parameter"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner1"
        style="@layout/spinner_layout"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/spinner3"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="22dp"
        android:paddingLeft="5dp"
        tools:listitem="@android:layout/simple_list_item_1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignLeft="@+id/output2"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="320dp"
        android:layout_toRightOf="@+id/textView7" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView7"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:text="@string/transmission"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/EditText1"
        android:layout_alignRight="@+id/EditText1"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="70" />

    <EditText
        android:id="@+id/EditText1"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_above="@id/editText2"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="50dp"
        android:ems="10"
        android:hint="@string/uren"
        android:inputType="number|numberDecimal" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/spinner3"
        android:text="@string/out"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/output2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView7"
        android:layout_alignBottom="@+id/textView7"
        android:layout_alignLeft="@+id/EditText1"
        android:layout_alignParentRight="true"
        android:background="@drawable/box"
        android:paddingTop="10dp"
        android:text="@string/textview"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner2"
        style="@layout/spinner_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView1"
        android:layout_marginBottom="80dp"
        android:layout_marginLeft="20dp"
        android:layout_toLeftOf="@+id/EditText1"
        android:layout_toRightOf="@id/textView2" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/editText1"
        android:text="@string/in"
        android:textColor="@color/White"
        android:textSize="18sp" />

</RelativeLayout>

这是上面的代码当键盘出现时会发生这种情况

What I want is when the keyboard pops up, to remove or decrease the margins, so that everything will stay visible in the top half of the screen (since half below is used by keyboard. And when keyboard goes away, then I want everything back to normal.

Struggling for 2 hour now, so not going to post everything I tried... I got android:windowSoftInputMode="adjustResize" in the manifest.

What happens now: When keyboard comes up, it puts up the rest up too (so the bottom half is on top half, and top half is outside the screen). But I want to decrease the margins, so everything stays on top half. Is this possible?

And could you help me along to achieve this.

Use this custom relative layout to detect soft keyboard. Based on the key board adjust layout params (margins).

/**
 * RelativeLayout that can detect when the soft keyboard is shown and hidden.
 *  
 */

public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {

    public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing);
    }
    private Listener listener;
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if (listener != null) {
            listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
    }

    }

This is the code by which you can get the event of keyboard pop-up.

In this, you'll need to change the margins programmatically or you can set alternative layout when keyboard pops-up, which will be easy in this case. Changing layout will cause loss of values already entered. But, you can also save them in local variables and when you change layout, use those values upon change in layout to preset the entered values in older layout.

For your spinners and edit-texts to be functional, you might need to call onCreate again upon onConfigurationChanged .

PS:- This is just an idea, there might be other ways also to implement this, but you can also try this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM