Android开发的小技巧

2019-04-17  本文已影响0人  傲娇的狗蛋

前言

平时开发中经常遇到的小的问题,这里记录一下。

EditText禁止自动弹出软键盘的方法

在包含EditText的父布局中添加android:focusable="true"android:focusableInTouchMode="true"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:focusable="true"
     android:focusableInTouchMode="true"
   >
   <EditText
       android:id="@+id/edit"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:inputType="text"
       android:maxLines="1"
       />
</LinearLayout>

ScrollView默认位置不是最顶部解决方案

跟EditText一样,在父元素的属性这两行即可android:focusable="true"android:focusableInTouchMode="true"

进入页面让EditText自动弹出键盘

  /**
     * 打开软键盘
     *
     * @param view
     */
    public static void openKeybord(final View view) {
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                ((InputMethodManager) view.getContext().getSystemService(
                        Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);
            }
        },100);

    }

目前测试此方法在页面初始化调用不能自动打开键盘,但在点击事件中可以打开

上一篇 下一篇

猜你喜欢

热点阅读