android 手机 usb连电脑 python 远程调试脚本 生成界面
(2012-12-24 09:41:34)
标签:
杂谈 |
分类: python/GAE/android[转] |
Introduction
The full screen
interface is a new, experimental feature that allows you to define
and display a Layout, and respond to button clicks and other key
events. This feature is available with
the
Update:
Details
Basically, you
take a screen layout as built by the android layout designer, and
call
This will display the screen. Button clicks and keypresses (include the BACK and Volume keys) will be returned as events "click" and "key" respectively.
You can query the settings for a given controil using fullQueryDetail, or list all controls using fullQuery.
fullSetProperty
Because I had to write my own parsing routines for the view (Android heavily preprocesses existing layouts in the compiler) I don't promise to support every possible property, but extensive use of reflection means it will give most things a serious shot.
Layouts
See
Querying the controls
One you've show the screen using "fullShow", you can query the current state of the controls, provided they have an id defined.
fullQueryDetail (id)
Properties returned are:
id, type, text, visibility, checked and tag.
More can be included on request.
In the sample code below, the line:
droid .fullQueryDetail("editText1").result
should return:
{u'id': u 'editText1','text':
u u '123456789','tag':
u u 'Tag Me','type':
u u 'EditText',
'visibility': u u '0'}
Setting Properties
You can use the fullSetProperty command to set properties of your controls, ie:
droid .fullSetProperty("textView1","text","Other stuff here")
Properities definitely available include "text", "visible" and "checked", but you should be able to set most simple properties. A property in this case is defined as any method the control has that starts with a "set". As an example (taken from example code below):
droid .fullSetProperty("background","backgroundColor","0xff7f0000")
If you look
at
You should be able to set most simple properties that way.
When defining a property, the layout inflater will recognize standard resources, ie:
droid .fullSetProperty("imageView1","src","@android:drawable/star_big_off")
Note that you can only use resources already compiled into sl4a or the standard android package. In theory you could use resources from other packages, but I haven't tried it. Full form of resource property is
@[package:]type/name
If you don't include a package id, it will default to the current package, typically sl4a. If you use the standalone template you should be able to include your own resources in there.
Images
Images can be included in the layout using "ImageView" controls. The property to set to display the image is "src". This can either be a resource from somewhere, or a file url. ie:
droid.fullSetProperty("imageView1","src","@android:drawable/star_big_off")
droid.fullSetProperty("imageView1","src","file:///sdcard/download/panda72.png")
Colors
When setting colors, you have to define them as either #aarrggbb or 0xaarrggbb. The aa (alpha) is important. For solid colors, this should be set to ff. Ie, black=#ff000000, red=#ffff0000.
As of r5x09,
colors should also recognizes #rrggbb, #rgb and #argb ans behave
sensibly. Also, as a convenience, all the standard html color names
are recognized. See:
Background
The background property can take either a color, a resource, or a bitmap file as a value.
droid.fullSetProperty("button1","background","file:///sdcard/download/panda72.png")
droid.fullSetProperty("button1","background","@android:drawable/star_big_off")
droid.fullSetProperty("button1","background","#ff00ff00")
Lists
You can associate a list of items with a Spinner or a ListView using fullSetList.
droid.fullSetList("listview1",["One","Two","Three"])
droid.fullSetList("spinner1",["Red","Green","Blue"])
ListViews will throw an "itemclick" event when an item is selected. Spinners don't yet throw a "click" event, but the selected item will be returned in !selectedPosition.
Responding to Events
Any control that supports the clickable interface will generate a "click" event when tapped. The basic properties of the control will be returned in data.
A button
(including Back, Search and the volume controls) will generate a
"key" event. The data will contain a map with the keycode (as
defined here:
Some useful keycodes: BACK=4, Search=84, volume up=24, volume down=25, and menu=82. Arrow keys/trackball: up=19, left=20, right=21 and down=22
Some keys (ie, the
volume keys) have a default behaviour, such as changing the
ringtone volume. You can override this behaviour by
callingfullKeyOverride
droid .fullKeyOverride([24,25],True)
Options Menu
If you have an
options menu defined (using
Debugging
fullDisplay and fullSetProperty both return messages detailing any errors with the layout or properies, including any unrecognized properties. If asking for more features, please include sample code and these debugging responses.
Example Code
importandroid =android.Android()
droid
defeventloop ():
while True:
event=droid.eventWait().result
event
if event["name"]=="click":
=event["data"]["id"] id
if id =="button3":
return
elif id =="button2":
.fullSetProperty("editText1","text","OK has been pressed") droid
elif id =="button1":
.fullSetProperty("textView1","text","Other stuff here") droid
droid .fullSetProperty("background","backgroundColor","0xff7f0000")
elif event["name"]=="screen":
if event["data"]=="destroy":
return
"Started"
layout="""<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
:id="@+id/background" android
:orientation="vertical" android android :layout_width="match_parent"
:layout_height="match_parent" android android :background="#ff000000">
<LinearLayout android :layout_width="match_parent"
:layout_height="wrap_content" android android :id="@+id/linearLayout1">
<Button android :id="@+id/button1"android :layout_width="wrap_content"
:layout_height="wrap_content" android android :text="Test 1"></Button>
<Button android :id="@+id/button2"android :layout_width="wrap_content"
:layout_height="wrap_content" android android :text="Ok"></Button>
<Button android :id="@+id/button3"android :layout_width="wrap_content"
:layout_height="wrap_content" android android :text="Cancel"></Button>
</LinearLayout>
textView1" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical|center_horizontal|center"></TextView> <TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="TextView"
android:id="@+id/
match_parent" <EditText android:layout_width="
wrap_content" android:id="@+id/editText1" android:layout_height="
Tag android:tag=" Me" android:inputType="textCapWords|textPhonetic|number">
wrap_content" android:id="@+id/checkBox1" android:layout_width="234dp" android:text="Howdy,neighbors." android:checked="true"></CheckBox> <requestFocus></requestFocus>
</EditText>
<CheckBox android:layout_height="
</LinearLayout>
"""layout printdroid .fullShow(layout)
eventloop()droid .fullQuery()"Data entered =",droid.fullQueryDetail("editText1").result
droid.fullDismiss()
Mini FAQ
Why is my screen transparent? You need to define a background color for your root layout, including the alpha. ie,
android:background="#ff000000"
(Actually, as of 5x13, the color values do not require an explicit alpha value)
TODO
- A function to query ALL properties, not just the basic ones.
- Including an options
menu
(and perhaps context menus?) - Ability to override standard actions on keypress.
SL4A API Help -UiFacade
indexUser Interface Facade.
Usage Notes
The UI facade provides access to a selection of dialog boxes for general user interaction, and also hosts the
The general use of the dialog functions is as follows:
- Create a dialog using one of the following calls:
- Set additional features to your dialog
-
dialogSetItems
Set a list of items. Used like a menu. -
dialogSetMultiChoiceItem
s Set a multichoice list of items. -
dialogSetSingleChoiceIte
ms Set a single choice list of items. -
dialogSetPositiveButtonT
ext -
dialogSetNeutralButtonTe
xt -
dialogSetNegativeButtonT
ext -
dialogSetMaxProgress
Set max progress for your progress bar.
-
dialogSetItems
- Display the dialog using
dialogShow - Update dialog information if needed
- Get the results
- Using
dialogGetResponse, which will wait until the user performs an action to close the dialog box, or - Use eventPoll to wait for a "dialog" event.
- You can find out which list items were selected
using
dialogGetSelectedItems, which returns an array of numeric indices to your list. For a single choice list, there will only ever be one of these.
- Using
- Once done, use
dialogDismiss to remove the dialog.
You can also manipulate menu options. The menu options are available for both
Some notes:
Not every dialogSet function is relevant to every dialog type, ie, dialogSetMaxProgress obviously only applies to dialogs created with a progress bar. Also, an Alert Dialog may have a message or items, not both. If you set both, items will take priority.
In addition to the above functions,
There is only ever one instance of a dialog. Any dialogCreate call will cause the existing dialog to be destroyed.
@author MeanEYE.rcf (meaneye.rcf@gmail.com)
addContextMenuItem | Adds a new item to context menu. label (String) event (String) eventData (Object) Context menus are used primarily with |
addOptionsMenuItem | Adds a new item to options menu. label (String) event (String) eventData (Object) iconName (String) Example (python) import android droid=android.Android() droid.addOptionsMenuItem("Silly","silly",None,"star_on") droid.addOptionsMenuItem("Sensible","sensible","I bet.","star_off") droid.addOptionsMenuItem("Off","off",None,"ic_menu_revert") print "Hit menu to see extra options." print "Will timeout in 10 seconds if you hit nothing." while True: # Wait for events from the menu. response=droid.eventWait(10000).result if response==None: break print response if response["name"]=="off": break print "And done." |
clearContextMenu | Removes all items previously added to context menu. |
clearOptionsMenu | Removes all items previously added to options menu. |
dialogCreateAlert | Create alert dialog. title (String) message (String) Example (python) import android droid=android.Android() droid.dialogCreateAlert("I like swords.","Do you like swords?") droid.dialogSetPositiveButtonT |
dialogCreateDatePicker | Create date picker dialog. year (Integer) month (Integer) day (Integer) |
dialogCreateHorizontalPr |
Create a horizontal progress dialog. title (String) message (String) maximum progress (Integer) |
dialogCreateInput | Create a text input dialog. title (String) message (String) defaultText (String) inputType (String) For inputType, see |
dialogCreatePassword | Create a password input dialog. title (String) message (String) |
dialogCreateSeekBar | Create seek bar dialog. starting value (Integer) maximum value (Integer) title (String) message (String) Will produce "dialog" events on change, containing:
|
dialogCreateSpinnerProgr |
Create a spinner progress dialog. title (String) message (String) maximum progress (Integer) |
dialogCreateTimePicker | Create time picker dialog. hour (Integer) minute (Integer) is24hour (Boolean) |
dialogDismiss | Dismiss dialog. |
dialogGetInput | Queries the user for a text input. title (String) message (String) defaultText (String) The result is the user's input, or None (null) if cancel was hit. Example (python) import android droid=android.Android() print droid.dialogGetInput("Title","Message","Default").result |
dialogGetPassword | Queries the user for a password. title (String) message (String) |
dialogGetResponse | Returns dialog response. |
dialogGetSelectedItems | This method provides list of items user
selected. returns: (Set) |
dialogSetCurrentProgress |
Set progress dialog current value. current (Integer) |
dialogSetItems | Set alert dialog list items. items (JSONArray) This effectively creates list of options. Clicking on an item will immediately return an "item" element, which is the index of the selected item. |
dialogSetMaxProgress | Set progress dialog maximum value. max (Integer) |
dialogSetMultiChoiceItem |
Set dialog multiple choice items and
selection. items (JSONArray) selected (JSONArray) This creates a list of check boxes. You can select multiple items out of the list. A response will not be returned until the dialog is closed, either with the Cancel key or a button (positive/negative/neutral). Use |
dialogSetNegativeButtonT |
Set alert dialog button text. text (String) |
dialogSetNeutralButtonTe |
Set alert dialog button text. text (String) |
dialogSetPositiveButtonT |
Set alert dialog positive button text. text (String) |
dialogSetSingleChoiceIte |
Set dialog single choice items and selected
item. items (JSONArray) selected (Integer) This creates a list of radio buttons. You can select one item out of the list. A response will not be returned until the dialog is closed, either with the Cancel key or a button (positive/negative/neutral). Use |
dialogShow | Show dialog. |
fullDismiss | Dismiss Full Screen. |
fullKeyOverride | Override default key actions keycodes (JSONArray) enable (Boolean) This will override the default behaviour of keys while in the fullscreen mode. ie: droid.fullKeyOverride([24,25],True)This will override the default behaviour of the volume keys (codes 24 and 25) so that they do not actually adjust the volume. Returns a list of currently overridden keycodes. |
fullQuery | Get Fullscreen Properties |
fullQueryDetail | Get fullscreen properties for a specific
widget id (String) |
fullSetList | Attach a list to a fullscreen widget id (String) list (JSONArray) |
fullSetProperty | Set fullscreen widget property id (String) property (String) value (String) |
fullSetTitle | Set the Full Screen Activity Title title (String) |
fullShow | Show Full Screen. layout (String) title (String) See |
webViewShow | Display a WebView with the given URL. url (String) wait (Boolean) See |