如何在android中创建活动时获得键盘的高度

Ruc*_*hir 14 keyboard android 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);
        }
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

hideKeyboard()方法 -

private void hidekeyBoard() {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editChatBox.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)

在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);
    }
Run Code Online (Sandbox Code Playgroud)

Hit*_*ahu 4

我在根视图上添加了全局布局,并且能够将列表视图的大小调整为与键盘相同的大小。

粉色框是调整大小的 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>
Run Code Online (Sandbox Code Playgroud)

活动课

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

                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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