Free Source Code and Program Tips
Add a Listener Tips Android View Sample
In the development for the control to add Listener is a very common work, the easiest way to add Listener can be:
1 | 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:
1 | 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
1 | <? 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).
1 | 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"
1 | 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)
| Print article | This entry was posted by support on October 18, 2009 at 6:16 am, and is filed under General. Follow any responses to this post through RSS 2.0. You can skip to the end and leave a response. Pinging is currently not allowed. |
No comments yet.
Eclipse developed Android, Hello, TimePicker (study 8)
about 7 months ago - No comments
Hello, TimePicker 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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Eclipse developed Android, Hello, WebView (study 6)
about 7 months ago - No comments
Hello, WebView 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 1 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
Eclipse developed Android, RelativeLayout (study 3)
about 7 months ago - No comments
Hello, RelativeLayout A RelativeLayout is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements. Start a new project / Activity called HelloRelativeLayout. Open the layout file. Make it like so: 1 <? xml version = "1.0" encoding = "utf-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width
Eclipse developed Android, Hello Spinner (Study 10)
about 7 months ago - No comments
HelloSpinner.java Source 1 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 1 <? xml version = "1.0"
Android View Add Listener Tips Sample
about 7 months ago - No comments
In the development for the control to add Listener is a very common work, the easiest way to add Listener can be: 1 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,
Eclipse developed Android, Hello, MapView (study 5)
about 7 months ago - No comments
Hello, MapView Learning Address: http://androidappdocs.appspot.com/guide/tutorials/views/hello-mapview.html HelloItemizedOverlay.java Code 1 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
Eclipse developed Android, TableLayout (study 4)
about 7 months ago - No comments
Hello, TableLayout A TableLayout is a ViewGroup that will Lay child View elements into rows and columns. Start a new project / Activity called HelloTableLayout. Open the layout file. Make it like so: 1 <? xml version = "1.0" encoding = "utf-8"?> <TableLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android if they had to switch screen solution (reproduced)
about 7 months ago - No comments
Original From: http://blog.sina.com.cn/s/blog_4ca975460100g5yj.html Android if they had to switch on the screen solution The time in the development of the game, some games are only horizontal screen play, so when placed in the erection of mobile phone, to keep the game screen is still horizontal screen. To achieve this requirement is very simple to configure
Android custom ContentProvider
about 7 months ago - No comments
Custom ContentProvider ———- The content provider is used to deal with data sources, while the content parser (ContentResolver) responsible for operating the specific content provider. The data source can be a file or database. More content resolver can simultaneously access the content provider because it is thread-safe . A URI can identify a resource, and
Eclipse developed Android, Hello FormStuff (study 9)
about 7 months ago - No comments
1 HelloFormStuff. Java code is as follows 1 package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.RadioButton; import android.widget.Toast; import android.widget.ToggleButton; public class HelloFormStuff extends Activity (/ ** Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) (super.onCreate (savedInstanceState);