目前,JavaFX 或 JavaFXPorts 中没有内置方法来获取(本机)iOS 软键盘。
获取键盘并找出是否有任何节点的解决方案,例如TextField
将被它覆盖,需要一个Service
来自 Gluon Charm Down 中的那些library,但目前还没有这样的KeyboardService
.
基于本机解决方案,例如this,当键盘显示或隐藏时很容易收到通知。因此我们可以利用这些侦听器并将高度值发送回 JavaFX 层。
所以让我们创建KeyboardService
考虑如何在 Charm Down 库中创建服务。
由于这有点超出范围,我创建了这个gist与所需的文件。
请按照以下步骤使其工作:
- 创建胶子项目
使用适用于您的 IDE 的最新版本的 Gluon 插件创建一个 Gluon 项目(单一视图)。
- 添加KeyboardService接口
添加包com.gluonhq.charm.down.plugins
。添加类KeyboardService
(link) and KeyboardServiceFactory
(link).
public interface KeyboardService {
public ReadOnlyFloatProperty visibleHeightProperty();
}
- iOS 实现
在iOS包下添加服务的iOS实现IOSKeyboardService
(link).
public class IOSKeyboardService implements KeyboardService {
static {
System.loadLibrary("Keyboard");
initKeyboard();
}
private static ReadOnlyFloatWrapper height = new ReadOnlyFloatWrapper();
@Override
public ReadOnlyFloatProperty visibleHeightProperty() {
return height.getReadOnlyProperty();
}
// native
private static native void initKeyboard();
private void notifyKeyboard(float height) {
Platform.runLater(() -> this.height.setValue(height));
}
}
- 原生代码
创建一个native
下的文件夹/src/ios
并添加Keyboard.h
(link) file:
#import <UIKit/UIKit.h>
#include "jni.h"
@interface Keyboard : UIViewController {}
@end
void sendKeyboard();
and the Keyboard.m
(link) file:
static int KeyboardInited = 0;
jclass mat_jKeyboardServiceClass;
jmethodID mat_jKeyboardService_notifyKeyboard = 0;
Keyboard *_keyboard;
CGFloat currentKeyboardHeight = 0.0f;
JNIEXPORT void JNICALL Java_com_gluonhq_charm_down_plugins_ios_IOSKeyboardService_initKeyboard
(JNIEnv *env, jclass jClass)
{
if (KeyboardInited)
{
return;
}
KeyboardInited = 1;
mat_jKeyboardServiceClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/charm/down/plugins/ios/IOSKeyboardService"));
mat_jKeyboardService_notifyKeyboard = (*env)->GetMethodID(env, mat_jKeyboardServiceClass, "notifyKeyboard", "(F)V");
GLASS_CHECK_EXCEPTION(env);
_keyboard = [[Keyboard alloc] init];
}
void sendKeyboard() {
GET_MAIN_JENV;
(*env)->CallVoidMethod(env, mat_jKeyboardServiceClass, mat_jKeyboardService_notifyKeyboard, currentKeyboardHeight);
}
@implementation Keyboard
- (void) startObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) stopObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)notification {
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
currentKeyboardHeight = kbSize.height;
sendKeyboard();
}
- (void)keyboardWillHide:(NSNotification*)notification {
currentKeyboardHeight = 0.0f;
sendKeyboard();
}
@end
- 构建原生库
在具有最新版本 XCode 的 Mac 上,您可以构建本机库libKeyboard.a
。为此,您需要添加xcodebuild
任务给build.gradle
项目文件(link)。它基于ios-build.gradle
file来自魅力下降。
task xcodebuild {
doLast {
xcodebuildIOS("$project.buildDir","$project.projectDir", "Keyboard")
}
}
保存您的项目并运行./gradlew clean build xcodebuild
从项目根目录下的命令行。
如果一切就位,你应该会发现libKeyboard.a
under build/native
。复制文件,创建文件夹jniLibs
under src/ios
,并将其粘贴到那里。
- 实施服务
Add a TextField
to the BasicView
,并将对齐方式更改为BOTTOM-CENTER
.
VBox controls = new VBox(15.0, label, button, new TextField());
controls.setAlignment(Pos.BOTTOM_CENTER);
实施服务:
Services.get(KeyboardService.class).ifPresent(keyboard -> {
keyboard.visibleHeightProperty().addListener((obs, ov, nv) ->
setTranslateY(-nv.doubleValue()));
});
- 部署并运行
你应该把一切都准备好。插入你的 iPhone/iPad,然后运行./gradlew --info launchIOSDevice
.
当textField获得焦点时,软键盘出现,并且视图被平移,因此textField完全可见:
希望这项服务能在某个时候包含在 Charm Down 中。但这也是如何添加自定义服务的一个很好的示例。另请注意,Charm Down 项目是开源的,因此欢迎任何贡献。