The buttons in Android mainly include button and ImageButton Two, Button inherits from TextView, and ImageButton inherits from ImageView. The button generates the text on the buttons, and the picture is displayed on the ImageButton.
The main function is to generate a button on the UI interface, and when the user taps this button, an onclick event is set to perform a task.
Simple example
<Tablelayoutxmlns: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"Tools:context=". Mainactivity "android:orientation= "Horizontal" > <TableRow> <!--Normal Text button - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button"android:textsize= "10pt" /> <!--Normal Picture button - <ImageButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/blue"Android:background= "#000000" /> <!--Picture Background Text button - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Picture background text button"Android:background= "@drawable/red"android:textsize= "10pt" /> </TableRow></Tablelayout>
Operation Result:
A small demo that asks the interface to display a text with a digital text, click on the button, the number increases
Mainactivity.java
Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classButtonactivityextendsActivity {TextView TV=NULL; String Count=NULL; intnum = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_button); TV=(TextView) Findviewbyid (R.id.count); Count=(String) tv.gettext (); Num=Integer.parseint (count); Findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Tv.settext (++num) + ""); Onrestart (); } }); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.D ("TEST", num + ""); }}
Activity_main.xml
<Tablelayoutxmlns: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"Tools:context= "Cn.lixyz.layoutdemo.ButtonActivity"> <!--Normal Text button - <TableRow> <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@drawable/red"Android:text= "Normal button"android:textsize= "10pt"/> </TableRow> <TextViewAndroid:id= "@+id/count"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "50pt"Android:textcolor= "#ff211c"Android:text= "1"/></Tablelayout>
Operation Result:
Android Notes (14) Basic components in Android--buttons