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

时间:2015-04-19 07:30:53

标签: android keyboard

如何在Android中获取当前有效的KeyboardView?

我试过

KeyboardView keyboardView = new KeyboardView(context, null);
Keyboard keyboard = keyboardView.getKeyboard();

但它一直在返回键盘是Null。

我试过这个以获得键盘高度。

2 个答案:

答案 0 :(得分:0)

实际上,你无法获得当前Keyboard.Detail的引用here(请记得阅读评论。)。

答案 1 :(得分:0)

我尝试使用[Is there any way in android to get the height of virtual keyboard of device

获取虚拟键盘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;
    }
}