我正在使用此庫:https://github.com/yshrsmz/KeyboardVisibilityEvent來檢測鍵盤何時打開或關閉,並且這依賴於輸入到Android清單的android:windowSoftInputMode="adjustResize"
。Android AdjustResize未正確調整大小
該庫完美地檢測軟鍵盤的打開和關閉事件,但由於adjustResize參數,我的內容被推出視圖。
的Java:
KeyboardVisibilityEvent.setEventListener( AddActivity.this, new KeyboardVisibilityEventListener() { @Override public void onVisibilityChanged(boolean isOpen) { // some code depending on keyboard visiblity status if (noteEditText.isFocused()) { if (isOpen) { Log.d("KB", "Keyboard is open"); noteEditText.setLines(12); noteEditText.setCursorVisible(true); } else { Log.d("KB", "Keyboard is closed"); noteEditText.setLines(50); noteEditText.setCursorVisible(false); } } } }); noteEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Log.d("KB", "onFocusChange"); if (firstStart) { noteEditText.setLines(12); noteEditText.setCursorVisible(true); firstStart = false; } } });
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/add_record" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" android:windowSoftInputMode="stateHidden"> <EditText android:id="@+id/title_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/enter_title" android:inputType="textCapSentences" android:textColor="@color/fontPrimary" android:theme="@style/EditTextCustomCursor"> <requestFocus /> </EditText> <EditText android:id="@+id/note_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:ellipsize="end" android:gravity="top|left" android:hint="@string/enter_note" android:inputType="textCapSentences|textMultiLine" android:lines="50" android:maxLines="20" android:minLines="5" android:paddingLeft="5dp" android:scrollHorizontally="false" android:scrollbars="vertical" android:textColor="@color/fontPrimary" android:theme="@style/EditTextCustomCursor" />
因此,這通過調節第二EditText上的線是的話,我的偉大工程在鍵盤上方鍵入,但當我的時候在鍵盤上,滾動到該EditText的底部,然後單擊底部,將EditText光標放在我點擊的位置,但它將第一個EditText和Support ActionBar推出視圖,並在底部留下很大的空隙(可以可以在下面的圖像(圖像2)中看到,因爲在那裏選擇'F',也就是EditText的底部)。
我還曾試圖使用'adjustNothing'並執行以下操作,但這看起來不起作用,因爲EditText高度不會改變,直到行數改變並且麻木只有在知道鍵盤是否打開或關閉時纔會更改線條。
private void setupListeners() {
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View mainView = (LinearLayout) findViewById(R.id.add_record);
int heightDiff = mainView.getHeight() - noteEditText.getHeight();
Log.d("KB", "HeightDiff: " + heightDiff);
if (heightDiff > 1000 || keyboardShown) { // 99% of the time the height diff will be due to a keyboard.
Log.d("KB", "Keyboard is open");
if (isKeyboardVisible) {
noteEditText.setLines(12);
noteEditText.setCursorVisible(true);
noteEditText.requestLayout();
isKeyboardVisible = false;
}
} else {
Log.d("KB", "Keyboard is closed");
if (!isKeyboardVisible) {
noteEditText.setLines(50);
noteEditText.setCursorVisible(false);
noteEditText.requestLayout();
isKeyboardVisible = true;
}
}
}
});
noteEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
numTimesClicked++;
Log.d("KB", "onClick: " + numTimesClicked);
if (clicked) {
// Run function
Log.d("KB", "clicked");
InputMethodManager imm = (InputMethodManager) AddActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
Log.d("KB", "Software Keyboard was shown");
isKeyboardVisible = true;
keyboardShown = true;
} else {
Log.d("KB", "Software Keyboard was not shown");
isKeyboardVisible = false;
keyboardShown = false;
}
} else {
Log.d("KB", "scroll");
clicked = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
clicked = false;
}
}, 3 * 1000);
}
}
});
noteEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.d("KB", "closeKeyboard");
noteEditText.setLines(50);
noteEditText.setCursorVisible(false);
noteEditText.requestLayout();
isKeyboardVisible = false;
}
return false;
}
});
因此,我將如何去實現調整的EditText線(鍵盤打開和關閉時)的預期的效果,而不是推其他內容拿出來看?那麼支持ActionBar和第一個EditText總是保持在同一位置,並且只有第二個EditText在軟鍵盤出現時被調整?
感謝您對ScrollView的建議(因爲這部分解決了我的問題),但如果用戶點擊一行而不必滾動(請參閱下面的答案),仍然會導致最後一個問題。 –