gpt4 book ai didi

android - 在android中创建 Activity 时如何获取键盘高度

转载 作者:可可西里 更新时间:2023-11-01 19:07:12 28 4
gpt4 key购买 nike

我想在用户进行特定 Activity 时自动显示与设备键盘高度相同的 ListView 。为此,我调用了三个方法,即 showKeyboard()、getKeyboardHeight() 和 hideKeyboard(),然后为该 ListView 提供高度并显示该 ListView 。但问题是一旦我调用 showKeyboard(),计算高度然后 hideKeyboard(),键盘不会隐藏并保持可见。此外,我得到的高度为 0。我无法显示该 listView。是否有任何其他过程来获取键盘高度或以下代码中的任何更正?请参阅下面的代码:

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);
}

最佳答案

我在 Root View 上添加了全局布局,并且能够将 ListView 的大小调整为与键盘相同的大小。

粉色框是调整大小的 ListView 。我已经在 2 台三星设备上测试了这个示例应用程序,它在两台设备上都运行良好。

enter image description here

布局文件

<?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>

Activity 类

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();

}
}
});
}
}

同样,这只是快速演示,您可以根据需要进行增强。

关于android - 在android中创建 Activity 时如何获取键盘高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35913458/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com