如何改变一个软键盘的高度在运行时?键盘、高度

2023-09-06 03:37:04 作者:嗜血剑客

我设计一个软键盘,我想改变其在运行时的高度为用户横向和纵向模式之间进行选择。我知道如何改变的关键的身高在XML中,但我需要动态地做到这一点。

I'm designing a soft keyboard and I want to change its height at run-time as the user choose between landscape and portrait mode. I know how to change key's height in xml, but I need to do it dynamically.

这是来到我的脑海里唯一的想法就是从键盘以继承并覆盖其 setKeysHeight ( INT 高),但似乎没用,因为全键盘停止对我的点击和高度(尽管从$ P $不同pviously)不关心的高的在上述功能。

The only thing that came to my mind was to subclass from Keyboard and override its setKeysHeight (int height), but it seems useless as the whole keyboard stopped responding to my clicks and the height (though different from previously) didn't care about 'height' in the aforementioned function.

任何想法/解决方法吗?

Any idea/workaround?

推荐答案

张贴在原液 http://stackoverflow.com/a /一百二十四万一千七百八十三分之九百六十九万五千四百八十二,但它没有附带解释所以在这里我向它一下。

Original solution posted at http://stackoverflow.com/a/9695482/1241783 but it doesn't come with explanation so here I extend it a bit.

1)创建一个扩展了覆盖的getHeight()方法键盘类的新类。

1) Create a new class that extends the Keyboard class that overrides the getHeight() method.

@Override
public int getHeight() {
   return getKeyHeight() * 3;
}

注:数字3在这里是行的总数,如果你的键盘有5行,把5

Note: the number 3 here is your total number of rows, if your keyboard has 5 rows, put 5.

如果您的键盘行高为每个行不同,在这里你需要自己计算并返回的总高度(单位为像素,带我一段时间才能弄清楚它是不是DP所以需要DP像素转换所有的计算),例如:

If your keyboard row height is different for each row, here you need to calculate yourself and return the total height (unit is in pixels, took me a while to figure out that it is not dp so need to convert dp to pixel for all calculations) for example:

@Override
public int getHeight() {
   return row1Height + row2Height + row3Height + row4Height + row5Height;
}

2)在同一个类中创建一个新的公共职能。

2) Create a new public function in the same class.

public void changeKeyHeight(double height_modifier)
{
   int height = 0;
   for(Keyboard.Key key : getKeys()) {
      key.height *= height_modifier;
      key.y *= height_modifier;
      height = key.height;
   }
   setKeyHeight(height);
   getNearestKeys(0, 0); //somehow adding this fixed a weird bug where bottom row keys could not be pressed if keyboard height is too tall.. from the Keyboard source code seems like calling this will recalculate some values used in keypress detection calculation
}

如果你不使用height_modifier而是设置为特定的高度,而不是,你需要自己进行计算key.y位置。

If you're not using height_modifier but set to specific height instead, you'll need to calculate key.y position yourself.

如果您的键盘行高为每个行不同,您可能需要检查的按键,确定它属于该行并设置高度改正值,如果没有钥匙将相互重叠。同时存储在getHeight()都会上面使用的行高在私有变量。 PS:在某些配置,我可以PSS改变键盘的高度后,下排按键没有$ P $,而且我发现,调用getNearestKeys()解决了,虽然我不知道是什么原因

If your keyboard row height is different for each row, you may need to check the keys, determine the row it belongs and set the height to correct value if not the keys will overlap each other. Also store the row heights in private variable to be used in getHeight() above. PS: On certain configuration I couldn't press the bottom row keys after changing keyboard height, and I found that calling getNearestKeys() fixes that though I'm not exactly sure why.

注:key.y是关键的y位置,坐标0开始从键盘上方,并下降作为值增加而增加。例如坐标100分至100像素从键盘的顶部:)

Note: key.y is y position of the key, coordinate 0 starts from the top of the keyboard, and goes down as the value increases. e.g. Coordinate 100 points to 100 pixel from the top of the keyboard :)

3)最后一步是调用changeKeyHeight在扩展InputMethodService主类。难道这里面(覆盖它)onStartInputView(),因为这是那里的键盘更改高度(通过preference或某事)后,应重新绘制。

3) Last step is to call changeKeyHeight in your main class that extends InputMethodService. Do it inside (override it) onStartInputView() as this is where the keyboard should be redrawn after you change the height (via preference or something).

如果你正在寻找Android的软键盘样本项目,这将是这样的:

If you're looking at the Android soft keyboard sample project, it will be like this:

@Override public void onStartInputView(EditorInfo attribute, boolean restarting) {
   super.onStartInputView(attribute, restarting);

   // Change the key height here dynamically after getting your value from shared preference or something
   mCurKeyboard.changeKeyHeight(1.5);

   // Apply the selected keyboard to the input view.
   mInputView.setKeyboard(mCurKeyboard);
   mInputView.closing();        

   final InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype();
   mInputView.setSubtypeOnSpaceKey(subtype);
}

干杯!

另外:如果你需要一个DP像素转换器,这里的code:

Extra: If you need a dp to pixel converter, here's the code:

private int convertDpToPx(int dp)
{
   return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}