Programming

EditText와 함께 AlertDialog.Builder를 사용할 때 소프트 키보드가 나타나지 않습니다.

procodes 2020. 7. 26. 13:21
반응형

EditText와 함께 AlertDialog.Builder를 사용할 때 소프트 키보드가 나타나지 않습니다.


EditText를 입력 방법으로 사용하여 입력 상자를 만들기 위해 AlertDialog.Builder사용 하고 있습니다.

불행히도 EditText 가 초점을 맞추고 있지만 명시 적으로 다시 터치 하지 않으면 Soft Keyboard가 나타나지 않습니다 .

강제로 팝업하는 방법이 있습니까?

(AlertDialog.Builder) .show () 후에 다음을 시도했습니다 . 그러나 아무 소용이 없습니다.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

누구든지 도울 수 있습니까?

감사!!


나는 그런 것을 만들었다

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();

나는 이것을 다음과 같이 해결했다.

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

동일한 코드가 태블릿에서 제대로 작동하고 키보드가 팝업되지만 전화에서는 더 이상 조사하지 않는 것이 "조정"옵션을 가리키는 것으로 나타났습니다.

나는 이것을 사용하고 있으며 훨씬 더 깨끗합니다.

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();

필자의 경우 대화 상자가 표시되었을 때 키보드를 표시 할 수있는 유일한 방법은 다음을 추가하는 것입니다 DialogFragment.

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

메모 SOFT_INPUT_STATE_ALWAYS_VISIBLE 대신 SOFT_INPUT_STATE_VISIBLE을 .

설명서에서 :

int SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode: please always make the soft input area visible when this window receives input focus.

showCreate를 호출하여 onCreateDialog에서 AlertDialog를 사용하여 만든 대화 상자를 표시 할 때

여기에 코드를 넣어야합니다

    @Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });

}

훨씬 더 나은 솔루션이 여기 에 제공 됩니다 .

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

해결 방법이 없습니다. EditText예상대로 동작합니다.


Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

이것은 이미 여기 에 답변 되었습니다 . OnFocusChangeListener를 사용하면 나에게 도움이되었습니다.


이것을 시도해보십시오.

소프트 키보드를 표시하려는 경우 :

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

그리고 그것을 숨기고 싶다면 :

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

내가 정답을 얻기위한 여정을 시작했다고 상상할 수 있습니까?

먼저 InputMethodManager.showSoftInput은 SHOW_FORCED 플래그를 사용하지 않습니다. 0과 SHOW_IMPLICIT 만 사용합니다.

솔루션 1

여기서 우리는 안드로이드에 문제가 있다고 이해했다. 불완전한 초기화로 인해 Android 함수가 작동하지 않는 경우가 많습니다. 미친 소리로 들리지만 안드로이드 객체는 정상적으로 작동하지 않는다고 생각합니다. 일반 객체는 생성시 완전히 초기화되지만, Android 객체는 onCreate, onStart, onThis, onThat 함수 호출을 거쳐야합니다.

Android는이를 알고 있으므로 각 View에는 post 및 postDelayed 기능이 있습니다. 뷰의 메시지 큐에서 어떤 포스트 명령을 처리해야합니까?

다음은 내 프로젝트의 실제 예입니다 ....

src / jav / android / Util.java

package jav.android;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.view.*;
import android.view.inputmethod.*;


public class Util 
{   
    public static void showKeyboard(final Context ctx,final View view) 
    {
        Runnable r = new Runnable()
        {
            @Override public void run()
            {
                if(view.requestFocus()) 
                {
                 InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        };

        view.post(r);
    }

    // Note that we dont need to use View.post() here
    public static void hideKeyboard(Context ctx,View view) 
    {
     InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}

솔루션 2

  1. Add to xml as EditText content. This will ensure that EditText has the focus at the get go. So you wont have to request it programatically and thus no need to use View.post(Runnable).

    <...Layout>

  2. Use InputMethodManager to show the keyboard.

    InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

    SUMMARY

My above answers answered your specific question. However, you may notice that keyboard pops up initially, but upon Activity minimize and popup back, the EditText has focus but the keyboard is missing. Another problem I had was when I press back, I wanted the keyboard to go missing forever, until user presses text box again.

참고URL : https://stackoverflow.com/questions/3455235/when-using-alertdialog-builder-with-edittext-the-soft-keyboard-doesnt-pop

반응형