📄 newphoneui.java
字号:
package com.hziee.phone_book.ui;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.AlertType;
import com.hziee.phone_book.model.*;
/**
* 该类是添加新记录的一个界面类
* 继承于Form类
*/
public class NewPhoneUI extends Form implements CommandListener,
ItemStateListener
{
private UIController uicontroller;//界面控制对象
private TextField nameField;
private ChoiceGroup choice;
private TextField mobileField;
private TextField phoneField;
private TextField emailField;
private int phoneIndex;
private int emailIndex;
/*保存和返回命令*/
public static final Command saveCommand = new Command("保存", Command.OK, 2);
public static final Command backCommand = new Command("返回", Command.BACK, 3);
public NewPhoneUI(UIController uicontroller)
{
super(Title.add_record);//这里的super()调用基类的构造函数
this.uicontroller = uicontroller;//在newphoneUI中定义uicontroller
/**TextField类的四个参数
* 1.表示标题,2.初始内容,3.允许输入的最大字符数,4.限制类型
*这里TextField.any任何类型的
*/
nameField = new TextField(Title.name, null, 25, TextField.ANY);
mobileField = new TextField(Title.mobile, null, 25,
TextField.PHONENUMBER);//电话类型
/**
* ChoiceGroup.EXCLUSIVE、ChoiceGroup.MULTIPLE两种模式,
* 分别支持单选和多选
*/
choice = new ChoiceGroup(null, ChoiceGroup.MULTIPLE);
phoneField = new TextField(Title.phone, null, 25, TextField.PHONENUMBER);
emailField = new TextField(Title.email, null, 25, TextField.EMAILADDR);//邮件类型
choice.append(Title.detail+"cccccccc", null);//详细信息
this.append(nameField);
this.append(mobileField);
this.append(choice);
this.addCommand(saveCommand);
this.addCommand(backCommand);
this.setCommandListener(this);
this.setItemStateListener(this);
}
//clear方法将姓名和手机号码设置为空
public void clear()
{
nameField.setString("");
mobileField.setString("");
if (choice.isSelected(0))
{
phoneField.setString("");
emailField.setString("");
}
}
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
if (arg0 == backCommand)
{
uicontroller.handleEvent(UIController.EventID.EVENT_NEWPHONE_BACK_MAINUI);
} else if (arg0 == saveCommand)
{ //按下保存键,将name保存
String userName = nameField.getString();
if (userName.length() == 0)//长度为0,则提出警告
{
uicontroller.showAlert(Title.userNameNull, this,
AlertType.WARNING);
return;
}
//保存mobilephone
String mobilePhone = mobileField.getString();
if (mobilePhone.length() == 0)
{
uicontroller.showAlert(Title.mobilePhoneNull, this,
AlertType.WARNING);
return;
}
String phone = "";
String email = "";
if (choice.isSelected(0))
{//如果choice被选中,保存电话和邮件地址
phone = phoneField.getString();
email = emailField.getString();
}
Account newAccount = new Account(userName, mobilePhone, phone,
email);//数据转换
uicontroller.handleEvent(
UIController.EventID.EVENT_SAVE_RECORD_SELECTED,
new Object[] { newAccount });
}
}
public void itemStateChanged(Item item)
{
if (item == choice)
{
if (choice.isSelected(0))
{
phoneIndex = this.append(phoneField);
emailIndex = this.append(emailField);
} else
{
this.delete(emailIndex);
this.delete(phoneIndex);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -