2010-10-07 174 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想要使用一些布爾值來檢查按鈕之間沒有被使用,這取決於你想實現什麼......)。

相關問題