Android Notes (14) Basic components in Android--buttons

Source: Internet
Author: User

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

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.