19 Dec
Posted by support as General 26 views, 0 Comments
A TimePicker is a Widget that allows the User to Select the time by hour, minute and AM or PM.
Start a new project / Activity called HelloTimePicker.
Modify HelloTimePicker.java code is as follows:
package com.example.test; import java.util.Calendar; import android.app.Activity; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class HelloTimePicker extends Activity (private TextView mTimeDisplay; private Button mPickTime; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); setContentView (R.layout.main); / / capture our View elements mTimeDisplay = (TextView) findViewById (R.id.timeDisplay); mPickTime = (Button) findViewById (R.id.pickTime); / / add a click listener to the button mPickTime.setOnClickListener (new View.OnClickListener () (public void onClick (View v) (showDialog (TIME_DIALOG_ID); ))); / / get the current time final Calendar c = Calendar.getInstance (); mHour = c.get (Calendar.HOUR_OF_DAY); mMinute = c.get (Calendar.MINUTE); / / display the current date updateDisplay (); } Overrideprotected Dialog onCreateDialog (int id) (switch (id) (case TIME_DIALOG_ID: return new TimePickerDialog (this, mTimeSetListener, mHour, mMinute, false); ) return null; ) / / updates the time we display in the TextViewprivate void updateDisplay () (mTimeDisplay.setText ( new StringBuilder (). append (pad (mHour)). append (":"). append (pad (mMinute ))); } private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener () (public void onTimeSet (TimePicker view, int hourOfDay, int minute) (mHour = hourOfDay; mMinute = minute; updateDisplay (); )); private static String pad (int c) (if (c> = 10) return String.valueOf (c); else return "0 "+ String.valueOf (c); ))
Layout-> main.xml
<? xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "vertical"> <TextView android:id="@+id/timeDisplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android : id = "@ + id / pickTime" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Change the time "/></ LinearLayout>
Run Now run it. Run the results are as follows:

Source Download: HelloTimePicker.zip
18 Dec
Posted by support as General 6 views, 0 Comments
A WebView allows you to Create your own Web Browser Activity. In this Tutorial, we'll Create a simple Activity that can View Web pages.
Learning Address: http://androidappdocs.appspot.com/guide/tutorials/views/hello-webview.html
HelloWebView.java Code
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.webkit.WebView; public class HelloWebView extends Activity (WebView webview; / ** Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); setContentView (R.layout.main); webview = (WebView) findViewById (R.id.webview); webview.getSettings (). setJavaScriptEnabled (true) ; webview.loadUrl (http://ditu.google.cn);))
Layout-> main.xml
<? xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "vertical"> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </ LinearLayout>
Implementation of the Eclipse-> HelloWebViewAndroid-> Android Application Show pictures are as follows:

Code download HelloWebView.zip
18 Dec
Posted by support as General 7 views, 0 Comments
A RelativeLayout is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements.
<? xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:"/> <EditText android: id = " @ + id / entry "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: background =" @ android: drawable / editbox_background "android: layout_below =" @ id / label "/> <Button android: id = "@ + id / ok" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ id / entry" android: layout_alignParentRight = "true" android: layout_marginLeft = "10dip" android: text = " OK "/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> < / RelativeLayout>
Pay attention to each of the additional layout_* attributes (besides the usual width and height, which are required for all elements). When using relative layout, we use attributes like layout_below and layout_toLeftOf to describe How we'd like to position each View. Naturally, these are different relative positions, and the value of the attribute is the id of the element we want the position relative to.
onCreate() method:
public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState); setContentView (R.layout.main);)
R.layout.main refers to the main.xml layout file.
18 Dec
Posted by support as General 4 views, 0 Comments
HelloSpinner.java Source
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class HelloSpinner extends Activity (@ Overridepublic void onCreate (Bundle savedInstanceState) ( super.onCreate (savedInstanceState); setContentView (R.layout.main); Spinner s = (Spinner) findViewById (R.id.spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource (this, R.array.planets, android.R. layout.simple_spinner_item); adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); s.setAdapter (adapter);))
layout-> main.xml
<? xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: padding = "10dip" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginTop = "10dip" android: text = "Please select a planet: "/> <Spinner android: id =" @ + id / spinner "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: drawSelectorOnTop =" true "android: prompt =" @ string / planet_prompt "/ > </ LinearLayout>
values-> arrays.xml
<resources> <string-array name="planets"> <item> Mercury </ item> <item> Venus </ item> <item> Earth </ item> <item> Mars </ item> <item> Jupiter < / item> <item> Saturn </ item> <item> Uranus </ item> <item> Neptune </ item> </ string-array> </ resources>
value-> strings.xml
<? xml version = "1.0" encoding = "utf-8"?> <resources> <string name="hello"> Hello World, HelloSpinner! </ string> <string name="app_name"> HelloSpinner </ string> <string name="planet_prompt"> Choose a planet </ string> </ resources>
run it 
In the development for the control to add Listener is a very common work, the easiest way to add Listener can be:
findViewById (R.id.myButton). setOnClickListener (new View.OnClickListener () (public void onClick (View v) (/ / Do stuff)));
Add a Listener using the above method has a drawback is that if control is too large, Listener will increase the number, so the following tips can be used to reduce the number of Listener:
View.OnClickListener handler = View.OnClickListener () (public void onClick (View v) (switch (v.getId ()) (case R.id.Button01: / / doStuff break; case R.id.Button02: / / doStuff break;))) findViewById (R.id.myButton). setOnClickListener (handler); findViewById (R.id.myOtherButton). setOnClickListener (handler);
The Android1.6 inside, add the Listener's work has become fairly simple (feeling more like doing web programming!), Concrete steps are as follows:
1. First, the definition in the layout inside the Button and specify the response Listener
<? 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="wrap_content" android:text="@string/hello" /> <Button android: text = "Button01" android : id = "@ + id/Button01" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "myClickHandler01" /> <Button android: text = "Button02" android: id = "@ + id / Button02 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: onClick =" myClickHandler02 "/> <TextView android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: text =" @ string / hello "/> </ LinearLayout>
These two lines of which the following are new features:
android: onClick = "myClickHandler01"
android: onClick = "myClickHandler02"
2. In the event inside the definition of public methods myClickHandler01, and myClickHandler02 (Note that these two methods must have a View of the shape parameter).
package com.ray.test; import android.app.Activity; import android.os.Bundle; import android.view.View; public class TestOnClickListener extends Activity (@ Override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState) ; setContentView (R.layout.main);) public void myClickHandler01 (View target) (setTitle ( "myClickHandler01");) public void myClickHandler02 (View target) (setTitle ( "myClickHandler02");))
Of course, you can also use this wording:
The two buttons set to the same Listener
android: onClick = "myClickHandler"
android: onClick = "myClickHandler"
package com.ray.test; import android.app.Activity; import android.os.Bundle; import android.view.View; public class TestOnClickListener extends Activity (@ Override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState) ; setContentView (R.layout.main);) public void myClickHandler (View target) (switch (target.getId ()) (case R.id.Button01: setTitle ( "myClickHandler01"); break; case R.id.Button02 : setTitle ( "myClickHandler02"); break;)))
Refer to the article: "UI framework changes in Android 1.6" (take over the wall)
18 Dec
Posted by support as General 29 views, 0 Comments
Learning Address: http://androidappdocs.appspot.com/guide/tutorials/views/hello-mapview.html
HelloItemizedOverlay.java Code
package com.example.test; import java.util.ArrayList; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class HelloItemizedOverlay extends ItemizedOverlay (private ArrayList <OverlayItem> mOverlays = new ArrayList <OverlayItem> (); public HelloItemizedOverlay (Drawable defaultMarker) (super (boundCenterBottom (defaultMarker ));// TODO Auto-generated constructor stub) @ Overridepublic int size () (return mOverlays. size ();) public void addOverlay (OverlayItem overlay) (mOverlays.add (overlay); populate ();}Overrideprotected OverlayItem createItem (int i) (return mOverlays.get (i);))
HelloMapView1.java Code
package com.example.test; import java.util.List; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.ZoomControls; import com.google.android . maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.OverlayItem; public class HelloMapView1 extends MapActivity (LinearLayout linearLayout; MapView mapView; ZoomControls mZoom; List <Overlay> mapOverlays; Drawable drawable; HelloItemizedOverlay itemizedOverlay; / ** Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) (super . onCreate (savedInstanceState); setContentView (R.layout.main); mapView = (MapView) findViewById (R.id.mapview); mapView.setBuiltInZoomControls (true); mapOverlays = mapView.getOverlays (); drawable = this.getResources ( ). getDrawable (R.drawable.androidmarker); itemizedOverlay = new HelloItemizedOverlay (drawable); GeoPoint point = new GeoPoint (19240000, -99120000); OverlayItem overlayitem = new OverlayItem (point, "", ""); itemizedOverlay.addOverlay ( overlayitem); mapOverlays.add (itemizedOverlay);) @ Override protected boolean isRouteDisplayed () (return false;))
Layout-> mail.xml
<? xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id / mainlayout "android: orientation =" vertical "android: layout_width =" fill_parent "android: layout_height =" fill_parent "> <com.google.android.maps.MapView android: id =" @ + id / mapview "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: clickable =" true "android: apiKey =" Your Maps API Key "/> <LinearLayout android: id =" @ + id / zoomview "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: layout_alignBottom = "@ id / mapview" android: layout_centerHorizontal = "true "/></ RelativeLayout>
Implementation of the android Application, found that google maps can not be displayed, please know the cause of the friend told my, thank you.