Android Button 按钮 设置 各种状态 图片 颜色

有2个方法可以实现,一种是用 选择器 定义每种状态的图片 selec.xml <?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:state_pressed="true"
        android:drawable="@drawable/text_down"
        ></item>
    <item
        android:drawable="@drawable/append1"
        ></item>
</selector>

:
在xml里定义按钮的背景色android:background="@drawable/ selec " 或在代码中也可以动态设置 btncall.setBackgroundResource(R.drawable. selec );

 

另一种就是在触摸事件中直接设置

    private
Button mBtn; //定义我们的按钮


   
在onCreate中加入


      mBtn
= (Button) findViewById(R.id.btn); //btn为layout中的Button ID


    
mBtn.setOnTouchListener(new OnTouchListener()

    
   {

         
public boolean onTouch(View arg0,MotionEvent arg1)

         
{

       
   if(arg1.getAction() == MotionEvent.ACTION_DOWN)

       
   {

          
arg0.setBackgroundResource(R.drawable.pressed);
//按下的图片对应pressed

       
   }

       
   else if(arg1.getAction() == MotionEvent.ACTION_UP)

       
   {

          
arg0.setBackgroundResource(R.drawable.normal);
//常态下的图片对应normal

       
   }


       
   else
if()  //这里还可以继续实现MotionEvent.ACTION_MOVE和MotionEvent.ACTION_CANCEL等实现更多的特效

posted on 2014-04-18 10:56  strangeman  阅读(1802)  评论(0编辑  收藏  举报

导航