Python is a dynamic language, relatively concise. Android does not directly support the development of applications using Python, and requires the use of other middleware or libraries. Pythonforandroid provides support for the Python language on the Android platform; CLE supports the interaction between Python and Java, while providing a common interface that can be used in many other languages. Wrapandroid Project encapsulates the Android class as a CLE object so that multiple languages can invoke the Android class. Using these three components, you can use Python to develop interface applications directly on the Android platform. The Wrapandroid project is in progress, and the current version of 0.8.5 already offers a package of most Android classes except Sqlite,opengl. You can write a standalone Python application entirely.
Develop Python applications on Android, and programming and packaging environments can use eclipse directly. 1. Preparation Environment
A: Install pythonforandroid:http://code.google.com/p/android-scripting
B:cle when the program is running, automatically installs from the network, also may download the library function, puts in the project. Java library file Starcore_android_r6.jar is required for development. The file is in Starcore_devfiles_r6.zip and can be downloaded from http://code.google.com/p/cle-for-android
C:wrapandroid Project library file Wrapandroid.jar, included in the compressed package http:/code.google.com/p/wrapandroid-for-multilaguage/download/ In Wrapandroid_devfiles_0_8_6.rar. 2. Start Programming
A. Open eclipse and create a new Android project "Introduction"
B. If you are installing from a network, you need to add permission permissions:
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
C. Starcore_android_r6.jar and Wrapandroid.jar the library files to the engineering directory, add the two files to the project, as shown below:
D. Edit Introductionactivity.java, modify as follows, load Python code
Import com.srplab.wrapandroid.*;
public class Introductionactivity extends Wrapandroidactivity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Staractivity._call ("Doassetsfile", "Python", "code.py");
}
}
If you do not want to install CLE from the network, you can include the CLE shared library files in the project as follows:
Set the download flag to false in the activity at the same time
public class Introductionactivity extends Wrapandroidactivity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Downloadfromnetflag = false;
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Staractivity._call ("Doassetsfile", "Python", "code.py");
}
}
Only at this time the resulting installation package is larger.
E. Editorial Layout:main.xml.
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/widget73"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<textview
Android:id= "@+id/widget45"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>
<button
Android:id= "@+id/widget74"
Android:layout_width= "220DP"
android:layout_height= "48DP"
android:text= "Thank for your"
Android:typeface= "Serif"
Android:textstyle= "Bold"
Android:textcolor= "#ffff0000"
android:layout_x= "284DP"
Android:layout_y= "220DP"
Android:textsize= "16DP"
/>
</LinearLayout>
F. Create code.py files in the assets directory. 3. The code.py code is as follows
A. Getting the current service
Srvgroup = Libstarpy._getsrvgroup ()
Service = Srvgroup._getservice ("", "")
B. Getting the current activity
Staractivity = Service.ActivityClass.getCurrent ();
C. Getting the objects defined in layout
MyText = Staractivity.findviewbyid ("Textviewclass", Staractivity.getresource ("id/widget45"));
The Findviewbyid function differs from the standard Android function by adding the name of the class to the input parameter, the class name being the Android class name, and the "class" suffix.
Mytext.settext ("from layout");
MyButton = Staractivity.findviewbyid ("Buttonclass", Staractivity.getresource ("id/widget74"));
To define a click event for a button object
def mybutton_onclick (self, Ev):
Service.toastclass._new (). Maketext ("button is click", 0). Show ();
Mybutton.onclick = Mybutton_onclick;
Mybutton.setonclicklistener ();
Setonclicklistener and Android functions are different, do not need to input listener, the event triggered after the incident to the team as a result.
D. Dynamically creating objects
Get LinearLayout layout
Mylinearlayout = Staractivity.findviewbyid ("Linearlayoutclass", Staractivity.getresource ("id/widget73"));
To create a dynamic button
Mydynabutton = Service.buttonclass._new (mylinearlayout);
def mydynabutton_onclick (self, Ev):
Service.toastclass._new (). Maketext ("button is click", 0). Show ();
Mydynabutton.onclick = Mydynabutton_onclick;
Mydynabutton.setonclicklistener ();
Mydynabutton.settext ("created dynamically");
Set the layout parameters for a button
Mydynabutton.setlinearlayoutparams (100,50);
The results of the operation are as follows:
Example Downloads:
Http://wrapandroid-for-multilanguage.googlecode.com/svn/wiki/examples/pythongui85_introduction.rar