2015-04-19 2 views
0

Как получить активное приложение KeyboardView в Android?Как получить текущую активную клавиатуру в Android?

Я попытался

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

Но он держит на возвращение клавиатуры является Null.

Я пробовал это, чтобы получить высоту клавиатуры.

+1

http://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard-of-device –

+1

здесь ссылку, которая может помочь вам получить высоту клавиатуры: http://stackoverflow.com/questions/16788959/is-there-any-way-in-android-to-get-the-height-of-virtual-keyboard- из-устройства –

ответ

0

Я пытаюсь получить высоту виртуальной клавиатуры Android, как это [Is there any way in android to get the height of virtual keyboard of device

Но я не получаю heigth - получить только экрана heigth.

(я сделать андроида плагин для Unity3d)

я пытаюсь известково высоту в функции трассировки().

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

На самом деле вы не можете получить ссылку на текущую клавиатуру. Деталь here (помните, что читайте комментарии.).

Смежные вопросы