Thursday, July 12, 2012

text - TextInput

Pivot提供了兩種輸入文字的元件,TextInput→單行未格式化的文字,它也可以使用密碼的方式來呈現文字;另一個元件則是TextArea,可允許輸入多行且格式化的文字。本文先介紹TextInput元件。

本範例是由官網取得。它的功能是當使用輸入的字元足以判斷出唯一的值時,系統會自動幫使用者填入剩餘的字元(顯示台中市各區名稱)。



============範例展示=================================================


============範例展示=================================================


以下是ui.bxml:

<!--ui.bxml-->
<myex:TextInputs title="Text Inputs" maximized="true"
                 xmlns:bxml="http://pivot.apache.org/bxml"
                 xmlns:myex="myex"
                 xmlns="org.apache.pivot.wtk">
    <BoxPane styles="{padding:4, verticalAlignment:'center'}">
        <Label text="台中市各區:"/>
        <TextInput bxml:id="stateTextInput"/>
    </BoxPane>
</myex:TextInputs>

接下來則是myex#TextInputs.java的實作。

package myex;

import java.net.URL;
import org.apache.pivot.beans.Bindable;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputContentListener;
import org.apache.pivot.wtk.Window;

public class TextInputs extends Window implements Bindable {

    private TextInput stateTextInput = null;
    private ArrayList<String> states;

    public TextInputs() {
        //使用ArrayList建立台中市各區名稱
        states = new ArrayList<String>();
        //大小寫不拘,中文用不到
        states.setComparator(String.CASE_INSENSITIVE_ORDER);
        //這裡的順序在官網中有提到要注意排序問題
        states.add("中區");
        states.add("大甲區");
        states.add("大安區");
        states.add("大肚區");
        states.add("大里區");
        states.add("大雅區");
        states.add("太平區");
        states.add("外埔區");
        states.add("石岡區");
        states.add("北屯區");
        states.add("北區");
        states.add("后里區");
        states.add("西屯區");
        states.add("西區");
        states.add("沙鹿區");
        states.add("和平區");
        states.add("東區");
        states.add("東勢區");
        states.add("南屯區");
        states.add("南區");
        states.add("神岡區");
        states.add("烏日區");
        states.add("梧棲區");
        states.add("清水區");
        states.add("新社區");
        states.add("潭子區");
        states.add("龍井區");
        states.add("豐原區");
        states.add("霧峰區");
    }

    @Override
    public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
        stateTextInput = (TextInput) namespace.get("stateTextInput");
        //處理文字輸入事件TextInputContent
        stateTextInput.getTextInputContentListeners().add(new TextInputContentListener.Adapter() {

            @Override
            public void textInserted(final TextInput textInput, int index, int count) {
                String text = textInput.getText();

                int i = ArrayList.binarySearch(states, text,
                        states.getComparator());

                if (i < 0) {
                    i = -(i + 1);
                    int n = states.getLength();

                    if (i < n) {
                        //中文不需要
                        //text = text.toLowerCase();
                        final String state = states.get(i);
                        //原本是state.toLowerCase().startsWith(text)中文不需要
                        if (state.startsWith(text)) {
                            String nextState = (i == n - 1)
                                    ? null : states.get(i + 1);
                            //!nextState.toLowerCase().startsWith(text)中文不需要
                            if (nextState == null
                                    || !nextState.startsWith(text)) {
                                textInput.setText(state);

                                int selectionStart = text.length();
                                int selectionLength = state.length() - selectionStart;
                                textInput.setSelection(selectionStart, selectionLength);
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    public void open(Display display, Window owner) {
        super.open(display, owner);
        //一開始TextInput就取得focus
        stateTextInput.requestFocus();
    }
}

No comments:

Post a Comment