Android Basic Top5_4: Click the button to change the style, set the transparency

Source: Internet
Author: User

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

Related Article

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.