Android

时间:2019-05-30 17:53:52

标签: gluon-mobile javafxports

当TextField获得焦点后虚拟键盘出现并且该文本字段在对话框阶段之外消失时,将向上滚动一个模式对话框(如登录对话框)。

我有一个登录对话框,其中显示一个按钮。当文本字段获得焦点时,虚拟键盘将在对话框阶段之外滚动对话框场景。因此,对话框的一半消失了,无法看到您正在输入的内容。对话阶段的另一半保持白色且为空。 在Android 4.4和8.0上进行了测试

一些图片:

这是对话框控制器代码:

public class DialogController implements Initializable {

    private Dialog<Map<String, String>> dialog = new Dialog<>();

    @FXML
    private AnchorPane dialogview;

    @FXML
    private PasswordField password;

    @FXML
    private TextField txtTerminal;

    DialogController() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("logindialog.fxml"));
        fxmlLoader.setController(this);
        dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
        dialog.setGraphic(null);
        dialog.setHeaderText(null);

        try {
            dialogview = fxmlLoader.load();
        } catch (IOException exception) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, exception);
        }
        dialog.getDialogPane().setContent(dialogview);

        Optional<Map<String, String>> result = dialog.showAndWait();
        if (result.isPresent()) {
            verificar();
        }
        password.setText("");
        txtTerminal.setText("");
        dialog.hide();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Node botonOK = dialog.getDialogPane().lookupButton(ButtonType.OK);
        ((Button) botonOK).setPrefSize(100, 40);
        Node botonCANCEL = dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
        ((Button) botonCANCEL).setPrefSize(100, 40);
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == ButtonType.OK) {
                HashMap<String, String> respuesta = new HashMap<>();
                respuesta.put("terminal", txtTerminal.getText());
                respuesta.put("password", password.getText());
                return respuesta;
            }
            return null;
        });
        Platform.runLater(() -> txtTerminal.requestFocus());
    }

    private void verificar() {
    }

}

fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<AnchorPane fx:id="dialogview"  prefWidth="250.0" style="-fx-background-color: linear-gradient(#5264AE, white);" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
    <BorderPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <center>
            <GridPane alignment="center" hgap="3" vgap="3">
                <padding>
                    <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
                </padding>
                <columnConstraints>
                    <ColumnConstraints halignment="LEFT" maxWidth="Infinity" minWidth="100.0" prefWidth="120.0">
                    </ColumnConstraints>
                    <ColumnConstraints hgrow="ALWAYS" maxWidth="Infinity" minWidth="120.0" prefWidth="180.0">
                    </ColumnConstraints>
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints minHeight="30.0" prefHeight="30.0" />
                    <RowConstraints minHeight="30.0" prefHeight="30.0" />
                </rowConstraints>
                <children>
                    <Label text="Terminal:">
                        <font>
                            <Font name="System Bold" size="14.0" />
                        </font>
                    </Label>
                    <Label text="Clave" GridPane.rowIndex="1">
                        <font>
                            <Font name="System Bold" size="14.0" />
                        </font>
                    </Label>
                    <TextField fx:id="txtTerminal" GridPane.columnIndex="1">
                        <font>
                            <Font size="14.0" />
                        </font>
                    </TextField>
                    <PasswordField fx:id="password" GridPane.columnIndex="1" GridPane.rowIndex="1">
                        <font>
                            <Font size="14.0" />
                        </font>
                    </PasswordField>
                </children>
            </GridPane>
        </center>
    </BorderPane>
</AnchorPane>


应用程序:


public class Prudialog extends Application {

    private AnchorPane root;

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Login");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                DialogController termcontroller = new DialogController();
            }
        });

        VBox vbox = new VBox(btn);

        AnchorPane.setLeftAnchor(vbox, 0.0);
        AnchorPane.setTopAnchor(vbox, 0.0);
        AnchorPane.setRightAnchor(vbox, 0.0);
        AnchorPane.setBottomAnchor(vbox, 0.0);

        root = new AnchorPane();
        root.getChildren().add(vbox);

        Rectangle2D pantalla = Screen.getPrimary().getVisualBounds();
        Scene scene = new Scene(root, pantalla.getWidth(), pantalla.getHeight());

        primaryStage.setTitle("Dialog Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

我希望当虚拟键盘出现时,对话框的文本字段仍然可见。 我注意到这种预期的行为有时是错误的,但很多时候不是,我不知道为什么。我已经测试了隐藏和显示键盘,更改焦点,按下取消按钮等,然后出现的对话框会有所不同。 (我无法上传屏幕截图)。

0 个答案:

没有答案