Android 在软键盘上方放置一个按钮
我有一个代码来检查软输入键盘是否可见。但我不知道如何将最初位于布局底部的按钮设置为键盘正上方。我已将布局设置为全屏,因此在清单中将软输入模式设置为调整大小将不再有效。我知道。现在我只设置按钮的“Y”位置(id=next)在键盘上方的某个值。但我希望它正好位于键盘上方。等待您的帮助!!提前感谢 Java代码:Android 在软键盘上方放置一个按钮,android,keyboard,Android,Keyboard,我有一个代码来检查软输入键盘是否可见。但我不知道如何将最初位于布局底部的按钮设置为键盘正上方。我已将布局设置为全屏,因此在清单中将软输入模式设置为调整大小将不再有效。我知道。现在我只设置按钮的“Y”位置(id=next)在键盘上方的某个值。但我希望它正好位于键盘上方。等待您的帮助!!提前感谢 Java代码: final Window mRootWindow=getActivity().getWindow(); frag1Binding.relative.getViewTreeOb
final Window mRootWindow=getActivity().getWindow();
frag1Binding.relative.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int screenheight=frag1Binding.relative.getRootView().getHeight();
Rect r=new Rect();
View view=mRootWindow.getDecorView();
view.getWindowVisibleDisplayFrame(r);
int keyboardheight=screenheight-r.bottom;
if (keyboardheight>150){
lastheight=keyboardheight;
Log.e("EditText_Bottom",Integer.toString(frag1Binding.phnmbr.getBottom()));
linearLayoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
orginalpositon=frag1Binding.next.getY();
Log.e("Original Y",Float.toString(frag1Binding.next.getY()));
frag1Binding.next.setY(100);
}
else {
if (lastheight>150){
frag1Binding.next.setY(orginalpositon);
}}
}
});
XML:
你的日子不好过
很简单,您不必编写java代码来处理这种行为。
使用ScrollView
包装您的内容,并将按钮放在您希望位于上方的ScrollView
之外
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<TextView
android:id="@+id/head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/ph_verify_lbl_mrgn"
android:text="verify_your_phone_number"
android:textColor="#bb2b67"
android:textSize="@dimen/ph_verify_head_tsxtsze" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/head"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="lorem_ipsum_lorem_ipsum" />
<TextView
android:id="@+id/countrycode"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_below="@+id/textView"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center|bottom"
android:text="+91"
android:textColor="#000000"
android:textSize="@dimen/ph_verify_head_tsxtsze" />
<EditText
android:id="@+id/phnmbr"
android:layout_width="312dp"
android:layout_height="60dp"
android:layout_below="@+id/textView"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@+id/countrycode"
android:background="@drawable/edittext_bg"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:freezesText="true"
android:gravity="start|center|bottom"
android:hint="Hint"
android:inputType="number"
android:paddingLeft="20dp" />
</RelativeLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="@drawable/button_bg"
android:text="NEXT"
android:textColor="#ffffff"
android:textSize="22sp">
</Button>
<ImageView
android:id="@+id/imageView"
android:layout_width="20dp"
android:layout_height="25dp"
android:layout_marginBottom="18dp"
android:layout_marginEnd="19dp"
android:layout_marginRight="19dp"
android:background="@drawable/arrow"
android:elevation="2dp" />
</LinearLayout>
</LinearLayout>
</layout>
在你的舱单课上
<application
...
>
<activity android:name=".YourActivity"
android:windowSoftInputMode="adjustResize"
>
</activity>
</application>
但是如果布局是全屏的,则adjustResize不起作用。上图有一个工具栏和一个状态栏。
<application
...
>
<activity android:name=".YourActivity"
android:windowSoftInputMode="adjustResize"
>
</activity>
</application>