itsource

액티비티를 시작할 때 키보드가 표시되지 않도록 합니다.

mycopycode 2022. 7. 21. 23:40
반응형

액티비티를 시작할 때 키보드가 표시되지 않도록 합니다.

나는 한 가지 활동을 하고 있다.Edit Text액티비티가 초기화되면 Android 키보드가 표시됩니다.사용자가 입력에 초점을 맞출 때까지 키보드를 숨긴 채로 두려면 어떻게 해야 합니까?

다음과 같은 것이 효과가 있을 것 같습니다.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

전에도 이런 용도로 써본 적이 있어요.

시험해 보세요-

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

또,

  1. 매니페스트 파일의 액티비티에서 선언할 수도 있습니다.
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateHidden"
          >
  1. 이미 사용하고 있는 경우android:windowSoftInputMode같은 가치로adjustResize또는adjustPan, 다음의 2개의 값을 조합할 수 있습니다.
<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

이렇게 하면 키보드는 항상 숨겨지지만 키보드를 표시해야 할 경우 액티비티 뷰를 이동합니다.

테마를 사용하는 모든 활동에 대해 숨기기

<style name="MyTheme" parent="Theme">
    <item name="android:windowSoftInputMode">stateHidden</item>
</style>

주제를 정하다

<application android:theme="@style/MyTheme">

상위 레이아웃에 이러한 두 가지 특성(예: 선형 레이아웃, 상대 레이아웃)을 추가합니다.

android:focusable="false"
android:focusableInTouchMode="false" 

효과가 있습니다. :)

API 레벨 21을 사용하는 경우 editText.setShowSoft를 사용할 수 있습니다.Input On Focus(false);

매니페스트 파일에 선언하려고 합니다.

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

Android Manifest.xml에 추가만 하면 됩니다.

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

문제가 있는 .xml 레이아웃 파일의 직접 부모 레이아웃에 다음 코드 행을 쓸 수도 있습니다.

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: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>


편집:

EditText가 다른 레이아웃에 포함되어 있는 경우의 예:

<?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 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();
}

이것을 manifest.xml 파일에 추가합니다.

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

다 끝나셨습니다.

@Lucas가 수락한 답변을 확대하려면:

초기 라이프 사이클 이벤트 중 하나에서의 활동에서 이 이름을 붙입니다.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Kotlin의 예:

override fun onResume() {
  super.onResume()

  window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}

키보드를 숨기는 기능.

public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();

    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

AndroidManifext.xml 파일에서 키보드를 숨깁니다.

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">

이 set unique Attribute는 각 요소에 대해 사용할 수 있습니다.

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

요소가 포커스에 있는 동안에는 키보드가 표시되지 않습니다.

//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

액티비티에 다음 항목을 추가합니다.

@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);
}

활동 태그 내의 매니페스트에 이 코드(안드로이드: windowSoftInputMode="stateAlwaysHidden")를 선언합니다.

다음과 같습니다.

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

API 26과 Kotlin에서는 이 솔루션만이 유효했습니다.

   override fun onResume() {
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    super.onResume()
}

또는 xml에서 포커서블태그를 사용할 수 있습니다.

Android: displayable="false"

false로 설정합니다.여기 코드의 일부분이 있습니다.

언급URL : https://stackoverflow.com/questions/9732761/prevent-the-keyboard-from-displaying-on-activity-start

반응형