Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止键盘在“活动开始”时显示_Java_Android_Focus_Android Keypad - Fatal编程技术网

Java 防止键盘在“活动开始”时显示

Java 防止键盘在“活动开始”时显示,java,android,focus,android-keypad,Java,Android,Focus,Android Keypad,我有一个带有编辑文本输入的活动。初始化活动时,将显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏状态?我认为以下方法可能有效 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 我以前用过这种东西。试试这个- this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWA

我有一个带有
编辑文本
输入的活动。初始化活动时,将显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏状态?

我认为以下方法可能有效

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
我以前用过这种东西。

试试这个-

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
或者

  • 您还可以在清单文件的活动中声明-
  • 
    
  • 如果您已经使用了
    android:windowSoftInputMode
    等值,如
    adjustResize
    adjustPan
    ,则可以组合两个值,如:
  • 
    

    这将在适当时隐藏键盘,但在必须显示键盘的情况下平移“活动”视图。

    为使用主题的所有活动隐藏它

    
    国家隐藏
    
    设定主题


    尝试在menifest文件中声明它

    <activity android:name=".HomeActivity"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateAlwaysHidden"
          >
    

    如果您使用的是API级别21,则可以使用editText.setShowSoftInputInfo(false)

    函数隐藏键盘

    在AndroidManifext.xml文件中隐藏键盘


    将这两个属性添加到父布局(例如:线性布局、相对布局)


    它将完成以下任务:)

    只需添加AndroidManifest.xml即可

    <activity android:name=".HomeActivity"  android:windowSoftInputMode="stateHidden">
    </activity>
    

    对我来说最好的解决方案,粘贴你的类

    @Override
    public void onResume() {
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onResume();
    }
    
    @Override
    public void onStart() {
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        super.onStart();
    }
    

    您还可以在出现“问题”的.xml布局文件的父布局中编写以下代码行:

    例如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        ...
        android:focusable="true"
        android:focusableInTouchMode="true" >
    
        <EditText
            android:id="@+id/myEditText"
            ...
            android:hint="@string/write_here" />
    
        <Button
            android:id="@+id/button_ok"
            ...
            android:text="@string/ok" />
    </LinearLayout>
    
    
    

    编辑:

    如果编辑文本包含在其他布局中,则示例:

    <?xml version="1.0" encoding="utf-8"?>
    <ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        ... >                                            <!--not here-->
    
        ...    <!--other elements-->
    
        <LinearLayout
            android:id="@+id/theDirectParent"
            ...
            android:focusable="true"
            android:focusableInTouchMode="true" >        <!--here-->
    
            <EditText
                android:id="@+id/myEditText"
                ...
                android:hint="@string/write_here" />
    
            <Button
                android:id="@+id/button_ok"
                ...
                android:text="@string/ok" />
        </LinearLayout>
    
    </ConstraintLayout>
    
    
    ...    
    
    关键是确保EditText不能直接聚焦

    再见!;-)

    只需在您的活动中添加以下内容:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
          if (getCurrentFocus() != null) {
               InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
          }
          return super.dispatchTouchEvent(ev);
    }
    

    要扩展@Lucas接受的答案:

    在早期生命周期事件之一中,从您的活动中调用此选项:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u HIDDEN)

    科特林示例:
    只需将其添加到manifest.xml文件中

    <activity android:name=".MainActivity"
                android:windowSoftInputMode="stateHidden">
    
    
    

    您都完成了。

    您可以尝试为每个元素设置此唯一属性

    TextView mtextView = findViewById(R.id.myTextView);
    mtextView.setShowSoftInputOnFocus(false);
    

    当元素为焦点时,键盘将不显示,或者您可以在xml中使用焦点标记

    android:focusable=“false”

    将其设置为false。

    声明此代码(android:windowSoftInputMode=“stateAllwayshidden”) 在活动标记内的清单中

    像这样:

    <activity android:name=".MainActivity"
      android:windowSoftInputMode="stateAlwaysHidden">
    
    
    
    在您的清单中
    可能重复了如何与
    android:WindowsOfInputMode=“adjustPan”
    一起使用?@János android:WindowsOfInputMode=“adjustPan | stateHidden”这条评论中的答案是我想要的答案:有没有办法将其设置为仅限数字的键盘?i、 e.12键键盘?@MohamedKhamis input.setRawInputType(配置键盘_12键);是的,它仍然有效@SagarNayak为什么要隐藏
    EditText
    ?:)这是在7年后包含
    EditText
    @Devealte的活动开始时隐藏键盘,它对我有效,您是否将其放在onCreate中?@Dymas是,几个月前,我刚刚修复了它:)就像这种全局方法一样。我在不同的地方使用了不同的主题,并用代码和xml来显示!事实上,这是最正确的答案!尤其是因为您可能花了3分钟编写这两种方法,所以您不是第一个;-)这对我不起作用,但是根据Jack t的回答,将它们设置为
    true
    会起作用。在最近的版本中是否有行为改变?除了我的答案之外,您还需要检查清单和编辑文本中的其他属性。我只是在其中有最基本的属性。我不明白为什么将这些设置为
    false
    会起作用,因为这样做的目的是让焦点远离编辑文本框。也许它过去是通过让焦点远离父布局来让焦点远离编辑文本框的?虽然这可能回答了作者的问题,但它缺少一些解释性的词语和/或指向文档的链接。如果没有一些短语,原始代码片段就没有多大帮助。你也会发现这很有帮助。请编辑你的答案-
    <?xml version="1.0" encoding="utf-8"?>
    <ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        ... >                                            <!--not here-->
    
        ...    <!--other elements-->
    
        <LinearLayout
            android:id="@+id/theDirectParent"
            ...
            android:focusable="true"
            android:focusableInTouchMode="true" >        <!--here-->
    
            <EditText
                android:id="@+id/myEditText"
                ...
                android:hint="@string/write_here" />
    
            <Button
                android:id="@+id/button_ok"
                ...
                android:text="@string/ok" />
        </LinearLayout>
    
    </ConstraintLayout>
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
          if (getCurrentFocus() != null) {
               InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
          }
          return super.dispatchTouchEvent(ev);
    }
    
    override fun onResume() {
      super.onResume()
    
      window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
    }
    
    <activity android:name=".MainActivity"
                android:windowSoftInputMode="stateHidden">
    
    TextView mtextView = findViewById(R.id.myTextView);
    mtextView.setShowSoftInputOnFocus(false);
    
    <activity android:name=".MainActivity"
      android:windowSoftInputMode="stateAlwaysHidden">