Android隐藏,显示软键盘以及不自动弹出软键盘

隐藏软键盘

方法一:

> 在 AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden

   < activity android:name=".Main"
      android:label="@string/app_name"
      android:windowSoftInputMode="adjustUnspecified|stateHidden"
      android:configChanges="orientation|keyboardHidden">
  < intent-filter>
  < action android:name="android.intent.action.MAIN" />
  < category android:name="android.intent.category.LAUNCHER" />
  < /intent-filter>
  < /activity>

方法二:

让 EditText失去焦点,使用EditText的clearFocus方法

  例如:
    EditText edit=(EditText)findViewById(R.id.edit);
  edit.clearFocus();

方法三:

强制隐藏Android输入法窗口

   例如:((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
        .hideSoftInputFromWindow(
    RegisterActivity.this.getCurrentFocus().getWindowToken(), 
    InputMethodManager.HIDE_NOT_ALWAYS);

显示软键盘

//显示软键盘,控件ID可以是EditText,TextView

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0); 

不自动弹出键盘

带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法:

方法一:在mainfest文件中把对应的activity设置

android:windowSoftInputMode="stateHidden"
或者android:windowSoftInputMode="stateUnchanged"。

方法二:可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。

注意TextView不要设置Visiable=gone,否则会失效,可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。

<TextView

        android:id="@+id/text_notuse"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:focusable="true"

android:focusableInTouchMode="true"

        />
TextView textView = (TextView)findViewById(R.id.text_notuse);

            textView.requestFocus();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容