itemstatelistenerdemo.java

来自「Java ME手机应用开发大全一书的配套光盘上的源码」· Java 代码 · 共 53 行

JAVA
53
字号
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
//MIDlet类实现CommandListener和ItemStateListener接口,构造监听器类监听Command事件和itemStateChanged事件
public class ItemStateListenerDemo extends MIDlet implements CommandListener,ItemStateListener{

  private Display display;
  private Form form; 
  private Command exit; 
  //声明2个TextField对象
  private TextField text1,text2; 
  public ItemStateListenerDemo()
  {
   display = Display.getDisplay(this);
   exit = new Command("退出", Command.EXIT, 1);
   //根据不同的约束条件,实例化这2个TextField对象
   text1 = new TextField("输入", "", 30, TextField.ANY);
   text2= new TextField("输入的是", "", 30, TextField.ANY);
   form = new Form("ItemStateListener接口测试");    
   form.addCommand(exit);
   //将TextField添加到Form表单上  
   form.append(text1);
   form.append(text2);
   form.setCommandListener(this);
   //向表单添加ItemStateListener监听器,参数为this
   form.setItemStateListener(this);
  }
  public void startApp()
  {
   display.setCurrent(form);
  }
  public void pauseApp()
  { 
  }
  public void destroyApp(boolean unconditional)
  { 
  }
  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exit)
   {
      destroyApp(false);
      notifyDestroyed();
   }
  }
  public void itemStateChanged(Item item)
	{
	  if(item==text1)
		{
		  text2.setString(text1.getString());
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?