2010-10-07 189 views
64

我正在开发Android应用程序。我想要4个按钮水平放置在屏幕的底部。在这4个按钮中,2个按钮上有图像。按钮的边框应为黑色,边框应尽可能薄。当我点击按钮时,我希望按钮的背景应该更改为蓝色,而不改变边框的颜色,并且应该保持该颜色一段时间。我如何在Android中实现这种情况?如何在Android中点击时更改按钮的颜色?

回答

7

转寄此,

boolean check = false; 
Button backward_img; 
Button backward_img1; 
backward_img = (Button) findViewById(R.id.bars_footer_backward); 
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1); 
backward_img.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     check = true; 
     backward_img.setBackgroundColor(Color.BLUE); 
    } 
}); 

if (check == true) { 
    backward_img1.setBackgroundColor(Color.RED); 
    backward_img.setBackgroundColor(Color.BLUE); 
} 
+2

这将永久性地设置BG,也就是说,如果用户单击后退按钮,背景将变为红色。 – methode 2010-10-07 13:33:41

+0

@methode:ok请参阅我已更正oldanswer的新答案 – 2010-10-07 13:39:41

110

一种方法是在drawable创建这样一个XML文件,名为whatever.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="true" 
     android:drawable="@drawable/bgalt" /> 

    <item 
     android:state_focused="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/bgalt" /> 

    <item android:drawable="@drawable/bgnorm" /> 
</selector> 

bgaltbgnorm在绘制PNG图像。

如果您在活动编程方式创建的按钮,可以设置与背景:

final Button b = new Button (MyClass.this); 
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever)); 

如果您设置按钮的风格与XML,你会做这样的事情:

<Button 
    android:id="@+id/mybutton" 
    android:background="@drawable/watever" /> 

最后一个link to a tutorial。 希望这有助于。

+16

如果要避免使用图像,还可以用颜色定义替换XML文件中的可绘制对象。 – haseman 2010-10-07 13:55:47

+0

@haseman真棒......你怎么知道的? – 2012-05-15 17:58:24

+6

@ jshin47一个试验和错误的经验值。我现在写了两本关于Android开发的书籍:-) – haseman 2012-05-24 16:23:15

4

试试这个......

首先创建一个名为button_pressed.xml 这些都是它的内容的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/icon_1" /> 
    <item android:state_focused="true" 
      android:state_pressed="true" 
      android:drawable="@drawable/icon_1_press" /> 
    <item android:state_focused="false" 
      android:state_pressed="true" 
      android:drawable="@drawable/icon_1_press" /> 
    <item android:drawable="@drawable/icon_1" /> 
</selector> 

Noe试试这个按钮。

int imgID = getResources().getIdentifier("button_pressed", "drawable", getApplication().getPackageName()); 
button.setImageResource(imgID); 

button_pressed.xml应该在可绘制文件夹中。 icon_1_press和icon_1是用于按下按钮和正常对焦的两个图像。

5

如果您想在按下按钮时更改该按钮的背景图像或颜色,那么只需复制此代码并粘贴到您的项目中的精确位置,如下所述。

 <!-- Create new xml file like mybtn_layout.xml file in drawable --> 
<?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/pressed" /> <!--pressed --> 
<item android:drawable="@drawable/normal" /> <!-- Normal --> 
</selector> 
    <!-- Now this file should be in a drawable folder and use this 
    single line code in button code to get all the properties of this xml file --> 

    <Button 
     android:id="@+id/street_btn" 
     android:layout_width="wrap_content" 
     android:background="@drawable/layout_a" > <!-- your required code --> 
    </Button> 
66

保存此代码绘制文件夹 “bg_button.xml”,并呼吁 “@绘制/ bg_button” 作为你的XML按钮的背景:

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape> 
      <solid 
       android:color="#004F81" /> 
      <stroke 
       android:width="1dp" 
       android:color="#222222" /> 
      <corners 
       android:radius="7dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 
    <item> 
     <shape> 
      <gradient 
       android:startColor="#89cbee" 
       android:endColor="#004F81" 
       android:angle="270" /> 
      <stroke 
       android:width="1dp" 
       android:color="#4aa5d4" /> 
      <corners 
       android:radius="7dp" /> 
      <padding 
       android:left="10dp" 
       android:top="10dp" 
       android:right="10dp" 
       android:bottom="10dp" /> 
     </shape> 
    </item> 
</selector> 
+2

谢谢你,使用样式可以减少应用程序的大小。不要使用可绘制的好主意 – 2014-12-11 14:22:57

+0

另一方面,基于xml矢量的绘图通常比图像更差,特别是在较旧的设备上。不要过早优化,但请牢记这一点。 – 2017-03-03 14:21:20

6

尝试这个

final Button button = (Button) findViewById(R.id.button_id); 
    button.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 
      if(event.getAction() == MotionEvent.ACTION_UP) { 
       button.setBackgroundColor(Color.RED); 
      } else if(event.getAction() == MotionEvent.ACTION_DOWN) { 
       button.setBackgroundColor(Color.BLUE); 
      } 
      return false; 
     } 

    }); 
-1

海最简单的方法是:

将此代码添加到mainactivity。java的

public void start(View view) { 

    stop.setBackgroundResource(R.color.red); 
start.setBackgroundResource(R.color.yellow); 
} 

public void stop(View view) { 
stop.setBackgroundResource(R.color.yellow); 
start.setBackgroundResource(R.color.red); 
} 

,然后在活动主

<button android:id="@+id/start" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="start" android:text="Click"> 

</button><button android:id="@+id/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="stop" android:text="Click"> 

或跟随沿着这tutorial

1

我使用此代码(与连锁反应):

<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/color_gray"> 
<item android:id="@android:id/mask"> 
    <color android:color="@color/color_gray" /> 
</item></ripple> 
1

可以试试这个代码来解决你的问题

<?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/login_selected" /> <!-- pressed --> 
    <item android:state_focused="true" 
    android:drawable="@drawable/login_mouse_over" /> <!-- focused --> 
    <item android:drawable="@drawable/login" /> <!-- default --> 
</selector> 

写这段代码在你的绘制做出新的资源并将它命名为你想要的东西,然后写这drwable在按钮的名称相同,我们指的是图像的src在android系统

+0

这个答案出现在低质量的审查队列中,大概是因为你没有提供任何代码的解释。如果此代码回答此问题,请考虑添加一些文字,说明答案中的代码。这样,你就更有可能获得更多赞扬 - 并帮助提问者学习新东西。 – lmo 2016-09-05 21:41:29

5
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- default --> 
    <item 
     android:state_pressed="false" 
     android:state_focused="false"> 
     <shape 
      android:innerRadiusRatio="1" 
      android:shape="rectangle" > 
      <solid android:color="#00a3e2" /> 

      <padding 
       android:bottom="10dp" 
       android:left="10dp" 
       android:right="10dp" 
       android:top="10dp" /> 

     </shape> 
    </item> 

    <!-- button focused --> 
    <item 
     android:state_pressed="false" 
     android:state_focused="true"> 
     <shape 
      android:innerRadiusRatio="1" 
      android:shape="rectangle" > 
      <solid android:color="#5a97f5" /> 

      <padding 
       android:bottom="5dp" 
       android:left="10dp" 
       android:right="10dp" 
       android:top="5dp" /> 
     </shape></item> 

    <!-- button pressed --> 
    <item 
     android:state_pressed="true" 
     android:state_focused="false"> 
     <shape 
      android:innerRadiusRatio="1" 
      android:shape="rectangle" > 
      <solid android:color="#478df9"/> 
      <padding 
       android:bottom="10dp" 
       android:left="10dp" 
       android:right="10dp" 
       android:top="10dp" /> 
     </shape></item> 
</selector> 
+4

我真的不明白最初的报价“Ya Mahdi aj”... – 2016-11-29 14:07:22

+0

他可能是指**“Hail Mahdi aj”** – CodeIt 2017-04-06 11:32:30

+0

Mahdī,是人类的终极救世主和十二伊玛目的最终伊玛目将与伊萨(耶稣基督)一起出现,以履行为世界带来和平与正义的使命。 – 2018-02-24 07:33:49

0

为了应对适当多长时间你想拥有你的按钮留在你的其他颜色我会建议这个版本:但是你migh

button.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch(event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         button.setBackground(getResources().getDrawable(R.drawable.on_click_drawable)); 
         break; 
        case MotionEvent.ACTION_UP: 
         new java.util.Timer().schedule(
           new java.util.TimerTask() { 
            @Override 
            public void run() { 
             ((Activity) (getContext())).runOnUiThread(new Runnable() { 
              @Override 
              public void run() { 
               button.setBackground(getResources().getDrawable(R.drawable.not_clicked_drawable)); 
              } 
             }); 
            } 
           }, BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION); 
         break; 
        default: 
       } 
       return false; 
      } 
     }); 

BUTTON_CLICK_TIME_AFTER_RELEASE_ANIMATION指示后多少时间[ms]按钮将重置到以前的状态( t想要使用一些布尔值来检查按钮之间没有被使用,这取决于你想实现什么......)。

相关问题