package textfield;
import java.io.IOException;
import java.util.Hashtable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class TextFieldText1 extends MIDlet {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO 自动生成方法存根
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void startApp() throws MIDletStateChangeException {
// TODO 自动生成方法存根
Display.init(this);
try {
Resources theme = Resources.open("/LWUITtheme.res");
UIManager.getInstance().setThemeProps(
theme.getTheme(theme.getThemeResourceNames()[0]));
} catch (IOException e) {
e.printStackTrace();
}
showForm();
}
public void showForm() {
Form f = new Form("测试TextField 的限制输入");
f.setLayout(new BorderLayout());
final Label label = new Label("空");
Hashtable ht = new Hashtable();
ht.put(new Integer('0'), "0");
ht.put(new Integer('9'), "9");
ht.put(new Integer('8'), "8");
ht.put(new Integer('7'), "7");
ht.put(new Integer('6'), "6");
ht.put(new Integer('5'), "5");
ht.put(new Integer('4'), "4");
ht.put(new Integer('3'), "3");
ht.put(new Integer('2'), "2");
ht.put(new Integer('1'), "1");
ht.put(new Integer('#'), "+");
TextField.addInputMode("+123", ht, false);
final TextField tf = new TextField();
tf.setConstraint(TextArea.PHONENUMBER );
tf.setInputMode("+123");
tf.setInputModeOrder(new String[]{"+123"});
tf.addDataChangeListener(new DataChangedListener(){
public void dataChanged(int i, int j) {
if(i == DataChangedListener.ADDED){
label.setText("ADDED");
if(tf.getText().length() > 5){
tf.setText(tf.getText().substring(0,5));
}
}
if(i == DataChangedListener.CHANGED){
label.setText("CHANGED");
}
if(i == DataChangedListener.REMOVED){
label.setText("REMOVED");
}
}
});
f.addComponent(BorderLayout.NORTH, tf);
f.addComponent(BorderLayout.SOUTH, label);
f.show();
}
}
lwuit TextField 的限制输入
最新推荐文章于 2019-10-07 11:39:17 发布