Android软键盘弹出

一、先看在沉浸式模式中,什么都不处理的。只是添加2个EditText

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:hint="请输入内容"/>

    <EditText
        android:id="@+id/input_edit_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入地址"/>
        <activity
            android:name=".InputManagerActivity"
            android:label="@string/title_activity_input_manager"
            android:windowSoftInputMode="adjustPan"
            android:theme="@style/AppTheme">
        </activity>

显示效果如下:
这里写图片描述

RE:上面这种方式,如果有多个输入框,下面的就没法显示,必须隐藏输入框,然后再点击。

2.根据代码判断是否弹出软键盘,移动界面的位置。

public class InputManagerActivity extends AppCompatActivity {

    private LinearLayout content_input_manager;

    EditText input_edit_two;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input_manager);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        content_input_manager = (LinearLayout) findViewById(R.id.content_input_manager);
        input_edit_two = (EditText) findViewById(R.id.input_edit_two);
        addLayoutListener(content_input_manager, input_edit_two);
    }

    //这种方式是通过代码,直接移动布局。移动的高度也位置,根据你传入的View(比如当前的是input_edit_two)确定
    private void addLayoutListener(final View view, final View scroll) {
        view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                //获取content_input_manager在窗口的可视区域
                view.getWindowVisibleDisplayFrame(rect);
                //view.getRootView().getHeight()为总高度(不变),rect.bottom为当前可视区域的底部(会发生变化)
                //如果没有软键盘弹出,view.getRootView().getHeight() 和 rect.bottom是相等的
                int inVisableHeight = view.getRootView().getHeight() - rect.bottom;
                if (inVisableHeight > 100) {
                    int[] location = new int[2];
                    scroll.getLocationInWindow(location);//获取input_edit_two的窗体坐标
                    int scrollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
                    view.scrollTo(0, scrollHeight);//这个最终目的是让input_edit_two的底部和软键盘的顶部对齐。也就是input_edit_two显示在可视区域的底部
                } else {
                    view.scrollTo(0, 0);
                }
            }
        });
    }

}

效果图如下:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值