User interface part of the learning is really nowhere to start Wow, can not be a control to send a text bar, a little bit of time ah ... Is this not the edge of learning to force it? So I'm going to start with the most practical button.
First put a few links, good things:
An exhaustive tutorial example series for the Android user interface:
Http://www.cnblogs.com/aimeng/archive/2012/06/26/2563762.html
Android User Interface Tutorial Example rollup:
Http://www.cnblogs.com/aimeng/archive/2012/06/25/2560905.html
The main content of this article is button, RELATED links:
A powerful study post:
Http://www.apkbus.com/android-48448-1-1.html
Official Document Information:
Http://developer.android.com/reference/android/widget/Button.html
Http://developer.android.com/guide/topics/ui/controls/button.html
Button Basic Use method
First, add the button control to the XML layout file. can also be added through the program.
Set some properties of the button in the layout file, such as position, width height, word on button, color, etc.
The more important thing is to give the button an ID number, which is the button's only name.
This allows the button to be obtained in the program in the following form:
Button = (button) Findviewbyid (R.id.buttonid);
Handle button click
There are two ways to handle button clicks.
The first is to implement this method in activity by setting the name of the method that handles the Click event through the OnClick property.
Another method is a typical application of the event monitoring mechanism, which is described in detail below.
1. Setting the processing method via the OnClick property
To set the properties of a button in an XML layout file:
android:onclick= "Yourmethodname"
The method is then implemented in the corresponding acitivity of the layout file:
/** called when the user touches the button */public void Yourmethodname (view view) { //does something in respons E to button click}
It is important to note that this method must meet three conditions:
1.public
2. return void
3. There is only one parameter view, this view is the control that is clicked.
2. Adding Listener objects using Setonclicklistener
For event listening mode, see
Http://www.cnblogs.com/mengdd/archive/2012/09/08/2676587.html
Can write an internal class , implement the Onclicklistener interface, implement the OnClick method in this class, the method is written in the button click to do the specific work.
The object of this inner class is passed into the Setonclicklistener method of the button, that is, the binding of the Listener object and the button is completed (the event listener is registered on the event Source button), and the OnClick method of the listener object is invoked whenever the button is clicked.
of course, there is no need to write an internal class, such as this example :
Button button = (button) Findviewbyid (r.id.button_send); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) { //does something in re Sponse to button click }});
Button Basic Operation example
On an example:
XML layout file
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Text View android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centerho Rizontal= "true" android:layout_centervertical= "true" android:padding= "@dimen/padding_medium" Android: text= "@string/hello_world" tools:context= ". Buttonactivity "/> <button android:id=" @+id/button_first "android:layout_height=" Wrap_content " Android:layout_width= "Wrap_content" android:text= "@string/buttontext" android:onclick= "Changebuttoncol or "> </Button> <button android:id=" @+id/button_second "Android:layo ut_height= "Wrap_content" android:layout_width= "wrap_content" android:text= "@string/buttontext2" android:layout_below= "@id/button_first" > </Button> </RelativeLayout>
Activity Code
Package Com.example.buttontest;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Buttonactivity extends Activity {private Button button01 = null; Private Button button02 = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_button); Button01 = (Button) Findviewbyid (R.id.button_first); BUTTON02 = (Button) Findviewbyid (R.id.button_second); Bind event Source and Listener Object Button02.setonclicklistener (New Mybuttonlistener ()); @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (r.menu.activity_but ton, menu); return true; }//button 1 Click event public void Changebuttoncolor (view view) {Button01.setbackgroundcolor (Getresources (). Get Color (r.color.red)); }//inner class, implement Onclicklistener interface//As listener class for the second button Mybuttonlistener implements Onclicklistener { public void OnClick (View v) {Button02.setbackgroundcolor (Getresources (). GetColor (R.color.blue)) ; } } }
Run
Before clicking:
After clicking:
Content in the related resource file
Strings.xml
<resources> <string name= "app_name" >ButtonTest</string> <string name= "Hello_world" >hello world!</string> <string name= "menu_settings" >Settings</string> <string Name= "Title_activity_button" >ButtonActivity</string> <string name= "ButtonText" >thisisabutton </string> <string name= "ButtonText2" >ThisIsAnotherButton</string></resources>
Colors.xml
<?xml version= "1.0" encoding= "UTF-8"?><resources> <color name= "Red" > #f00 </color> <color name= "Green" > #0f0 </color> <color name= "Blue" > #00f </color> <color Name = "BLACK" > #000 </color></resources>
Transfer from: Link
Android Tutorial Instance Series