A button consists of a text or an icon (or text and an icon), and when the user touches it, some action occurs. Today we start the button study.
A brief description of the button
Depending on whether you want a button with text, an icon, or both, you can create a button in three ways to lay it out:
- with text, using the Button class:
< Button Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/button_ Text " />
- with an icon, using the ImageButton class:
< ImageButton Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:src= "@drawable/button_ Icon " />
- With text and a icon, using the
Button class with the android:drawableLeft attribute:
< Button Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/button_ Text " android:drawableleft=" @drawable/button_icon " />
The response of the button
1) add attribute Android:onclick in the Button defined in XML
- Define the Android:onclick attribute in XML:
<? xml version= "1.0" encoding= "Utf-8" ?> < button xmlns:android = "Http://schemas.android.com/apk/res/android" Android:layout_width = "Wrap_content" Android:layout_height = "Wrap_content" Android:text = "@string/button_send" Android:onclick = "SendMessage" />
- Define the SendMessage method in Mainactivity:
Public void sendMessage (view view) { // do something in response to button click}
2) use onclicklistener in your code
Button button = (Button) Findviewbyid (r.id.button_send); Button.setonclicklistener (new View.onclicklistener () { publicvoid OnClick (View v) { /// do Something in response to button click }});
button Style
1) Borderless button : to create a borderless button, apply the Borderlessbuttonstyle style to the button.
< button android:onclick = "SendMessage" = "Wrap_content" = "Wrap_content" = "bordless button" style = "? Android:attr/borderlessbuttonstyle" />
2) Custom Background:instead of supplying a simple bitmap or color, however, your background should is a state list R Esource that changes appearance depending on the button's current state
- Create three bitmaps for the button background that represent the default, pressed, and focused button states.
- Place the bitmaps into the res/drawable/directory of your project. Be sure each bitmap are named properly to reflect the button state this they each represent.
- Create a new XML file in the res/drawable/directory (name it something like Button_custom.xml). Insert the following XML
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/button_pressed"android:state_pressed= "true" /> <Itemandroid:drawable= "@drawable/button_focused"android:state_focused= "true" /> <Itemandroid:drawable= "@drawable/button_default" /></selector>
- Then simply apply the drawable XML file as the button background
< Button Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:text= "send button " android:background=" @drawable/button_custom " />
tested Projects
Project structure:
Mainactivity.java:
Packagecom.huhx.linux.buttontest;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.sendbutton); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "button click", Toast.length_short). Show (); } }); } //bordless Button Public voidsendMessage (view view) {Toast.maketext (mainactivity. This, "borderless button", Toast.length_short). Show (); }}
Button_custom.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/button_pressed"android:state_pressed= "true" /> <Itemandroid:drawable= "@drawable/button_focused"android:state_focused= "true" /> <Itemandroid:drawable= "@drawable/button_default" /></selector>
Activity_main.xml.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns: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"android:orientation= "vertical"Tools:context= "Com.huhx.linux.buttontest.MainActivity"> <ButtonAndroid:id= "@+id/sendbutton"Android:text= "Text Button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <ImageButtonandroid:src= "@mipmap/ic_launcher"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <ButtonAndroid:drawableright= "@mipmap/ic_launcher"Android:text= "Linux"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> <ButtonAndroid:onclick= "SendMessage"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "bordless button"style= "? Android:attr/borderlessbuttonstyle" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send Button"Android:background= "@drawable/button_custom" /></LinearLayout>
The results are as follows:
Android Basic components---->buttons use