Frage

Ich möchte ein nicht eedierbares Textfeld (oder eine Unterklasse), in der die Pflege nicht einmal angezeigt wird. Alternativ möchte ich ein Multiline Labelfield. Ist eine davon möglich?

War es hilfreich?

Lösung

Textfeld ohne Fokuscursor

TextField readOnly = new TextField(NON_FOCUSABLE);
readOnly.setText("Read only, no carret");
add(readOnly);

Textfield Drawfocus Override

Wenn der Text zu groß ist, um den Bildschirm anzupassen, können Sie die Drawfocus -Methode in Textfield überschreiben, sodass Scrollen verfügbar sind:

TextField readOnly = new TextField(READONLY)
{
    protected void drawFocus(Graphics graphics, boolean on) {}
};

Textfelder, getrennt mit Nullfields

Eine weitere Option besteht darin, Textfield in mehrere zu teilen, die mit Nullfields getrennt sind:

class Scr extends MainScreen {
    public Scr() {

        String text = "Lorem ipsum dolor sit amet, consectetuer "
                + "adipiscing elit, sed diam nonummy nibh euismod "
                + "tincidunt ut laoreet dolore magna aliquam erat "
                + "volutpat. Ut wisi enim ad minim veniam, quis "
                + "nostrud exerci tation ullamcorper suscipit "
                + "lobortis nisl ut aliquip ex ea commodo consequat. "
                + "Duis autem vel eum iriure dolor in hendrerit in "
                + "vulputate velit esse molestie consequat, vel "
                + "illum dolore eu feugiat nulla facilisis at vero "
                + "eros et accumsan et iusto odio dignissim qui "
                + "blandit praesent luptatum zzril delenit augue "
                + "duis dolore te feugait nulla facilisi.";

        text = addScrollText(text, 150);
    }

    private String addScrollText(String text, int partSize) {
        while (0 < text.length()) {
            int len = Math.min(partSize, text.length());
            TextField readOnly = new TextField(NON_FOCUSABLE);
            readOnly.setText(text.substring(0, len));
            add(readOnly);
            add(new NullField());
            text = text.substring(len);
        }
        return text;
    }
}

Multiline Labelfield

Multiline Text in Labelfield, verwenden Sie einfach Newline Escape -Zeichen:

String text = "first line \nnew line \nanother line";
LabelField multiLine = new LabelField(text);
add(multiLine);

Andere Tipps

Ja, Sie können die Farbmethode jedes UI -Elements überschreiben

Ein Beispiel:

public class WhiteLabelField extends LabelField 
{
    public WhiteLabelField()
    {
        super();
    }
    public WhiteLabelField(ObjectGroup text)
    {
        super(text);
    }
    public WhiteLabelField(Object text, long style)
    {
        super(text, style);
    }
    public void paint(Graphics _g)
    {
        _g.setColor(Color.WHITE);
        super.paint(_g);
    }
    // Custom
    public void setSmallFontSize()
    {
        setFont( Font.getDefault( ).derive( Font.PLAIN, 16 ));
    }
}

Sie könnten auch a verwenden RichTextField mit einem Stil von Field.NON_FOCUSABLE Und Sie werden das haben, was Sie wollen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top