▼MIDP2.0メモ▼
テキストフィールドを使う


テキストフィールドを使うプログラムです。

 


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

//テキストフィールドを使う(本体)
public class TextFieldEx extends MIDlet {
    static MIDlet current;

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

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

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

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


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

//テキストフィールドを使う(フォーム)
class TextFieldForm extends Form
    implements CommandListener {
    TextField  textField0;//テキストフィールド0(ANY)
    TextField  textField1;//テキストフィールド1(EMAILADDR)
    TextField  textField2;//テキストフィールド2(NUMERIC)
    TextField  textField3;//テキストフィールド3(PASSWORD)
    TextField  textField4;//テキストフィールド4(PHONENUMBER)
    TextField  textField5;//テキストフィールド5(URL)
    Command    soft1;     //ソフトキー1

    //コンストラクタ
    TextFieldForm(){
        super("TextFieldEx");

        //テキストフィールド0(ANY)
        textField0=new TextField("ANY","",30,TextField.ANY);
        append(textField0);

        //テキストフィールド1(EMAILADDR)
        textField1=new TextField("EMAILADDR","",30,TextField.EMAILADDR);
        append(textField1);

        //テキストフィールド2(NUMERIC)
        textField2=new TextField("NUMERIC","",30,TextField.NUMERIC);
        append(textField2);

        //テキストフィールド3(PASSWORD)
        textField3=new TextField("PASSWORD","",30,TextField.PASSWORD);
        append(textField3);

        //テキストフィールド4(PHONENUMBER)
        textField4=new TextField("PHONENUMBER","",30,TextField.PHONENUMBER);
        append(textField4);

        //テキストフィールド5(URL)
        textField5=new TextField("URL","",30,TextField.URL);
        append(textField5);

        //ソフトキー
        soft1=new Command("OK",Command.SCREEN,1);
        addCommand(soft1);
        setCommandListener(this);
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        if (c==soft1) {
            //アラート表示
            Alert alert=new Alert("ANY",
                "["+textField0.getString()+"]を入力しました",
                null,AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            (Display.getDisplay(TextFieldEx.current)).setCurrent(alert);
        }
    }
}



−戻る−