Android Remember button Select color change state

Source: Internet
Author: User
Tags getcolor

In order to be able to review some of the special effects you have done, but there is no need to rewrite the code again, I hereby collect the code from the project, the button color change status is as follows:

  

  

  To achieve the effect, you first write a layout XML file with the following code:

  < LinearLayout

Android:layout_width="Match_parent"

android:layout_height="50DP"

android:background="#ffffff"

android:orientation="Horizontal" >

<linearlayout

android:id="@+id/ll1"

Android:layout_width="100DP"

android:layout_height="32DP"

android:layout_marginleft="15DP"

android:layout_margintop="12DP"

android:background="@drawable/shape_cart"

android:gravity="center"

android:orientation="vertical" >

<textview

android:id="@+id/tv1"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:text=" I Love "

android:textcolor="#666666"

android:textsize="14sp" />

</LinearLayout>

<linearlayout

android:id="@+id/ll2"

Android:layout_width="100DP"

android:layout_height="32DP"

android:layout_marginleft="15DP"

android:layout_margintop="12DP"

android:background="@drawable/shape_cart"

android:gravity="center"

android:orientation="vertical" >

<textview

android:id="@+id/tv2"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:text=" your "

android:textcolor="#666666"

android:textsize="14sp" />

</LinearLayout>

<linearlayout

android:id="@+id/ll3"

Android:layout_width="100DP"

android:layout_height="32DP"

android:layout_marginleft="15DP"

android:layout_margintop="12DP"

android:background="@drawable/shape_cart"

android:gravity="center"

android:orientation="vertical" >

<textview

android:id="@+id/tv3"

Android:layout_width="Wrap_content"

android:layout_height="Wrap_content"

android:text=" beautiful and kind "

android:textcolor="#666666"

android:textsize="14sp" />

</LinearLayout>

</LinearLayout>

  

  The form Java code is as follows:

  

Public class Mainactivity extends Activity implements Onclicklistener {

Private    LinearLayout ll1, Ll2, Ll3; Nesting the layout of text labels

Private     TextView TV1, TV2, TV3; Text labels

Static solid disappears as the program closes, and if you want to permanently remember that the color can be stored locally

Public Static String seller_id = "0", available = "0",canorder= "0";

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Fragment_main);

Initview ();

}

Private void Initview () {

LL1 = (linearlayout) Findviewbyid (r.id. Ll1);

Ll1.setonclicklistener (this);

TV1 = (TextView) Findviewbyid (r.id. TV1);

Ll2 = (linearlayout) Findviewbyid (r.id. Ll2);

Ll2.setonclicklistener (this);

TV2 = (TextView) Findviewbyid (r.id. TV2);

Ll3 = (linearlayout) Findviewbyid (r.id. Ll3);

Ll3.setonclicklistener (this);

TV3 = (TextView) Findviewbyid (r.id. TV3);

Set color

if (seller_id. Equals ("1")) {

Ll1.setbackgroundresource (r.drawable. Shape_cart3);

Tv1.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

} Else if (seller_id. Equals ("0") | | seller_id. Equals ("")) {

Ll1.setbackgroundresource (r.drawable. Shape_cart);

Tv1.settextcolor (this. getresources (). GetColor (R.color. Txt6));

}

if (available. Equals ("1")) {

Ll2.setbackgroundresource (r.drawable. Shape_cart3);

Tv2.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

} Else if (available. Equals ("0") | | available. Equals ("")) {

Ll2.setbackgroundresource (r.drawable. Shape_cart);

Tv2.settextcolor (this. getresources (). GetColor (R.color. Txt6));

}

if (canorder. Equals ("1")) {

Ll3.setbackgroundresource (r.drawable. Shape_cart3);

Tv3.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

} Else if (canorder. Equals ("0") | | canorder. Equals ("")) {

Ll3.setbackgroundresource (r.drawable. Shape_cart);

Tv3.settextcolor (this. getresources (). GetColor (R.color. Txt6));

}

}

@Override

Public void OnClick (View v) {

Switch (V.getid ()) {

Case R.id. ll1:

if (seller_id. Equals ("0") | | seller_id. Equals ("")) {

Ll1.setbackgroundresource (r.drawable. Shape_cart3);

Tv1.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

seller_id = "1";

} Else if (seller_id. Equals ("1")) {

Ll1.setbackgroundresource (r.drawable. Shape_cart);

Tv1.settextcolor (this. getresources (). GetColor (R.color. Txt6));

seller_id = "0";

}

break;

Case R.id. ll2:

if (available. Equals ("0") | | available. Equals ("")) {

Ll2.setbackgroundresource (r.drawable. Shape_cart3);

Tv2.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

Available = "1";

} Else if (available. Equals ("1")) {

Ll2.setbackgroundresource (r.drawable. Shape_cart);

Tv2.settextcolor (this. getresources (). GetColor (R.color. Txt6));

Available = "0";

}

break;

Case R.id. Ll3:

if (canorder. Equals ("0") | | canorder. Equals ("")) {

Ll3.setbackgroundresource (r.drawable. Shape_cart3);

Tv3.settextcolor (this. Getresources ()

. GetColor (R.color. Tab_light));

Canorder = "1";

} Else if (canorder. Equals ("1")) {

Ll3.setbackgroundresource (r.drawable. Shape_cart);

Tv3.settextcolor (this. getresources (). GetColor (R.color. Txt6));

canorder = "0";

}

break;

default:

break;

}

}

}

  

  The border file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<shape xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:shape= "Rectangle" >

<stroke
Android:width= "1DP"
Android:color= "#db2725"/>

<padding
Android:bottom= "1DP"
android:left= "1DP"
android:right= "1DP"
android:top= "1DP"/>
<corners android:radius= "3DP"/>
<solid android:color= "#ffffff"/>

</shape>

  

  Shape: Sets the state of the border (this is the rectangle)

Stroke: Border Line

padding: Inner margin

Corners: Setting of border angle

Solid: Solid (background color of border)

  

There is also a shape file that modifies the color of the stroke, and the colors file for text color settings can be customized.

  

Android Remember button Select color change state

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.