이 사용자 지정 상대 레이아웃을 사용하여 소프트 키보드를 감지합니다. 키보드 조정 레이아웃 매개 변수 (여백)를 기반으로합니다.
/**
* RelativeLayout that can detect when the soft keyboard is shown and hidden.
*
*/
public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {
public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
}
public interface Listener {
public void onSoftKeyboardShown(boolean isShowing);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int height = MeasureSpec.getSize(heightMeasureSpec);
Activity activity = (Activity)getContext();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
int diff = (screenHeight - statusBarHeight) - height;
if (listener != null) {
listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
-------------------이것은 당신이 키보드 팝업의 이벤트를 얻을 수있는 코드입니다.
여기에서 여백을 프로그래밍 방식으로 변경하거나 키보드 팝업시 대체 레이아웃을 설정할 수 있습니다.이 경우에는 쉽습니다. 레이아웃을 변경하면 이미 입력 한 값이 손실됩니다. 그러나 로컬 변수에 저장할 수도 있고 레이아웃을 변경할 때 레이아웃 변경시 해당 값을 사용하여 이전 레이아웃에 입력 된 값을 미리 설정할 수 있습니다.
스피너와 편집 텍스트가 작동하려면를 onCreate
다시 호출해야 할 수 있습니다 onConfigurationChanged
.
추신 :-이것은 단지 아이디어 일뿐입니다. 이것을 구현하는 다른 방법도있을 수 있지만 시도해 볼 수도 있습니다.
출처
https://stackoverflow.com/questions/22039788