2015-08-27 31 views
3

嗨,大家好我是新的Android如何在android中应用按钮的颜色动画?

我在XML中的LinearLayout,当我的LinearLayout点击我需要的颜色Appearences用于onclick事件的具体时间,之后颜色就会消失, 任何一个可以帮助我球员

holder.ll2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(final View v) { 

        v.setBackgroundColor(0xFF00FF00); 
        v.invalidate(); 
       } 
      } 
     }); 

回答

0

您可以使用处理器为

holder.ll2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(final View v) { 
        handler=new Handler(); 
        handler.postDelayed(myRunnable, TIME_MILI_TO_SHOW_COLOR_INT); 
        btn.setBackgroundColor(0xFF00FF00); 
       } 
      } 
     }); 

Runnable for即

myRunnable=new Runnable() { 

      @Override 
      public void run() { 

       btn.setBackgroundColor(YOUR_DEFAULT_COLOR); 
      } 
     } 
0

这个链接可以帮助你

How to create custom button in Android using XML Styles

基本上你想单击了规定的button..when点击它应该表现出不同的颜色,当释放显示原始color.It可以这样做定义按钮声明为XML,然后将该XML设置为按钮背景。

+0

我想显示当我单击事件完成后点击linearlayout的颜色外观颜色也消失 –

+0

当我点击一个按钮时我需要背景的颜色效果之后它将消失 –

0

试试这个代码

Button btn = (Button)this.findViewById(R.id.btn1); 
//Let's change background's color from blue to red. 
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)}; 
TransitionDrawable trans = new TransitionDrawable(color); 
//This will work also on old devices. The latest API says you have to use setBackground instead. 
btn.setBackgroundDrawable(trans); 
trans.startTransition(5000); 
+0

如何显示clickevent在android中的颜色效果? –

+0

只需调用此方法按钮单击 – sasikumar

+0

我需要特定时间的彩色动画,并且我正在使用onclick进行布局 –

2

试试这个 请在资源文件 绘制夹中创建此XML 您可以根据您的要求

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" 
     android:state_pressed="false" 
     android:drawable="@drawable/big_box_hover"/> 
<item android:state_focused="true" 
     android:state_pressed="true" 
     android:drawable="@drawable/big_box_hover" /> 
<item android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/big_box_hover" /> 
<item android:drawable="@drawable/big_box" /> 
</selector> 

然后 在布局 改变它,你可以做到这一点

<Button 
      android:id="@+id/button" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="6dp" 
      android:layout_marginTop="6dp" 
      android:descendantFocusability="blocksDescendants" 
      android:background="@drawable/dashboardbuttonclick" 
      android:gravity="center_vertical" > 
+0

您可以根据您的要求设置drawables,例如按下视图时需要什么样的颜色 –

相关问题