J2ME:高級列表

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class list1 extends MIDlet implements CommandListener{
    Display dis;
    List l=new List("請你選擇操作",List.MULTIPLE);  //EXCLUSIVE爲顯示選擇單選,IMPLICIT爲普通單選(可以指定被選中時的command對象),MULTIPLE爲多選
 Ticker t=new Ticker("......");   //滾動顯式文字
 String s1="楠木阿尼陀佛";
 String s2="阿呢路亞";
 private Command cmdOK=new Command("OK",Command.SCREEN,1);  //主動搶佔右軟鍵
 
 
    public list1() throws IOException{
 
    
  // TODO Auto-generated constructor stub
 }

 protected void destroyApp(boolean arg0) 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
  Image img=null;
     try {
   img=Image.createImage("/12.png");   //獲取圖片資源,注意圖片一定要放在res包內
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Font font=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_LARGE); //設置字體樣式
  dis=Display.getDisplay(this);       //感覺上Displayable類和Swing裏面的Contends一樣,屬於框架上面的容器
  dis.setCurrent(l);                 
  l.append("刪除該號碼",img);
  l.append("向該號碼發送短信",null);
  l.append("編輯該號碼",null);
  l.append("講該號碼設置爲好友",null);
  //l.delete(0);  //刪除列表項目
  //l.set(2,"叼你啊", null);
  //l.insert(3,"反叼翻你" , null);
  //l.setCommandListener(this);
  l.append("唸佛", null);
  l.append("講耶穌", null);
  l.setFont(1,font);  //改變選項字體
  l.setTicker(t);    //設置滾動顯示
  l.addCommand(cmdOK);   //添加按鈕
  l.setCommandListener(this);   //添加監聽器
  
 }

 public void commandAction(Command c, Displayable d) {
  // TODO Auto-generated method stub
  //l.setTitle(l.getString(l.getSelectedIndex()));
  int size=l.size();
        //多選項判斷方法一,遍歷isSelected
  for(int i=0;i<size;i++)
   if(l.isSelected(i))
    System.out.println(l.getString(i)+":  被選中");
  
  //多選項判斷方法二,使用getSelectedFlags獲取點擊狀態
  boolean[] bs=new boolean[size];
  l.getSelectedFlags(bs);
  for(int i=0;i<size;i++)
      if(bs[i])
       System.out.println(l.getString(i)+":  被選中");
  
  if(l.getSelectedIndex()==4||c==l.SELECT_COMMAND)
      t.setString(s1);
  if(l.getSelectedIndex()==5)
   t.setString(s2);
 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章