IT story

활동이 시작될 때 소프트 키보드를 숨기는 방법

hot-time 2020. 6. 17. 07:48
반응형

활동이 시작될 때 소프트 키보드를 숨기는 방법


android:windowSoftInputMode="stateVisible"매니페스트에 편집 텍스트가 있습니다. 이제 활동을 시작할 때 키보드가 표시됩니다. 숨기는 방법? android:windowSoftInputMode="stateHidden키보드가 표시되면 앱을 최소화하고 다시 시작하여 키보드가 표시되므로 사용할 수 없습니다 . 나는 함께 노력했다

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

그러나 작동하지 않았습니다.


키보드를 표시하거나 숨기려면 다음 기능을 사용하십시오.

/**
 * Hides the soft keyboard
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Shows the soft keyboard
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}

에서 AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName"
          android:windowSoftInputMode="stateHidden"  />

또는 시도

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

확인하시기 바랍니다


editText의 상위 뷰에 두 개의 속성을 추가하십시오.

android:focusable="true"
android:focusableInTouchMode="true"

이것을 액티비티 태그 안의 매니페스트에 넣으십시오.

  android:windowSoftInputMode="stateHidden"  

이 시도:

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
>

자세한 내용 이것을 보십시오 .


새로운 활동 시작 또는시의 softkeyboard 숨기려면 onCreate(), onStart()당신이 아래의 코드를 사용할 수있는 등 :

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

AndroidManifest.xml 사용

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

자바 사용하기

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

위의 솔루션 키보드를 사용하여 activiy가 생성 될 때 포커스를 얻지 못하고 편집 텍스트를 숨기고 다음을 사용하여 터치하면 텍스트를 가져옵니다.

EditText에 추가하십시오

<EditText
android:focusable="false" />

EditText의 리스너를 추가하십시오.

youredittext.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    v.setFocusable(true);
    v.setFocusableInTouchMode(true);
    return false;
}});

xml 파일에 다음 텍스트를 추가하십시오.

<!--Dummy layout that gain focus -->
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="vertical" >
            </LinearLayout>

나는 이것이 효과가 있기를 희망하며 많은 방법을 시도했지만 이것은 나를 위해 일했다 fragments. 이 줄을 onCreateview / init에 넣으십시오.

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

New Activity start 또는 onCreate (), onStart () 메소드 등에서 소프트 키보드를 숨기려면 아래 코드를 사용하십시오.

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

버튼 클릭시 소프트 키보드를 숨기려면 클릭하십시오.

View view = this.getCurrentFocus();

    if (view != null) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

이 코드를 자바 파일로 만들고 object에 대한 인수를 edittext에 전달하십시오.

private void setHideSoftKeyboard(EditText editText){
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

AndroidManifest.xml에서 구성을 설정할 수 있습니다

예:

<activity
    android:name="Activity"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@*android:style/Theme.NoTitleBar"
    android:launchMode="singleTop"
    android:windowSoftInputMode="stateHidden"/>

활동을 시작할 때 처음으로 소프트 키보드를 숨기려면 다음 코드를 사용하십시오.

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

이것도 시도

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);

Ed_Cat_Search.setInputType(InputType.TYPE_NULL);

Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
        Ed_Cat_Search.onTouchEvent(event); // call native handler
        return true; // consume touch even
    }
});

Use SOFT_INPUT_STATE_ALWAYS_HIDDEN instead of SOFT_INPUT_STATE_HIDDEN

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

This is what I did:

yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
        yourEditText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);   // handle the event first
                InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {

                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard
                    yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
                }
                return true;
            }
        });

Try this.

First in your searchable xml the fields (name and hint etc) put @string and not literal strings.

Then method onCreateOptionsMenu, it must have a ComponentName object with your package name and your completed class name (with package name) - In case activity which has the SearchView component is the same as the show search results use getComponentName(), as the google android developer says.

I tried a lot of solutions and after much,much work this solution works for me.


If your application is targeting Android API Level 21 or more than there is a default method available.

editTextObj.setShowSoftInputOnFocus(false);

Make sure you have set below code in EditText XML tag.

<EditText  
    ....
    android:enabled="true"
    android:focusable="true" />

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

it will works


add in your activity in manifasts this property

android:windowSoftInputMode="stateHidden" 

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);

Ed_Cat_Search.setInputType(InputType.TYPE_NULL);

Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
        Ed_Cat_Search.onTouchEvent(event); // call native handler
        return true; // consume touch even
    }
});

this one worked for me

참고URL : https://stackoverflow.com/questions/18977187/how-to-hide-soft-keyboard-when-activity-starts

반응형