Skip to content

Instantly share code, notes, and snippets.

@pandako
Created April 14, 2011 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pandako/919013 to your computer and use it in GitHub Desktop.
Save pandako/919013 to your computer and use it in GitHub Desktop.
Titanium mobile で作った ListCulculator のソースコード
// 何も無い時の背景色
Ti.UI.setBackgroundColor('#FFF');
// windowを作成
var win = Titanium.UI.createWindow({
title:'ListCalculator',
fullscreen:false,
width:"auto",
height:"auto",
backgroundColor:'#fff',
exitOnClose: true
});
//演算子選択ダイアログ
var od = Ti.UI.createOptionDialog({
title: 'Select operator',
rowID:0,
myParent:"",
options: ['+','-','×','÷'],
cancel:-1
});
od.addEventListener('click',function(e){
//該当の行の演算子選択ボタンを変更する
var p = e.source.myParent;
var i = e.index;
var f = "btn_plus";
if (i == 0) {
} else if (i == 1){
f = "btn_minus";
} else if (i == 2){
f = "btn_multi";
} else if (i == 3){
f = "btn_div";
}
p.backgroundFocusedImage = f+"1.png";
p.backgroundImage = f+".png";
p.backgroundSelectedImage = f+"2.png";
p.value = i;
});
//入力欄の作成====================================
//row(行)と各control(textFieldとかbuttonとか)を紐付けるためのID
var staticNum = 0;
//tableViewの作成
var tbl = Ti.UI.createTableView({
data:[{title:""}],
touchEnabled:false,
focusable:false,
bottom:50,
backgroundImage:"bg.png",
width:"auto",
height:"auto"
});
//rowを追加する関数を実行
//2行無いと計算にならないので2回実行
addRow(0);
addRow(0);
//tableViewをwindowに追加
win.add(tbl);
//合計欄の作成====================================
var answer = Ti.UI.createView({
//backgroundColor:"#CEEBC0",
backgroundImage:"bg2.png",
opacity:0.8,
borderWidth:1,
borderColor:"#5F9342",
borderRadius:5,
layout:'horizontal',
bottom:0,
width:"auto",
height:50
});
//計算実行ボタン作成
var re = Ti.UI.createButton({
width:48,
height:48,
backgroundFocusedImage:"btn_equal1.png",
backgroundImage:"btn_equal.png",
backgroundSelectedImage:"btn_equal2.png"
});
re.addEventListener('click', function(e){
getAnswer();
});
//計算結果フィールド作成
var total = Ti.UI.createLabel({
text:"0",
font:{fontSize:24},
width:200,
height:48,
left:5,
color:"#000"
});
//合計欄に追加追加ぁ!
answer.add(re);
answer.add(total);
win.add(answer);
//windowオープン====================================
win.open();
//rowの追加関数====================================
function addRow(n) {
//row作成
var row = Ti.UI.createTableViewRow({
rowID:staticNum,
height:'auto',
focusable:false,
layout:'horizontal'
});
//演算子選択ボタン作成
var sel = Ti.UI.createButton({
rowID:staticNum,
backgroundFocusedImage:"btn_plus1.png",
backgroundImage:"btn_plus.png",
backgroundSelectedImage:"btn_plus2.png",
value:0,
width:48,
height:48,
title:""
});
sel.addEventListener('click', function(e){
//押されたボタンと同じrowIDを持つ行を探す。
//もっとスマートなやり方がありそう…
for (var i=0;i<tbl.data[0].rows.length;i++){
if (tbl.data[0].rows[i].rowID == e.source.rowID){
//rowIDを渡してから演算子選択ダイアログを表示
od.rowID = e.source.rowID;
od.myParent = e.source;
od.selectedIndex = e.source.value;
od.show();
break;
}
}
//getAnswer();//この時点では正しく計算できない時がある(謎)
});
//数値入力フィールド作成
//エミュレータではAndroid 2.2 以上だとカンマとかマイナスとか入力できなかった
//1.6と実機のIS01(1.6)だと無問題
var tx = Ti.UI.createTextField({
keyboardType:Titanium.UI.KEYBOARD_NUMBERS_PUNCTUATION,
rowID:staticNum,
value:"0",
width:130,
height:"auto"
});
//row追加ボタン作成
var plus = Ti.UI.createButton({
rowID:staticNum,
backgroundFocusedImage:"btn_add1.png",
backgroundImage:"btn_add.png",
backgroundSelectedImage:"btn_add2.png",
width:48,
height:48,
title:""
});
plus.addEventListener('click', function(e){
for (var i=0;i<tbl.data[0].rows.length;i++){
if (tbl.data[0].rows[i].rowID == e.source.rowID){
addRow(i);
break;
}
}
//getAnswer();//この時点では正しく計算できない時がある(謎)
});
//row削除ボタン作成
var del = Ti.UI.createButton({
rowID:staticNum,
backgroundFocusedImage:"btn_del1.png",
backgroundImage:"btn_del.png",
backgroundSelectedImage:"btn_del2.png",
width:48,
height:48,
title:""
});
del.addEventListener('click', function(e){
if (tbl.data[0].rows.length <= 1) {
alert("これ以上消せないよ");
return;
}
for (var i=0;i<tbl.data[0].rows.length;i++){
if (tbl.data[0].rows[i].rowID == e.source.rowID){
tbl.deleteRow(i);
break;
}
}
//getAnswer();//この時点では正しく計算できない時がある(謎)
});
row.add(sel);
row.add(tx);
row.add(plus);
row.add(del);
//row追加
if (staticNum == 0) {
//最初のrow追加時にはinsertじゃダメ
tbl.updateRow(0,row);
}else{
tbl.insertRowAfter(n,row);
}
//ID更新
++staticNum;
}
//計算関数====================================
function getAnswer(){
var t = 0;
var r = tbl.data[0].rows;
for (var i=0;i<r.length;i++){
//演算子とtextFieldを参照
var o = r[i].children[0].value;
var v = r[i].children[1].value;
if (!isNaN(v)){
if (o == 0){
t += parseFloat(v);
} else if (o == 1) {
t -= parseFloat(v);
} else if (o == 2) {
t *= parseFloat(v);
} else if (o == 3) {
t /= parseFloat(v);
}
r[i].children[1].color = "#000";
} else {
//数値じゃないものは赤字にしてスルー
r[i].children[1].color = "#f00";
}
}
//小数点以下第三位で四捨五入
total.text = round2(t,1000);
}
//四捨五入関数================
function round2(n,c){
n *= c;
return String(Math.round(n) / c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment