Unity3d虚拟Android键盘的高度

时间:2015-04-19 09:28:39

标签: android keyboard

我尝试获取Unity3d虚拟Android键盘的高度

我为Unity3d编写了android插件,并在下面的代码中尝试了函数Trace()中的calc高度(但总是得到屏幕的高度或0.我创建窗口并编辑字段并在需要时显示它。我需要得到虚拟的高度键盘用于在虚拟键盘上放置一些图像):

public class KeyboardTool
{

    private EditTextEx input;
    public Activity _activity;
    private PopupWindow window;
    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.setTouchable(false);
            window.setFocusable(true);
            window.setSplitTouchEnabled(false);
            window.setOutsideTouchable(true);
            window.setIgnoreCheekPress();
            window.setClippingEnabled(false);


            window.setContentView(input);
            window.setBackgroundDrawable(new ColorDrawable(0));


            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);
            UnityPlayer.currentActivity.getWindow().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());
                }
                });
    }

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

0 个答案:

没有答案