- package com.mopietek;
- 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;
- public class TextTest extends MIDlet{
- TextField tf = null;
- protected void destroyApp(boolean unconditional)
- throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- }
- protected void pauseApp() {
- // TODO Auto-generated method stub
- }
- protected void startApp() throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- Display.init(this);
- Form f = new Form("ceshi");
- f.setLayout(new BorderLayout());
- tf = new TextField();
- final Label label = new Label("kong");
- 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);
- 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();
- }
- }
用Hashtable來限制textfild數字輸入
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.