Code writing
After getting ready to work, we can finally start writing our Hello Android, before we begin to write code, we first understand a few files:
Res/layout/main.xml app main form layout file, what your app looks like here, has design and text two modes
Res/values/strings.xml can be understood as a i18n file, which is used to store the various strings that the program calls
Src/com/example/helloandroid/myactivity.java This is our main program class, and so on, the functions to be implemented are added in this file
First, add a textview with ID Hellotextview for your app and a button,mail.xml code with ID Hellobutton as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " > <textview android:layout_width=" fill_parent " android:layout_height=" 180DP " android:text=" @string/default_message " android:id=" @+id/hellotextview "android:textcolor=" #00ff00 " android:gravity= "center"/> <button android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "@string/button_send" android:id= "@+id/hellobutton" Android:layout _gravity= "Center"/></linearlayout>
The strings used by the code and controls are defined as follows:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >helloandroid by hiwanz</string> <string name= "button_send" >say something</string> <string name= " Default_message ">click button below!</string> <string name=" Interact_message ">You just clicked on The button!</string></resources>
The main program defines a button click to change the text displayed by the TextView, and a toast prompt message appears with the following code:
Package Com.example.helloandroid;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.widget.button;import Android.widget.textview;import Android.widget.toast;public class MyActivity Extends Activity {/** * Called when the activity is first created. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Get button Instance hellobtn = (Button) Findviewbyid (R.id.hellobutton); Set the Listener button click event Hellobtn.setonclicklistener (New View.onclicklistener () {@Override public void O Nclick (View v) {//Get TextView instance TextView Hellotv = (TextView) Findviewbyid (R.id.hellotextview ); The popup Toast prompt button was clicked on Toast.maketext (Myactivity.this, "Clicked", Toast.length_short). Show (); Read the interact_message information defined by the Strings.xml and write to TextView Hellotv.settext (R. string.interact_message); } }); }}
After the code is written, the computer is connected to the phone via a USB cable, the developer option in the phone system setup opens the USB debug, and the IDE directly points to run to see the effect on the phone.
Android app Development Getting Started tutorial-button