如何在运行时更改软键盘的高度?

how to change a soft keyboard#39;s height at run-time?(如何在运行时更改软键盘的高度?)

本文介绍了如何在运行时更改软键盘的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个软键盘,我想在用户在横向和纵向模式之间进行选择时在运行时更改它的高度.我知道如何在 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.

我想到的唯一一件事就是从 Keyboard 继承并覆盖它的 setKeysHeight (int height),但它似乎没用,因为整个键盘停止响应我的点击,并且高度(虽然与以前不同)并不关心上述函数中的 'height'.

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?

推荐答案

原解决方案发布于 https://stackoverflow.com/a/9695482/1241783 但它没有附带解释,所以我在这里扩展一下.

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

1) 创建一个新类来扩展覆盖 getHeight() 方法的 Keyboard 类.

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:在某些配置下,我在更改键盘高度后无法按下底行键,我发现调用 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) 最后一步是在扩展 InputMethodService 的主类中调用 changeKeyHeight.在 onStartInputView() 内部执行(覆盖它),因为这是在更改高度后(通过首选项或其他方式)重绘键盘的位置.

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 到像素转换器,代码如下:

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

这篇关于如何在运行时更改软键盘的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在运行时更改软键盘的高度?

基础教程推荐