Android, my first contact!

Source: Internet
Author: User

I have been fighting for 10 hours and can finally see the Android app!

During the weekend break, I read the news and many people are talking about Google, Nokia, and Microsoft's "war" on smartphones. So I wonder how Android is developed based on it. I have never touched Java, I have never seen Android, and I have never even heard of its IDE! Start from-1 and never have the courage to start from 0! This kind of exploration is very painful, but fortunately, the world is good now, because there is a search!

I. Development Environment:

1. JDK

Latest Version: jdk-6u17-windows-i586.exe

Download http://java.sun.com/javase/downloads/index.jsp

2. Development IDE (eclipse)

Latest eclipse-jee-galileo-sr1-win32.zip

Http://www.eclipse.org/downloads/

3. adroid SDK

The latest version: android-sdk_r04-windows.zip

Http://developer.android.com/intl/zh-CN/sdk/index.html

Note: After downloading and deploying, you must configure environment variables. There are a lot of websites, so I will not talk about it anymore. Finally, install ADT online.

Run:

I have been working on Windows recently. The interface is a little bit dumb. It looks novel at first glance!

I explored Android development, which is similar to Windows Mobile development. The UI uses XML, which is similar to the WPF/silverligh XAML. I think the code is similar to the compilation of FLEX, and it tastes a bit actionscrip. However, there may be some differences in habits, probably because of Java. For example, it is very easy to assign a value to a text box in Windows Mobile:

Textbox. Text = "China ";

But in Android, it is a little troublesome:

Edittext = (edittext) findviewbyid (R. Id.Edittext01);

Edittext. settext ("China ");

3. Create a project

Create a simple application and generate a list. Use listview to display the list. You can specify the number of data entries to be generated, or use the default value. After the configuration is complete, use textview to give a prompt. Here, the knowledge points are used:

A. text display: assign a value to textview text;

B. Use of the text box and value assignment of edittext;

C. check whether or not to select multiple selection boxes;

D. Use of the list control and assign values to the listview Data Source

1. The form elements can be seen as standard XML:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: Orientation = "vertical"
4 Android: layout_width = "fill_parent"
5 Android: layout_height = "fill_parent"
6>
7 <textview
8 Android: Id = "@ + ID/txtview"
9 Android: layout_width = "fill_parent"
10 Android: layout_height = "wrap_content"
11 Android: text = "Hello, android! "
12 Android: textsize = "20px" Android: textstyle = "bold"/>
13 <checkbox Android: Id = "@ + ID/checkbox01"
14 Android: layout_width = "fill_parent"
15 Android: layout_height = "wrap_content"
16 Android: text = "@ string/checkbox01"
17 Android: checked = "true"
18/>
19 <edittext Android: text = "100"
20 Android: maxlines = "1"
21 Android: AutoText = "true"
22 Android: Id = "@ + ID/edittext01"
23 Android: layout_width = "200dip"
24 Android: layout_height = "wrap_content"
25/>
26 <button Android: Id = "@ + ID/button01"
27 Android: textsize = "20sp"
28 Android: layout_width = "200dip"
29 Android: layout_height = "wrap_content"
30 Android: text = "@ string/button_ OK"/>
31
32 <listview Android: Id = "@ + ID/listview01"
33 Android: layout_width = "wrap_content"
34 Android: layout_height = "wrap_content"/>
35 </linearlayout>

2. Java code:

1 package Android. mytest;
2
3 Import Android. App. activity;
4 Import Android. OS. Bundle;
5 import Android. View. view;
6 Import Android. View. View. onclicklistener;
7 Import Android. widget .*;
8
9 publicclass testactivity extends activity implements onclicklistener {
10/** called when the activity is first created .*/
11 @ override
12 publicvoid oncreate (bundle savedinstancestate ){
13 super. oncreate (savedinstancestate );
14 setcontentview (R. layout. Main );
15 button BTN = (button) findviewbyid (R. Id. button01 );
16 BTN. setonclicklistener (this );
17}
18
19 publicvoid onclick (view aview)
20 {
21 // generate 100 data entries
22 int K = 100;
23 edittext = (edittext) findviewbyid (R. Id. edittext01 );
24 checkbox = (checkbox) findviewbyid (R. Id. checkbox01 );
25
26 // listview
27 listview = (listview) findviewbyid (R. Id. listview01 );
28 arrayadapter <string> arrada = new arrayadapter <string> (
29 This, Android. R. layout. simple_list_item_1 );
30
31 if (! Checkbox. ischecked () & edittext. gettext (). tostring (). Trim (). Length ()> 0 ){
32 // If the multiple choice box is not selected, the data list is generated based on the value entered in the text box.
33 k = integer. parseint (edittext. gettext (). tostring (). Trim ());
34}
35 // construct the data source
36 For (INT I = 0; I <K; I ++ ){
37 arrada. Add (string. valueof (I) + "Android and windowsmobile ");
38}
39 // set the listview Data Source
40 listview. setadapter (arrada );
41
42 // prompt message
43 textview textview((textview)findviewbyid(r.id.txt view );
44 textview. settext (string. valueof ("data generated successfully! Total: "+ listview. getcount () +" Data! "));
45}
46}

Summary:

The example is very simple. I have never touched java before, and the syntax looks similar to C # (maybe this is what people say "C # Is a copy of Java "). The operation on the control is not the same as that in windwos. It was a bit difficult at the beginning. For example, the value assignment method settext () starts with set and the value gettext () starts with get.

Mostly depressing:

1. during compilation and running, the program can be run occasionally, but it cannot be run more often, and a main is generated. out. XML file to display an error again:

[2009-12-27 16:48:42-test] error in an XML file: Aborting build.

[16:48:42-test] res \ Layout \ main. xml: 0: Error: resource entry main is already defined.

[16:48:42-test] res \ Layout \ main. Out. xml: 0: originally defined here.

[16:48:42-test] E: \ test \ Android \ test \ res \ Layout \ main. Out. xml: 1: Error parsing XML: No element found

I cannot find the reason when I am crazy! This problem has plagued me for about three hours and I don't know what to do !! Finally, we can find a solution in a foreign e text: make sure to get the focus in the src directory in the project tree before generation. I don't know why ???

2. the most painful thing is that I can't understand e-text. I can't put a "translator" on my hand. It's just a waste of time! Awesome! Great pain! Great sorrow!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.