我的问题是,在android中,当用户通过触摸从一个文本字段切换到另一个文本字段时,虚拟键盘每次都会消失并重新出现。当用户触摸另一个文本字段时,如何确保键盘始终可见?我使用 Android SDK 的模拟器和真正的 Android 手机尝试了 graddle 项目(旧模板)和 Maven 项目模板。全部都一样。我使用了 Y 滚动框布局,如其他帖子中所述。但没有任何效果。
我在下面提供我的代码示例。
旧 Gradle 项目:
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
// use two network threads instead of one
updateNetworkThreadCount(2);
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature
Log.bindCrashProtection(true);
addNetworkErrorListener(err -> {
// prevent the event from propagating
err.consume();
if(err.getError() != null) {
Log.e(err.getError());
}
Log.sendLogAsync();
Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
});
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World", new BoxLayout(BoxLayout.Y_AXIS));
hi.getContentPane().setScrollableY(true);
hi.setScrollableY(true);
System.out.println(hi.isScrollableY());
hi.add(new TextField("","1"));
hi.add(new TextField("","2"));
hi.add(new TextField("","3"));
hi.add(new TextField("","4"));
hi.add(new TextField("","5"));
hi.add(new TextField("","6"));
hi.add(new TextField("","7"));
hi.add(new TextField("","8"));
hi.add(new TextField("","9"));
hi.add(new TextField("","10"));
hi.add(new TextField("","11"));
hi.add(new TextField("","12"));
hi.add(new TextField("","13"));
hi.add(new TextField("","14"));
hi.add(new TextField("","15"));
hi.add(new TextField("","16"));
hi.add(new TextField("","17"));
hi.add(new TextField("","18"));
hi.add(new TextField("","19"));
hi.add(new TextField("","20"));
hi.show();
}
public void stop() {
current = getCurrentForm();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = getCurrentForm();
}
}
public void destroy() {
}
}
新建 Maven 项目:
public class MyApp extends Lifecycle {
@Override
public void runApp() {
Form hi = new Form("Hi World", BoxLayout.y());
Button helloButton = new Button("Hello World");
hi.add(helloButton);
TextField tf1 = new TextField("","text1");
TextField tf2 = new TextField("","text2");
TextField tf3 = new TextField("","text3");
TextField tf4 = new TextField("","text4");
hi.add(tf1);
hi.add(tf2);
hi.add(tf3);
hi.add(tf4);
helloButton.addActionListener(e -> hello());
hi.getToolbar().addMaterialCommandToSideMenu("Hello Command",
FontImage.MATERIAL_CHECK, 4, e -> hello());
hi.show();
}
private void hello() {
Dialog.show("Hello Codename One", "Welcome to Codename One", "OK", null);
}
}
如果您按下一步,键盘应保持打开状态,如果您滚动,键盘也应保持打开状态。不幸的是,当您通过触摸单击新文本字段时,我们需要在执行其他操作之前提交前一个文本字段,这就是发生这种情况的原因。
也许可以针对这种特殊情况进行改进,但这并非易事,因为代码在两个单独的线程(Android Native UI 线程和 Codename One EDT)上运行。