python编写android程序_使用 Python 编写 Android 的 UI 应用

这篇博客介绍了如何使用Python编写Android应用程序,特别是涉及到GUI部分,包括按钮事件处理、滚动视图、菜单创建、对话框的展示等。通过具体的代码示例,展示了如何创建和操作Android界面元素。
摘要由CSDN通过智能技术生成

http://www.codeproject.com/Articles/377432/Writing-an-Android-GUI-using-Python

1.[代码]按钮

MyButtonIndex = 1;

//#set onClick event listener

def MyButton_onClick(self,Ev) :

global MyButtonIndex;

//#Create a new button in scroll view

b = Service.ButtonClass._New(MyScrollLayout);

//#Set text content and color

b.setText("Button" + str(MyButtonIndex));

b.setTextColor(0xFFFF0000);

b.setLinearLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);

MyButtonIndex = MyButtonIndex + 1;

return;

//#assign event listener to object

MyButton.onClick = MyButton_onClick;

2.[代码]ScrollView

MyScrollLayout = Service.LinearLayoutClass._New(MyScroll);

MyScrollLayout.setOrientation("VERTICAL");

MyScrollLayout.setFrameLayoutParams(Service.FILL_PARENT,Service.WRAP_CONTENT);

3.[代码]onCreateOptionsMenu

def StarActivity_onCreateOptionsMenu(self,menu) :

menu.add1(0, 1, 1, "open");

menu.add1(0, 2, 2, "edit");

menu.add1(0, 3, 3, "update");

menu.add1(0, 4, 4, "clode");

return True;

StarActivity.onCreateOptionsMenu = StarActivity_onCreateOptionsMenu;

4.[代码]onPrepareOptionsMenu

def StarActivity_onPrepareOptionsMenu(self,menu) :

return True;

StarActivity.onPrepareOptionsMenu = StarActivity_onPrepareOptionsMenu;

5.[代码]onOptionsItemSelected

def StarActivity_onOptionsItemSelected(self,menuitem) :

id = menuitem.getItemId();

if( id == 1 ) :

self.setTitle("Open Text!");

if( id == 2 ) :

self.setTitle("Edit Text!");

if( id == 3 ) :

self.setTitle("Update Text!");

if( id == 4 ) :

self.setTitle("Close Text!");

return True;

StarActivity.onOptionsItemSelected = StarActivity_onOptionsItemSelected;

6.[代码]点击按钮打开对话框

OpenPopWindowButton = Service.ButtonClass._New(ButtonLayout);

OpenPopWindowButton.setText("Open popup window");

OpenPopWindowButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);

def OpenPopWindowButton_onClick(self, Ev):

return;

OpenPopWindowButton.onClick = OpenPopWindowButton_onClick

7.[代码]创建对话框

def OpenPopWindowButton_onClick(self, Ev):

//#create popup window

MyPopupWindow = Service.PopupWindowClass._New()

//#set onDismiss event listener

def MyPopupWindow_onDismiss(self, Ev):

Service.ToastClass._New().makeText("PopupWindow is dismiss",0).show();

return;

MyPopupWindow.onDismiss = MyPopupWindow_onDismiss;

//#create root layout for popup window

PopupLayout = Service.LinearLayoutClass._New();

PopupLayout.setBackgroundColor(0xFF0000FF) #--blue

//#create a button

MyButton = Service.ButtonClass._New(PopupLayout);

def MyButton_onClick(self, Ev) :

//#when is clicked, we show information and close the popup window

Service.ToastClass._New().makeText("Button is click",0).show();

MyPopupWindow.dismiss();

return;

MyButton.onClick = MyButton_onClick;

MyButton.setText("CloseWindow");

MyButton.setLinearLayoutParams(Service.WRAP_CONTENT,Service.WRAP_CONTENT);

//#assign layout to popup window.

MyPopupWindow.setContentView(PopupLayout);

MyPopupWindow.setWidth(200);

MyPopupWindow.setHeight(200);

MyPopupWindow.setFocusable(True);

MyPopupWindow.showAtLocation(self,17,0,0);

//#prevent garbage collected by python.

MyPopupWindow._LockGC();

return;

8.[图片] 屏幕截图

9.[文件] pythongui_menu_scrollview_popupwindow.rar ~ 791KB     下载(191)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值