鞋子哥哥永垂不朽吧 关注:4,444贴子:48,639
  • 0回复贴,共1

Python写的安卓电话本

只看楼主收藏回复

自己写了一个Python 电话簿,
程序很垃圾,有太多地方需要改进,大家一起讨论一下:
import sys
import android
import types
import time
import re
droid=android.Android()
#address={ "zhangsan":8061506, "lisi": 13325041633 };
def show_result_dialog(title):
droid.dialogCreateAlert(title)
droid.dialogSetPositiveButtonText("YES")
droid.dialogSetNegativeButtonText("NO")
droid.dialogShow()
response = droid.dialogGetResponse().result
droid.dialogDismiss()
global result_file
def open_file():
html_head = '''<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PhoneBook</title>
<style type="text/css">
</style>
</head>
<body>
<table border = "1">
</table>
</body>
</html>'''
try :
result_file = open('/sdcard/SL4A_test_result.html', 'w')
result_file.writelines(html_head)
except IOError, e:
print e
exit
result_file.close()
def open_find():
html_head = '''<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PhoneBook</title>
<style type="text/css">
</style>
</head>
<body>
<table border = "1">
</table>
</body>
</html>'''
try :
find = open('/sdcard/SL4A_phonebook_find.html', 'w')
find.writelines(html_head)
except IOError, e:
print e
exit
find.close()
list_find = []
matchPattern = re.compile(name)
file_find = open('/sdcard/SL4A_test_result.html','r')
while 1:
line = file_find.readline()
if not line:
break
elif matchPattern.search(line):
list_find.append(line)
else:
pass
file_find.close()
f_find=open('/sdcard/SL4A_phonebook_find.html', 'r')
a=f_find.readlines()
list2=list(a)
f_find.close()
b=str(list_find)
list2[-3:-3]=[b]
f1_find=open('/sdcard/SL4A_phonebook_find.html', 'w')
f1_find.writelines(list2)
f1_find.close()
def add_file():
f=open('/sdcard/SL4A_test_result.html', 'r')
a=f.readlines()
f.close()
lis=list(a)
b='<tr><td>'+'name'+'</td><td>' + name +'</td><td>Phone</td><td>' + phone + '</td></tr>\n'
lis[-3:-3]=[b]
f1=open('/sdcard/SL4A_test_result.html', 'w')
f1.writelines(lis)
f1.close()
def del_file():
lis = []
matchPattern = re.compile(name)
#r=re.match("abcd", line)
fil = open('/sdcard/SL4A_test_result.html','r')
while 1:
line = fil.readline()
if not line:
break
elif matchPattern.search(line):
pass
else:
lis.append(line)
fil.close()
fi = open('/sdcard/SL4A_test_result.html', 'w')
for i in lis:
fi.write(i)
fi.close()
while 1:
item = droid.dialogGetInput('选择相关信息',"第一次使用'Enable' 添加联系人'Add'\n删除联系人'Del' 查看联系人'See'\n查找联系人'Find' 查看帮助'Help'" ,'').result
#for item in items:
if item=='Enable':
open_file()
if item=='Add':
name=droid.dialogGetInput('请输入姓名:').result
phone=droid.dialogGetInput('请输入号码:').result
add_file()
# address[name]=phone;
# address={name:phone}
elif str(item)=='Del':
name=droid.dialogGetInput('请输入姓名:').result
del_file()
elif item=='Help':
show_result_dialog('对不起,帮助文档为空!\n请联系管理员。')
time.sleep(5)
elif item=='See':
#close_file()
droid.viewHtml('/sdcard/SL4A_test_result.html')
break
elif item=='Find':
name=droid.dialogGetInput('请输入姓名:').result
open_find()
droid.viewHtml('/sdcard/SL4A_phonebook_find.html')
break
continue


来自Android客户端1楼2014-05-05 12:35回复