当用户转到该特定活动时,我想自动显示具有相同设备键盘高度的列表视图。为此,我调用了三个方法,showKeyboard(),getKeyboardHeight()然后hideKeyboard(),然后给listview提供高度并显示listview。但问题是,一旦我调用showKeyboard(),计算高度然后hideKeyboard(),键盘就不会隐藏并保持可见。此外,我的高度为0.我无法显示该列表。在下面的代码中是否还有其他进程可以获得键盘高度或任何校正?请参阅以下代码:
showKeyboard()方法 -
private void showKeyboard() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editChatBox, 0);
}
getKeyboardHeight()方法 -
public int getKeyboardHeight() {
final View rootview = this.getWindow().getDecorView();
linearChatLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
rootview.getWindowVisibleDisplayFrame(r);
int screenHeight = rootview.getRootView().getHeight();
int newHeight = screenHeight - (r.bottom - r.top);
if (newHeight > heightOfKeyboard) {
heightOfKeyboard = screenHeight
- (r.bottom - r.top);
// heightOfKeyboard = heightDiff;
}
Log.d("Keyboard Size", "Size: " + heightOfKeyboard);
}
});
return heightOfKeyboard;
}
hideKeyboard()方法 -
private void hidekeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editChatBox.getWindowToken(), 0);
}
在onCreate()方法中 -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_new_layout);
ArrayAdapter<String> chatQueAdapter = new ArrayAdapter<>(this,
R.layout.chat_que_row, R.id.textChatQue, queArrays);
myListView.setAdapter(chatQueAdapter);
showKeyboard();
heightOfKeyboard = getKeyboardHeight();
hidekeyBoard();
myListView.getLayoutParams().height = heightOfKeyboard;
myListView.setVisibility(View.VISIBLE);
}
答案 0 :(得分:3)
我在根视图上添加了全局布局,并且我能够将列表视图的大小调整为与键盘相同的大小。
Pink Box是调整大小的ListView。我在2台三星设备上测试了这个示例应用程序,它在两者上运行良好。
布局文件
<?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="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="text"
android:maxLines="1" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:visibility="gone" />
</LinearLayout>
活动类
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.fet.minebeta.R;
public class ListActivity extends AppCompatActivity {
PagerAdapter adapterViewPager;
ViewPager viewPager;
private int heightDiff;
private ListView myListView;
private EditText editChatBox;
private boolean wasOpened;
private final int DefaultKeyboardDP = 100;
// Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
myListView = (ListView) findViewById(R.id.list);
editChatBox = (EditText) findViewById(R.id.edt);
//Listen for keyboard height change
setKeyboardListener();
// InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// editChatBox.requestFocus();
// inputMethodManager.showSoftInput(editChatBox, 0);
//
// if (getCurrentFocus() != null) {
// inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
// inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// }
//
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
// );
}
public final void setKeyboardListener() {
final View activityRootView = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rect r = new Rect();
@Override
public void onGlobalLayout() {
// Convert the dp to pixels.
int estimatedKeyboardHeight = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());
// Conclude whether the keyboard is shown or not.
activityRootView.getWindowVisibleDisplayFrame(r);
heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
boolean isShown = heightDiff >= estimatedKeyboardHeight;
if (isShown == wasOpened) {
Log.d("Keyboard state", "Ignoring global layout change...");
return;
}
wasOpened = isShown;
if (isShown) {
//Set listview height
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = heightDiff;
myListView.setLayoutParams(params);
myListView.requestLayout();
myListView.setVisibility(View.VISIBLE);
Toast.makeText(ListActivity.this, "KeyBoard Open with height " + heightDiff +
"\n List View Height " + myListView.getHeight(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
再次,这只是一个快速演示,您可以根据需要增强功能。
答案 1 :(得分:1)
据我所知,没有完美的解决方案可以随时使用。我见过很多人使用ViewTreeObserver
来获得键盘高度,虽然这有时不起作用,但总比没有好。 Try it out
几年前我写了一个输入法编辑器。我记得,没有从IME类中暴露出名为“getCurrentHeight”的函数。所以不要这样走。
请注意,现在很多IME支持在运行时更改键盘高度(当它显示在屏幕上时),也许你必须考虑到这一点。
答案 2 :(得分:1)
使用OnGlobalLayoutListener获取键盘高度或实现上面的代码段
chatRootLayout是您的xml根布局 将此rootLayout作为checkKeyboardHeight
中的parentLayout参数传递 private void checkKeyboardHeight(final View parentLayout)
{
chatRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
chatRootLayout.getWindowVisibleDisplayFrame(r);
int screenHeight = chatRootLayout.getRootView().getHeight();
int keyboardHeight = screenHeight - (r.bottom);
if (previousHeightDiffrence - keyboardHeight > 50)
{
// Do some stuff here
}
previousHeightDiffrence = keyboardHeight;
if (keyboardHeight> 100)
{
isKeyBoardVisible = true;
changeKeyboardHeight(keyboardHeight);
}
else
{
isKeyBoardVisible = false;
}
}
});
}
keyboardHeight()方法: -
private void changeKeyboardHeight(int height)
{
if (height > 100)
{
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, keyboardHeight);
yourLayout.setLayoutParams(params);
}
}
答案 3 :(得分:0)
请检查此KeyBoard,此应用已使用RelativeLayout onSize更改了https://github.com/w446108264/XhsEmoticonsKeyboard
答案 4 :(得分:0)
我已经通过使用自定义传递计算出高度然后计算它
private int keyboardHeight;
// passed here 230dp
final float popUpheight = getResources().getDimension(
R.dimen.keyboard_height);
changeKeyboardHeight((int) popUpheight);
// call this where you want height of keyboard
checkKeyboardHeight();
int previousHeightDiffrence = 0;
private void checkKeyboardHeight() {
rootlayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
rootlayout.getWindowVisibleDisplayFrame(r);
int screenHeight = rootlayout.getRootView()
.getHeight();
int heightDifference = screenHeight - (r.bottom);
if (Math.abs(previousHeightDiffrence - heightDifference) > 50) {
popupWindow.dismiss();
imageEmoji
.setImageResource(R.drawable.emoji_btn_normal);
}
previousHeightDiffrence = heightDifference;
if (heightDifference > 100) {
isKeyBoardVisible = true;
changeKeyboardHeight(heightDifference);
} else {
isKeyBoardVisible = false;
}
}
});
}
/**
* change height of emoticons keyboard according to height of actual
* keyboard
*
* @param height minimum height by which we can make sure actual keyboard is
* open or not
*/
private void changeKeyboardHeight(int height) {
if (height > 100) {
keyboardHeight = height;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, keyboardHeight);
emoticonsCover.setLayoutParams(params);
}
}
了解详情here, 希望这能帮到你