Hi, Khách!
|
[Bài 9 List gugồ gọi là danh sách
Bắt đầu:
>>List là 1dạng danh sách trong đồ hoạ cấp cao
hiểu vậy thôi
>>cách khai báo 1List(chỉ cần 1 List là dc rồi)
tui thường dùng kiểu List.IMPLICIT
ngoài ra vẫn còn 1vài kiểu khác nhưng ko cần thiết
>tạo 1list rồi thì nó sẽ chưa có j cả, cta phải append vào thôi:
Cách thêm:
l.append("abc",hình ảnh);
như vậy là màn hình đã có thêm 1dòng "abc" kèm hình ảnh nếu có
>>thêm Command vào list:
l.addCommand(ok);
thêm vào như bt thôi
d.setCurrent(l);
//hiển thị list lên màn hình
XONG phần khởi tạo;
>>Cách dùng:
>cta sẽ dùng Command để sủ dụng nó
public class minh extends MIDlet implements CommandListener{
dùng List trong hàm
commandAction(Command cm,Displayable dp){
>>khai báo vị trí trong List:
int index=l.getSelectedIndex();
//index = vị trí trọn trong list
index=0 tương ứng với vị trí thứ nhất trong List
>if(index==0){
d.setCurrent(f1);
}
nếu chọn vị trí thứ 1 (index =0) thì ta cho hiển thị form 1 ứng với dòng "abc" trong List(dòng đầu tiên)
if(dp==f1&&cm==back){
d.setCurrent(l);
//đang ở form 1 bấm back để trở về List
còn các Command khác thì tùy các bạn
Ngoài ra còn 1số thao tác khác nhưng theo tui thỳ ko cần thiết và chưa bao giờ cần sử dụng
code//
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class minh extends MIDlet implements CommandListener{
Command ok,back,exit;
Display d;
Form f1,f2;
List l;
public minh(){
l=new List("test list",List.IMPLICIT);
f1=new Form("form1");
f2=new Form("form2");
d=Display.getDisplay(this);
ok=new Command("OK",4,1);
back=new Command("BACK",Command.BACK,1);
exit=new Command("EXIT",7,1);
f1.append("dòng 1 tương ứng form 1");
f1.addCommand(back);
f1.setCommandListener(this);
f2.append("dòng 2 tương ứng form 2 do ta qđịnh");
f2.addCommand(back);
f2.setCommandListener(this);
l.append("vào form1");
l.append("vào form2");
l.addCommand(ok);
l.addCommand(exit);
d.setCurrent(l);
}
public void startApp(){}// code ở hết minh() rồi nên startApp() tui để trống cũng ko sao
public void pauseApp(){}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command cm,Displayable dp){
int index=l.getSelectedIndex();
if(index==0)d.setCurrent(f1);
if(index==1)d.setCurrent(f2);
if(dp==l&&cm==exit){
destroyApp(true);
}
if(dp==f1&&cm==back||dp==f2&&cm==back){
d.setCurrent(l);
}
}
}
Copy code
import javax.microedition.midlet.*;
public class minh extends MIDlet implements CommandListener{
Command ok,back,exit;
Display d;
Form f1,f2;
List l;
public minh(){
l=new List("test list",List.IMPLICIT);
f1=new Form("form1");
f2=new Form("form2");
d=Display.getDisplay(this);
ok=new Command("OK",4,1);
back=new Command("BACK",Command.BACK,1);
exit=new Command("EXIT",7,1);
f1.append("dòng 1 tương ứng form 1");
f1.addCommand(back);
f1.setCommandListener(this);
f2.append("dòng 2 tương ứng form 2 do ta qđịnh");
f2.addCommand(back);
f2.setCommandListener(this);
l.append("vào form1");
l.append("vào form2");
l.addCommand(ok);
l.addCommand(exit);
d.setCurrent(l);
}
public void startApp(){}// code ở hết minh() rồi nên startApp() tui để trống cũng ko sao
public void pauseApp(){}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command cm,Displayable dp){
int index=l.getSelectedIndex();
if(index==0)d.setCurrent(f1);
if(index==1)d.setCurrent(f2);
if(dp==l&&cm==exit){
destroyApp(true);
}
if(dp==f1&&cm==back||dp==f2&&cm==back){
d.setCurrent(l);
}
}
}
Copy code
>>>Source:
Tải Về Listzip_tLxkt.zip (4.54 KB)
>>>Ảnh minh họa:

hãy tải source về xem rõ hơn
Trực Tuyến:
Khách: 1






