用Hashtable來限制textfild數字輸入

  1. package com.mopietek;   
  2.   
  3. import java.util.Hashtable;   
  4.   
  5. import javax.microedition.midlet.MIDlet;   
  6. import javax.microedition.midlet.MIDletStateChangeException;   
  7.   
  8. import com.sun.lwuit.Display;   
  9. import com.sun.lwuit.Form;   
  10. import com.sun.lwuit.Label;   
  11. import com.sun.lwuit.TextArea;   
  12. import com.sun.lwuit.TextField;   
  13. import com.sun.lwuit.events.DataChangedListener;   
  14. import com.sun.lwuit.layouts.BorderLayout;   
  15.   
  16. public class TextTest extends MIDlet{   
  17.   
  18.      TextField tf = null;   
  19.     protected void destroyApp(boolean unconditional)   
  20.             throws MIDletStateChangeException {   
  21.         // TODO Auto-generated method stub   
  22.            
  23.     }   
  24.   
  25.     protected void pauseApp() {   
  26.         // TODO Auto-generated method stub   
  27.            
  28.     }   
  29.   
  30.     protected void startApp() throws MIDletStateChangeException {   
  31.         // TODO Auto-generated method stub   
  32.         Display.init(this);   
  33.            
  34.         Form f = new Form("ceshi");   
  35.         f.setLayout(new BorderLayout());   
  36.         tf = new TextField();   
  37.         final Label label = new Label("kong");   
  38.            
  39.         Hashtable ht = new Hashtable();   
  40.         ht.put(new Integer('0'), "0");   
  41.         ht.put(new Integer('9'), "9");   
  42.         ht.put(new Integer('8'), "8");   
  43.         ht.put(new Integer('7'), "7");   
  44.         ht.put(new Integer('6'), "6");   
  45.         ht.put(new Integer('5'), "5");   
  46.         ht.put(new Integer('4'), "4");   
  47.         ht.put(new Integer('3'), "3");   
  48.         ht.put(new Integer('2'), "2");   
  49.         ht.put(new Integer('1'), "1");   
  50.         ht.put(new Integer('#'), "+");   
  51.         TextField.addInputMode("+123", ht, false);   
  52.            
  53.            
  54.         tf.setConstraint(TextArea.PHONENUMBER);   
  55.            
  56.         //缺少這兩句話就會報數組越界異常   
  57.         tf.setInputMode("+123");   
  58.         tf.setInputModeOrder(new String[]{"+123"});   
  59.            
  60.            
  61.         tf.addDataChangeListener(new DataChangedListener(){   
  62.   
  63.             public void dataChanged(int i, int j) {   
  64.                    
  65.                 if(i == DataChangedListener.ADDED){   
  66.                     label.setText("ADDED");   
  67.                     if(tf.getText().length()>5){   
  68.                         tf.setText(tf.getText().substring(0,5));   
  69.                     }   
  70.                 }   
  71.                 if(i == DataChangedListener.CHANGED){   
  72.                     label.setText("CHANGED");   
  73.                 }   
  74.                 if(i == DataChangedListener.REMOVED){   
  75.                     label.setText("REMOVED");   
  76.                 }   
  77.                
  78.             }   
  79.                
  80.         });   
  81.          f.addComponent(BorderLayout.NORTH,tf);   
  82.          f.addComponent(BorderLayout.SOUTH,label);   
  83.          f.show();   
  84.      }       
  85.  }          
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章