TextField

Berikut contoh penggunaan TextField

Kelas Midlet


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tutorial;

import Tutorial.Textfield.TextfieldForm;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;

/**
* @author Photosphere
*/
public class Midlet extends MIDlet {
private Display display;
private TextfieldForm textfieldForm;
public Midlet() {
display = Display.getDisplay(this);
textfieldForm = new TextfieldForm(display, this, "Contoh Textfield");
display.setCurrent(textfieldForm);
}

public void startApp() {
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void exit(){
destroyApp(false);
notifyDestroyed();
}
}

dan kelas berekstensi Form untuk meletakkan textfield


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Tutorial.Textfield;

import Tutorial.Midlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

/**
*
* @author Photosphere
*/
public class TextfieldForm extends Form implements CommandListener{

private Display display;
private Midlet midlet;
private Command commExit;

private TextField any, email, numeric, password, phone, url;

public TextfieldForm(Display display, Midlet midlet, String title) {
super(title);
this.display = display;
this.midlet = midlet;

//buat command
commExit = new Command("Exit", Command.EXIT, 1);
addCommand(commExit);

//membuat textfield
//1. textfield dengan constrain any
any = new TextField("Textfield any", "", 50, TextField.ANY);
any.setString("diisi apapun");
append(any);

//2. textfield dengan constrain email
email = new TextField("Email", "", 50, TextField.EMAILADDR);
email.setString("adhitya_dk@live.com");
append(email);

//3. textfield dengan constrain numeric
numeric = new TextField("Numeric", "", 50, TextField.NUMERIC);
numeric.setString("1234567890");
append(numeric);

//4. textfield dengan constrain password
password = new TextField("Password", "", 50, TextField.PASSWORD);
password.setString("textfield");
append(password);

//5. textfield dengan constrain phone
phone = new TextField("Nomor Telepon", "", 50, TextField.PHONENUMBER);
phone.setString("085649973306");
append(phone);

//6. textfield dengan constrain url
url = new TextField("Textfield any", "", 50, TextField.ANY);
url.setString("adhityadwikristanto.wordpress.com");
append(url);

setCommandListener(this);
}

public void commandAction(Command c, Displayable d) {
if(c==commExit){
midlet.exit();
}
}
}

Berikut tampilan yang dihasilkan

TextField