Create two styles in Res/drawable before/after clicking
Round
1<?xml version="1.0"encoding="Utf-8"?>2<shape xmlns:android="http://schemas.android.com/apk/res/android">3<!--set the gradient color angle="0"is from left to right 90 is from top to bottom 180 is right to left---4<Gradient5Android:startcolor="#4169E1"6Android:endcolor="#E6E6FA"7android:angle="0"/>8<Stroke9Android:width="2DP"TenAndroid:color="#D2691E" One/> A<corners android:radius="10dip"/> -<padding -android:left="10DP" theandroid:top="10DP" -android:right="10DP" -Android:bottom="10DP" -/> +</shape>
Newround:
1<?xml version="1.0"encoding="Utf-8"?>2<shape xmlns:android="http://schemas.android.com/apk/res/android">3<!--angle="0"is from left to right 90 is from top to bottom 180 is right to left and must be a multiple of 45--4<Gradient5Android:startcolor="#FFD700"6Android:endcolor="#f5deb3"7android:angle="0"/>8<Stroke9Android:width="2DP"TenAndroid:color="#FFB6C1" One/> A<corners android:radius="20dip"/> -<padding -android:left="10DP" theandroid:top="10DP" -android:right="10DP" -Android:bottom="10DP" -/> +</shape>
Next J
Activity:
<linearlayout 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 " android:gravity= "center" android:background= "@drawable/asdf" tools:context= "Com.example.top5_4. Mainactivity "> <button android:id=" @+id/but " android:layout_height=" Wrap_content " Android:layout_width= "Wrap_content" android:text= "click button to change the button style" android:background= "@drawable/round" /></linearlayout>
Java:
1 Public classMainactivity extends Activity {2 PrivateButton but;3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 super.oncreate (savedinstancestate);6 Setcontentview (r.layout.activity_main);7but=(Button) Findviewbyid (r.id.but);8But.setonclicklistener (NewOnclicklistener () {9 Ten @Override One Public voidOnClick (View v) { A //TODO auto-generated Method Stub - //Change button style after clicking - But.setbackgroundresource (r.drawable.newround); the //also set the transparency of the button setalpha () The value size is 0~255 -But.getbackground (). Setalpha ( About); - } - }); + } -}
Operating effect:
Before you click
After clicking:
Android Basic Top5_4: Click the button to change the style, set the transparency