如何在Android中获得当前的活动键盘?

如何在Android中获得当前的活动键盘?,android,keyboard,Android,Keyboard,如何在Android中获取当前活动的键盘视图 我试过了 KeyboardView keyboardView = new KeyboardView(context, null); Keyboard keyboard = keyboardView.getKeyboard(); 但它不断返回键盘为空 我尝试了这个方法来获取键盘高度。实际上,您无法获取当前键盘的参考。详细信息是(请记住阅读注释)。我尝试像这样获取Android虚拟键盘的高度[ 但我并没有得到高度——只有屏幕的高度 (我为Unity3d

如何在Android中获取当前活动的键盘视图

我试过了

KeyboardView keyboardView = new KeyboardView(context, null);
Keyboard keyboard = keyboardView.getKeyboard();
但它不断返回键盘为空


我尝试了这个方法来获取键盘高度。

实际上,您无法获取当前键盘的参考。详细信息是(请记住阅读注释)。

我尝试像这样获取Android虚拟键盘的高度[

但我并没有得到高度——只有屏幕的高度

(我为Unity3d制作android插件)

我尝试在函数Trace()中计算高度


这里有一个链接可以帮助您获得键盘高度:
public class KeyboardTool
{

    private EditTextEx input;
    public Activity _activity;
    private PopupWindow window;
    private int height= 0;
    public KeyboardTool(Activity activity)
    {
        if (activity == null)
            activity = com.unity3d.player.UnityPlayer.currentActivity;

        _activity = activity;
        _activity.runOnUiThread(new Runnable() {@SuppressLint("NewApi") public void run() 
        {

            window = new PopupWindow(_activity);
            input = new EditTextEx(_activity);

            //window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
            //window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            window.setTouchable(false);
            window.setFocusable(true);
            window.setSplitTouchEnabled(false);
            window.setOutsideTouchable(true);
            window.setIgnoreCheekPress();
            window.setClippingEnabled(false);
            window.setTouchInterceptor(new OnTouchListener()
            {

                @Override
                public boolean onTouch(View arg0, final MotionEvent arg1) {
                    Log.i("touch", arg0.toString() + ":" + arg1.toString());

                    _activity.dispatchTouchEvent(arg1);

                    return true;
                }

            });
            //window.
            window.setContentView(input);
            window.setBackgroundDrawable(new ColorDrawable(0));
            //window.setBackgroundDrawable(null);
            //


//          _activity.addContentView(input, new LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT));
//          _activity.addContentView( new EditTextEx(_activity), new LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT));
//          _activity.onWindowFocusChanged(false);
            input.setInputType(InputType.TYPE_CLASS_TEXT);
            input.setBackgroundColor(0);
            input.setVisibility(View.VISIBLE);
            input.setFocusable(true);
            input.setFocusableInTouchMode(true);

            window.setInputMethodMode(PopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            //window.showAtLocation(_activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 100);
            //input.requestFocus();
            //input.bringToFront();

//          UnityPlayer.currentActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//          window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

            Trace();
        }});
    }

    @SuppressLint("NewApi") private void Trace()
    {
        Window rootWindow = UnityPlayer.currentActivity.getWindow();
        View rootView = rootWindow.getDecorView().findViewById(android.R.id.content);

        Log.i("xxx", "xxx rootView " + rootView);

        rootView.getViewTreeObserver().addOnGlobalLayoutListener
        (
                new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout(){
                    Rect r = new Rect();
                    View view = UnityPlayer.currentActivity.getWindow().getDecorView();
                    view.getWindowVisibleDisplayFrame(r);
                    Log.i("xxx", "xxx left " + r.left +" " + r.top+ " " +  r.right + " " + r.bottom);
                    // r.left, r.top, r.right, r.bottom

                    Window rootWindow = UnityPlayer.currentActivity.getWindow();
                    View rootView = rootWindow.getDecorView().findViewById(android.R.id.content);
                    int heightDiff = rootView.getHeight()- rootWindow.getDecorView().getHeight();
                    Log.i("", "xxx heightDiff " + heightDiff + "rootView.getHeight() " + rootView.getHeight() + " rootWindow.getDecorView().getHeight() " + rootWindow.getDecorView().getHeight());
                }
                });

        View keyboardView22 = rootWindow.getDecorView().findViewById(android.R.id.keyboardView);
        Log.i("", "xxxx keyboardView22 " + keyboardView22);

        View view = UnityPlayer.currentActivity.getCurrentFocus();
        Log.i("xxx", "xxx view current " + view);
        Log.i("xxx", "xxx view as " + view.getHeight() + " " + view.getBottom() + " "+ view.getMeasuredHeight());
        KeyboardView keyboardView2 = new KeyboardView(UnityPlayer.currentActivity.getApplicationContext(),   null);
        Log.i("", "xxx keyboardView2.getKeyboard() "  + keyboardView2.getKeyboard() + " " + keyboardView2.getHeight());
        if (keyboardView2.getKeyboard() != null)
        {
            int height = (keyboardView2.getKeyboard()).getHeight();
            Log.i("", "xxx Trace  height2 + " + height + " "  + Toast.LENGTH_LONG);
        }

        Log.i("", "xxx input " + input.getBottom() + " " + input.findViewById(android.R.id.keyboardView));

        Rect outRect = new Rect();
        input.getWindowVisibleDisplayFrame(outRect);
        Log.i("", "xxxx outRect " + outRect.height() + " " + outRect.width() + " " + outRect.left + " " + outRect.right + " "+ outRect.centerY());
        if (outRect.centerY() != 0)
            height = outRect.centerY();
    }

    private boolean done;
    public boolean isDone()
    {
        UpdateStates();
        return done;
    }
    private boolean canceled;
    public boolean isCanceled()
    {
        UpdateStates();
        return canceled;
    }

    public void UpdateStates()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            done = input.done;
            canceled = input.canceled;
        }});
    }

    public void Show()
    {
        this.canceled = false;
        this.done = false;
        if (input != null)
        {
            input.done = false;
            input.canceled = false;
        }
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            window.setFocusable(true);
            input.setFocusable(true);
            input.setFocusableInTouchMode(true);

            window.showAtLocation(_activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0);
            input.requestFocus();
            input.bringToFront();
            InputMethodManager imm = (InputMethodManager)_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(input, 0);

            Trace();
        }});
    }

    public void Hide()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            window.setFocusable(false);
            input.setFocusable(false);
            input.setFocusableInTouchMode(false);
            input.clearFocus();
            window.dismiss();

            InputMethodManager imm = (InputMethodManager)_activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

            Trace();
        }});
    }
    private String text;
    public String GetText()
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
            text = input.getText().toString();
        }});
        return text;
    }

    public void SetText(final String text)
    {
        _activity.runOnUiThread(new Runnable() {public void run() 
        {
        input.setText(text);
        input.setSelection(text.length());
        }});
    }

    public void SetRect (final int x, final int y, final int width, final int height)
    {   
        _activity.runOnUiThread(new Runnable() {public void run() 
        {

            window.update(x, y, width, height);
            window.setWidth(width);
            window.setHeight(height);
            input.setWidth(width);
            input.setHeight(height);
        }});
    }

    public int GetHeight()
    {
        return this.height;
    }
}