很抱歉,公司上传不了图片,下面的代码直接拷贝到xml文件和.java文件里即可运行看效果。
方法一:不需要设置Activity的输入法模式,底部没有悬浮的按钮,核心思想是底部有一个view,平时是gone的,弹出软键盘之后,变为visible,这样输入框就有足够的空间上下滚动了。缺点:view的高度必须和软键盘的高度一致才完美,我这里简单设为100dp,其实是不完美的。推荐指数:1个星。
package com.sf.edittexttest;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et_test = (EditText) findViewById(R.id.et_test);
final TextView tv_bottom = (TextView) findViewById(R.id.tv_bottom);
et_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
et_test.getWindowVisibleDisplayFrame(r);
if (et_test.getRootView().getHeight() - (r.bottom - r.top) > 500) { // if more than 100 pixels, its probably a keyboard...
//键盘弹出了
tv_bottom.setVisibility(View.VISIBLE);
} else {
//键盘隐藏了
tv_bottom.setVisibility(View.GONE);
}
}
});
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_dark"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_dark">
<EditText
android:id="@+id/et_test"
android:layout_width="200dp"
android:layout_height="60dp"
android:background="@android:color/white"
android:layout_centerInParent="true"/>
</RelativeLayout>
<TextView
android:id="@+id/tv_bottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:visibility="gone"
android:background="@android:color/holo_blue_dark"/>
</LinearLayout>
</ScrollView>
方法二:不需要设置Activity的输入法模式,不需要代码,只需要配置xml文件,底部没有常驻悬浮按钮,自计算软键盘高度,完美。推荐指数:5个星。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_dark">
<EditText
android:id="@+id/et_test"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="@android:color/white" />
</RelativeLayout>
<TextView
android:id="@+id/tv_bottom"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_dark"
android:visibility="gone" />
</LinearLayout>
</ScrollView>
</FrameLayout>
方法三:不需要设置Activity的输入法模式,不需要代码,但是常驻底部的按钮会被顶上来。如果底部按钮需要被顶上来的话,推荐指数:5个星,不需要顶上来的话,推荐指数:0个星。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_dark">
<EditText
android:id="@+id/et_test"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="@android:color/white" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是常驻底部的按钮,不会随ScrollView滚动,但会被软键盘顶上来"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</FrameLayout>
方法四:不需要配置Acitivity的输入法模式,常驻底部的按钮不会被顶上来。推荐指数:5个星。
代码:
package com.sf.edittexttest;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et_test = (EditText) findViewById(R.id.et_test);
final RelativeLayout rl_bottom = (RelativeLayout) findViewById(R.id.rl_bottom);
final View view_bottom = findViewById(R.id.view_bottom);
et_test.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
et_test.getWindowVisibleDisplayFrame(r);
if (et_test.getRootView().getHeight() - (r.bottom - r.top) > 500) { // if more than 100 pixels, its probably a keyboard...
//键盘弹出了
rl_bottom.setVisibility(View.GONE);
view_bottom.setVisibility(View.GONE);
} else {
//键盘隐藏了
rl_bottom.setVisibility(View.VISIBLE);
view_bottom.setVisibility(View.VISIBLE);
}
}
});
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="@android:color/holo_blue_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@android:color/holo_green_dark">
<EditText
android:id="@+id/et_test"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:background="@android:color/white" />
</RelativeLayout>
<!--我是底部的占位控件,我的高度必须和常驻底部的按钮高度一致,这样在滚动到底部的时候,我上方的布局才能完整展示出来,软键盘弹出之后我会隐藏,通常我是透明的-->
<View
android:id="@+id/view_bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@android:color/transparent" />
</LinearLayout>
</ScrollView>
<!--这层布局占满屏幕,但是背景为透明,所以能触发ScrollView的滚动-->
<RelativeLayout
android:id="@+id/rl_bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<!--这层布局高度和底部按钮一样,或稍大,作为按钮的背景,也是常驻底部的,不随ScrollView而滚动-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@android:color/holo_blue_dark"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="我是常驻底部的按钮,不会随ScrollView滚动,软键盘弹出之后我会隐藏,也就是说不会被软键盘顶上来了" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
方法五:activity配置android:windowSoftInputMode=”adjustPan”,代码不再需要专门监听软键盘弹出来隐藏底部按钮,配置该属性之后,弹出软键盘自动会覆盖底部常驻按钮,只是滚动不到底部了,ScrollView的一部分也被软键盘遮住了,这时又回到方法一,必须设置底部view为软键盘的高度才完美,我这里没有去计算软键盘高度,推荐指数:1个星。
附上监听软键盘的方法,上面只用到了其中的一种,朋友们根据需要自取。
如果觉得以上方法麻烦,不妨在Activity onCreate的时候,添上这句话试试:
//解决软键盘弹出界面变型问题
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
这跟在清单文件配置的效果是一样的,结果都是不能完美把ScrollView的全部内容展示出来,其实和方法一差不多,都是要精确计算出软键盘的高度才能完美。