Android记住按钮选择颜色变化状态

  为以后可以温习自己做过的一些特效例子,同时也没有必要再一次重写代码,特此从项目中收集代码,按钮颜色变化状态如下:

  

  

  要实现如图效果,首先要编写一个布局xml文件,代码如下:

  <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="我爱"

                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="妳的"

                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="美丽善良"

                android:textColor="#666666"

                android:textSize="14sp" />

        </LinearLayout>

    </LinearLayout>

  

  窗体java代码如下:

  

  public class MainActivity extends Activity implements OnClickListener {

  

     private LinearLayout ll1, ll2, ll3;    //套住文本标签的布局

     private TextView tv1, tv2, tv3;     //文本标签

     //静态常亮随着程序的关闭而消失,如果要永久的记住颜色可以存储在本地

     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);

 

        //设置颜色

        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;

        }

    }

  }

  

  边框文件如下: 

  <?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:设置边框的状态(这里是矩形)

  stroke:边框线

  padding:内边距

  corners :边框角的设置

  solid :实体(边框的背景色)

  

  还有一个 shape 文件修改 stroke 的颜色即可,用于文字颜色设置的 colors 文件可自定义。

  

posted @ 2016-05-29 18:07  飞牛冲天  阅读(4657)  评论(0编辑  收藏  举报