▼MIDP2.0メモ▼
テキストボックスを使う


テキストボックスを使うプログラムです。



プログラム
TextBoxEx.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

//テキストボックスを使う(本体)
public class TextBoxEx extends MIDlet {
    static MIDlet current;

    //コンストラクタ
    public TextBoxEx() {
        current=this;
        Display.getDisplay(this).setCurrent(new TextBoxForm());
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean flag) {
    }
}


TextBoxForm.java
import javax.microedition.lcdui.*;

//テキストボックスを使う(フォーム)
class TextBoxForm extends Form
    implements CommandListener {
    StringItem stringItem;//文字列アイテム
    TextBox    textBox;   //テキストボックス

    //コンストラクタ
    public TextBoxForm(){
        super("TextBoxEx");
        //文字列アイテム
        stringItem=new StringItem("文字列","");
        append(stringItem);

        //テキストボックス
        textBox=new TextBox("","",200,TextField.ANY);
        textBox.addCommand(new Command("OK",Command.SCREEN,1));
        textBox.setCommandListener(this);

        //ソフトキー
        addCommand(new Command("入力",Command.SCREEN,0));
        setCommandListener(this);
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        //テキストボックスを開く
        if (c.getPriority()==0) {
            textBox.setString(stringItem.getText());
            Display.getDisplay(TextBoxEx.current).setCurrent(textBox);
        }

        //テキストボックスを閉じる
        if (c.getPriority()==1) {
            stringItem.setText(textBox.getString());
            Display.getDisplay(TextBoxEx.current).setCurrent(this);
        }
    }
}



−戻る−