欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

blackberry系统ui学习一

更新时间: 2014-01-05 02:42:14 责任编辑: Author_N1

 

BlackBerry系统UI学习1

先看个例子

package com.hoocy;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class UserInterface extends UiApplication {
	public static void main(String[] args) {
		UserInterface theApp = new UserInterface();
		theApp.enterEventDispatcher();
	}

	public UserInterface() {
		pushScreen(new UserInterfaceScreen());
	}
}

final class UserInterfaceScreen extends MainScreen {
	// 水平布局管理器
	HorizontalFieldManager _fieldManagerTop;
	// 垂直布局管理器
	VerticalFieldManager _fieldManagerMiddle;
	HorizontalFieldManager _fieldManagerBottom;

	//
	BitmapField _bitmap;
	Bitmap _canadaImage, _ukImage, _usImage;

	LabelField _label;
	BasicEditField _input;
	String _canadaCapital, _ukCapital, _usCapital, _capital;
	int displayed = 0;

	public UserInterfaceScreen() {
		super();
		LabelField title = new LabelField("User Interface Sample",
				LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		setTitle(title);
		_fieldManagerTop = new HorizontalFieldManager();
		_fieldManagerMiddle = new VerticalFieldManager();
		_fieldManagerBottom = new HorizontalFieldManager();
		add(_fieldManagerTop);
		add(new SeparatorField());
		add(_fieldManagerMiddle);
		add(new SeparatorField());
		add(_fieldManagerBottom);
		_canadaImage = Bitmap.getBitmapResource("canada.png");
		_ukImage = Bitmap.getBitmapResource("uk.png");
		_usImage = Bitmap.getBitmapResource("us.png");
		_bitmap = new BitmapField();
		_bitmap.setBitmap(_canadaImage);

		_fieldManagerTop.add(_bitmap);
		_canadaCapital = "Ottawa";
		_ukCapital = "London";
		_usCapital = "Washington";
		_capital = _canadaCapital;

		_label = new LabelField("Please press a button!");
		_input = new BasicEditField("Capital (can be changed): ", _capital);
		_fieldManagerMiddle.add(_label);
		_fieldManagerMiddle.add(_input);

		FieldChangeListener listenerCanada = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				_bitmap.setBitmap(_canadaImage);
				_input.setText(_canadaCapital);
				displayed = 0;
			}
		};
		
		FieldChangeListener listenerUK = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				_bitmap.setBitmap(_ukImage);
				_input.setText(_ukCapital);
				displayed = 1;
			}
		};
		FieldChangeListener listenerUS = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				_bitmap.setBitmap(_usImage);
				_input.setText(_usCapital);
				displayed = 2;
			}
		};
		ButtonField canadaButton = new ButtonField("Canada");
		ButtonField ukButton = new ButtonField(" UK ");
		ButtonField usButton = new ButtonField(" USA ");
		//设置事件给不同的按钮
		canadaButton.setChangeListener(listenerCanada);
		ukButton.setChangeListener(listenerUK);
		usButton.setChangeListener(listenerUS);
		_fieldManagerBottom.add(canadaButton);
		_fieldManagerBottom.add(ukButton);
		_fieldManagerBottom.add(usButton);
	}

	// 增加主菜单只需要复写方法即可
	protected void makeMenu(Menu menu, int instance) {
		menu.add(_changeCapital);
		menu.add(_close);
	}

	// 菜单对象 1
	private MenuItem _changeCapital = new MenuItem("Change Capital", 110, 10) {
		public void run() {
			if (displayed == 0)
				_canadaCapital = _input.getText();
			else if (displayed == 1)
				_ukCapital = _input.getText();
			else if (displayed == 2)
				_usCapital = _input.getText();
		}
	};
	// 菜单对象 2
	private MenuItem _close = new MenuItem("Close", 110, 10) {
		public void run() {
			onClose();
		}
	};
	
	//关闭事件
	public boolean onClose() {
		Dialog.alert("Goodbye!");
		System.exit(0);
		return true;
	}
}
 
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:42:14
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/13379/
本文WWW.DOC100.NET DOC100.NET版权所有。