Programing

Android에서 "가상 키보드 표시 / 숨기기"이벤트를 캡처하는 방법은 무엇입니까?

lottogame 2020. 4. 26. 21:02
반응형

Android에서 "가상 키보드 표시 / 숨기기"이벤트를 캡처하는 방법은 무엇입니까?


가상 키보드의 표시 여부에 따라 레이아웃을 변경하고 싶습니다. API와 다양한 블로그를 검색했지만 유용한 것을 찾을 수 없습니다.

가능합니까?

감사!


노트

이 솔루션은 소프트 키보드에서는 작동 onConfigurationChanged하지 않으며 소프트 (가상) 키보드에서는 호출되지 않습니다.


구성 변경을 직접 처리해야합니다.

http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

견본:

// from the link above
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

그런 다음 일부보기의 가시성을 변경하고 필드를 업데이트 한 다음 레이아웃 파일을 변경하십시오.


이것이 가장 효과적인 해결책은 아닙니다. 그러나 이것은 매번 나를 위해 일했습니다 ... 나는 softKeyboard를 들어야 할 때 마다이 기능을 호출합니다.

boolean isOpened = false;

public void setListenerToRootView() {
    final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
                Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboardup", 0).show();

                if (isOpened == false) {
                    //Do two things, make the view top visible and the editText smaller
                }
                isOpened = true;
            } else if (isOpened == true) {
                Toast.makeText(getApplicationContext(), "softkeyborad Down!!!", 0).show();
                isOpened = false;
            }
        }
    });
}

참고 : 사용자가 플로팅 키보드를 사용하는 경우이 방법으로 문제가 발생합니다.


활동에서 IMM (가상) 키보드 창 표시 / 숨기기를 처리하려면 레이아웃을 서브 클래 싱하고 onMesure 메서드를 재정의해야합니다 (측정 된 너비와 측정 된 높이의 레이아웃을 결정할 수 있도록). 그런 다음 setContentView ()에 의해 하위 클래스 레이아웃을 활동의 기본보기로 설정하십시오. 이제 IMM 표시 / 숨기기 창 이벤트를 처리 할 수 ​​있습니다. 이것이 복잡하게 들린다면, 실제로는 그렇지 않습니다. 코드는 다음과 같습니다.

main.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
        <EditText
             android:id="@+id/SearchText" 
             android:text="" 
             android:inputType="text"
             android:layout_width="fill_parent"
             android:layout_height="34dip"
             android:singleLine="True"
             />
        <Button
             android:id="@+id/Search" 
             android:layout_width="60dip"
             android:layout_height="34dip"
             android:gravity = "center"
             />
    </LinearLayout>

이제 Activity (라이브러리)에서 레이아웃의 서브 클래스 선언

    public class MainSearchLayout extends LinearLayout {

    public MainSearchLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main, this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("Search Layout", "Handling Keyboard Window shown");

        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();

        if (actualHeight > proposedheight){
            // Keyboard is shown

        } else {
            // Keyboard is hidden
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

코드에서 서브 클래스 생성자에서 Activity의 레이아웃을 팽창시키는 것을 볼 수 있습니다.

inflater.inflate(R.layout.main, this);

이제 Activity에 대한 서브 클래스 레이아웃의 컨텐츠 뷰를 설정했습니다.

public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MainSearchLayout searchLayout = new MainSearchLayout(this, null);

        setContentView(searchLayout);
    }

    // rest of the Activity code and subclassed layout...

}

나는 이렇게했다 :

OnKeyboardVisibilityListener인터페이스를 추가하십시오 .

public interface OnKeyboardVisibilityListener {
    void onVisibilityChanged(boolean visible);
}

HomeActivity.java :

public class HomeActivity extends Activity implements OnKeyboardVisibilityListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    // Other stuff...
    setKeyboardVisibilityListener(this);
}

private void setKeyboardVisibilityListener(final OnKeyboardVisibilityListener onKeyboardVisibilityListener) {
    final View parentView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
    parentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        private boolean alreadyOpen;
        private final int defaultKeyboardHeightDP = 100;
        private final int EstimatedKeyboardDP = defaultKeyboardHeightDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
        private final Rect rect = new Rect();

        @Override
        public void onGlobalLayout() {
            int estimatedKeyboardHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, parentView.getResources().getDisplayMetrics());
            parentView.getWindowVisibleDisplayFrame(rect);
            int heightDiff = parentView.getRootView().getHeight() - (rect.bottom - rect.top);
            boolean isShown = heightDiff >= estimatedKeyboardHeight;

            if (isShown == alreadyOpen) {
                Log.i("Keyboard state", "Ignoring global layout change...");
                return;
            }
            alreadyOpen = isShown;
            onKeyboardVisibilityListener.onVisibilityChanged(isShown);
        }
    });
}


@Override
public void onVisibilityChanged(boolean visible) {
    Toast.makeText(HomeActivity.this, visible ? "Keyboard is active" : "Keyboard is Inactive", Toast.LENGTH_SHORT).show();
  }
}

이것이 도움이되기를 바랍니다.


Nebojsa Tomcic의 코드를 기반으로 다음 RelativeLayout-Subclass를 개발했습니다.

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class KeyboardDetectorRelativeLayout extends RelativeLayout {

    public interface IKeyboardChanged {
        void onKeyboardShown();
        void onKeyboardHidden();
    }

    private ArrayList<IKeyboardChanged> keyboardListener = new ArrayList<IKeyboardChanged>();

    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public KeyboardDetectorRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KeyboardDetectorRelativeLayout(Context context) {
        super(context);
    }

    public void addKeyboardStateChangedListener(IKeyboardChanged listener) {
        keyboardListener.add(listener);
    }

    public void removeKeyboardStateChangedListener(IKeyboardChanged listener) {
        keyboardListener.remove(listener);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();

        if (actualHeight > proposedheight) {
            notifyKeyboardShown();
        } else if (actualHeight < proposedheight) {
            notifyKeyboardHidden();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private void notifyKeyboardHidden() {
        for (IKeyboardChanged listener : keyboardListener) {
            listener.onKeyboardHidden();
        }
    }

    private void notifyKeyboardShown() {
        for (IKeyboardChanged listener : keyboardListener) {
            listener.onKeyboardShown();
        }
    }

}

이것은 잘 작동합니다 ... Mark,이 솔루션은 활동의 소프트 입력 모드가 "WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE"로 설정된 경우에만 작동합니다.


@amalBit의 답변과 같이 리스너를 전역 레이아웃에 등록하고 dectorView의 보이는 아래쪽과 제안 된 아래쪽의 차이를 계산하십시오. 차이가 일부 값보다 큰 경우 (IME 높이)를 추측하면 IME가 증가했다고 생각합니다.

    final EditText edit = (EditText) findViewById(R.id.edittext);
    edit.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (keyboardShown(edit.getRootView())) {
                Log.d("keyboard", "keyboard UP");
            } else {
                Log.d("keyboard", "keyboard Down");
            }
        }
    });

private boolean keyboardShown(View rootView) {

    final int softKeyboardHeight = 100;
    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    int heightDiff = rootView.getBottom() - r.bottom;
    return heightDiff > softKeyboardHeight * dm.density;
}

높이 임계치 (100)는 추측 된 IME의 최소 높이이다.

이것은 adjustPan과 adjustResize 모두에 적용됩니다.


Nebojsa의 솔루션은 거의 나를 위해 일했습니다. 여러 줄의 EditText 내부를 클릭하면 키보드가 표시되는 것을 알았지 만 EditText 내부에서 입력을 시작했을 때 actualHeight와 proposalHeight는 여전히 동일하므로 키보드가 여전히 표시되는지 알 수 없었습니다. 최대 높이를 저장하도록 약간 수정했으며 정상적으로 작동합니다. 다음은 수정 된 하위 클래스입니다.

public class CheckinLayout extends RelativeLayout {

    private int largestHeight;

    public CheckinLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.checkin, this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
        largestHeight = Math.max(largestHeight, getHeight());

        if (largestHeight > proposedheight)
            // Keyboard is shown
        else
            // Keyboard is hidden

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

내 사용자 정의 EditText에서 onKeyPreIme (int keyCode, KeyEvent event)를 재정 의하여이 문제를 해결합니다.

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        //keyboard will be hidden
    }
}

누군가가 이것을 게시했는지 확실하지 않습니다. 발견 이 솔루션 간단한 사용에! . SoftKeyboard 클래스는 gist.github.com에 있습니다 . 그러나 키보드 팝업 / 숨기기 이벤트 콜백 동안 UI에서 올바르게 작업하려면 핸들러가 필요합니다.

/*
Somewhere else in your code
*/
RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use your root layout
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);

/*
Instantiate and pass a callback
*/
SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{

    @Override
    public void onSoftKeyboardHide() 
    {
        // Code here
        new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    // Code here will run in UI thread
                    ...
                }
            });
    }

    @Override
    public void onSoftKeyboardShow() 
    {
        // Code here
        new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    // Code here will run in UI thread
                    ...
                }
            });

    }   
});

나는 이것을하기위한 일종의 해킹을 가지고있다. 소프트 키보드가 표시하거나 숨길 때 감지하는 방법이있을 것 같지 않지만, 당신은 할 수 있습니다 이 경우 실제로 감지 에 대한 을 설정하여 표시하거나 숨길 수 OnFocusChangeListener를 ON EditText에 당신이있는 거 듣기.

EditText et = (EditText) findViewById(R.id.et);
et.setOnFocusChangeListener(new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View view, boolean hasFocus)
        {
            //hasFocus tells us whether soft keyboard is about to show
        }
    });

참고 : 이 핵에 대해 알아야 할 사항은이 콜백이 EditText포커스를 얻거나 잃을 때 즉시 시작된다는 것입니다. 이것은 실제로 소프트 키보드가 나타나거나 숨기 직전에 시작 됩니다. 키보드 가 보이 거나 숨은 후에 무언가 하는 가장 좋은 방법 은 a를 사용하고 Handler~ 400ms 정도 지연시키는 것입니다.

EditText et = (EditText) findViewById(R.id.et);
et.setOnFocusChangeListener(new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View view, boolean hasFocus)
        {
            new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        //do work here
                    }
                }, 400);
        }
    });

샌더, 나는 당신이 소프트 키보드에 의해 차단 된보기를 표시하려고한다고 생각합니다. http://android-developers.blogspot.com/2009/04/updating-applications-for-on-screen.html을 사용해보십시오 .


한 줄짜리 텍스트 뷰 백 코딩의 문제를 해결했습니다.

package com.helpingdoc;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class MainSearchLayout extends LinearLayout {
    int hieght = 0;
    public MainSearchLayout(Context context, AttributeSet attributeSet) {

        super(context, attributeSet);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main, this);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("Search Layout", "Handling Keyboard Window shown");
       if(getHeight()>hieght){
           hieght = getHeight();
       }
        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();
        System.out.println("....hieght = "+ hieght);
        System.out.println("....actualhieght = "+ actualHeight);
        System.out.println("....proposedheight = "+ proposedheight);
        if (actualHeight > proposedheight){
            // Keyboard is shown


        } else if(actualHeight<proposedheight){
            // Keyboard is hidden

        }

        if(proposedheight == hieght){
             // Keyboard is hidden
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

첫 번째 DecorView의 하위 하단 패딩을 확인할 수도 있습니다. 키보드가 표시되면 0이 아닌 값으로 설정됩니다.

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    View view = getRootView();
    if (view != null && (view = ((ViewGroup) view).getChildAt(0)) != null) {
        setKeyboardVisible(view.getPaddingBottom() > 0);
    }
    super.onLayout(changed, left, top, right, bottom);
}

Hide | OnGlobalLayoutListener에서 간단한 해킹을 통해 키보드의 Show 이벤트를들을 수 있습니다.

 final View activityRootView = findViewById(R.id.top_root);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

                if (heightDiff > 100) {
                    // keyboard is up
                } else {
                    // keyboard is down
                }
            }
        });

여기 activityRootView는 활동의 루트보기입니다.


Nebojsa Tomcic의 답변은 나에게 도움이되지 않았습니다. 나는이 RelativeLayout함께 TextView하고 AutoCompleteTextView그 안에. TextView키보드가 표시되고 숨겨져있을 때 아래쪽 으로 스크롤해야합니다 . 이것을 달성하기 위해 나는 onLayout방법을 무시 하고 그것은 나에게 잘 작동합니다.

public class ExtendedLayout extends RelativeLayout
{
    public ExtendedLayout(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.main, this);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);

        if (changed)
        {
            int scrollEnd = (textView.getLineCount() - textView.getHeight() /
                textView.getLineHeight()) * textView.getLineHeight();
            textView.scrollTo(0, scrollEnd);
        }
    }
}

참고 URL : https://stackoverflow.com/questions/4312319/how-to-capture-the-virtual-keyboard-show-hide-event-in-android

반응형