gpt4 book ai didi

java - 弹出键盘时更改边距

转载 作者:太空狗 更新时间:2023-10-29 15:05:54 25 4
gpt4 key购买 nike

我得到下面的代码是我的 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>

This is the code above This happens when keyboard comes up

我想要的是当键盘弹出时,删除或减少边距,以便所有内容都在屏幕的上半部分保持可见(因为键盘使用下半部分。当键盘消失时,我希望一切恢复正常。

现在挣扎了 2 个小时,所以不会发布我尝试过的所有内容...我在 list 中得到了 android:windowSoftInputMode="adjustResize"

现在会发生什么:当键盘出现时,它也会把其余部分也放上去(所以下半部分在上半部分,上半部分在屏幕外面)。但我想减少边距,所以一切都保持在上半部分。这可能吗?

你能帮我实现这个目标吗?

最佳答案

使用此自定义相对布局来检测软键盘。基于键盘调整布局参数(边距)。

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

}

关于java - 弹出键盘时更改边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com