我正在開發Android應用程序。我想要4個按鈕水平放置在屏幕的底部。在這4個按鈕中,2個按鈕上有圖像。按鈕的邊框應爲黑色,邊框應儘可能薄。當我點擊按鈕時,我希望按鈕的背景應該更改爲藍色,而不改變邊框的顏色,並且應該保持該顏色一段時間。我如何在Android中實現這種情況?如何在Android中點擊時更改按鈕的顏色?
回答
轉寄此,
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);
}
一種方法是在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>
bgalt
和bgnorm
在繪製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。 希望這有助於。
試試這個......
首先創建一個名爲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是用於按下按鈕和正常對焦的兩個圖像。
如果您想在按下按鈕時更改該按鈕的背景圖像或顏色,那麼只需複製此代碼並粘貼到您的項目中的精確位置,如下所述。
<!-- 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>
保存此代碼繪製文件夾 「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>
謝謝你,使用樣式可以減少應用程序的大小。不要使用可繪製的好主意 – 2014-12-11 14:22:57
另一方面,基於xml矢量的繪圖通常比圖像更差,特別是在較舊的設備上。不要過早優化,但請牢記這一點。 – 2017-03-03 14:21:20
嘗試這個
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;
}
});
海最簡單的方法是:
將此代碼添加到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
我使用此代碼(與連鎖反應):
<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>
可以試試這個代碼來解決你的問題
<?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系統
這個答案出現在低質量的審查隊列中,大概是因爲你沒有提供任何代碼的解釋。如果此代碼回答此問題,請考慮添加一些文字,說明答案中的代碼。這樣,你就更有可能獲得更多讚揚 - 並幫助提問者學習新東西。 – lmo 2016-09-05 21:41:29
<?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>
我真的不明白最初的報價「Ya Mahdi aj」... – 2016-11-29 14:07:22
他可能是指**「Hail Mahdi aj」** – CodeIt 2017-04-06 11:32:30
Mahdī,是人類的終極救世主和十二伊瑪目的最終伊瑪目將與伊薩(耶穌基督)一起出現,以履行爲世界帶來和平與正義的使命。 – 2018-02-24 07:33:49
爲了應對適當多長時間你想擁有你的按鈕留在你的其他顏色我會建議這個版本:但是你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想要使用一些布爾值來檢查按鈕之間沒有被使用,這取決於你想實現什麼......)。
- 1. 如何更改顏色點擊按鈕?
- 2. 不能僅在點擊時更改按鈕顏色(Android)
- 3. 更改點擊按鈕的顏色
- 4. 更改按鈕點擊按鈕顏色暫時在C#
- 5. 離子 - 如何更改點擊時的按鈕顏色
- 6. 點擊更改javafx按鈕顏色?
- 7. 點擊(Javascript)時如何更改按鈕顏色?
- 8. 如何在WP7中單擊時更改按鈕的顏色?
- 9. 如何保存在用戶點擊的按鈕更改顏色
- 10. 如何更改點擊按鈕的顏色
- 11. 如何更改按鈕上的組件顏色點擊
- 12. 如何改變點擊時的按鈕顏色
- 13. 如何通過點擊按鈕來更改網格顏色?
- 14. 如何在點擊Dundas Dashboard v 2.5時更改按鈕的顏色
- 15. 點擊按鈕顏色沒有改變
- 16. 如何在點擊時更改按鈕顏色,圖形和文字?
- 17. 單擊按鈕時更改按鈕顏色
- 18. 更改NavigationDrawer的顏色點擊顏色
- 19. 如何在點擊按鈕時更改按鈕的Css類
- 20. 如何更改UIActionsheet按鈕的顏色?
- 21. 如何更改按鈕的顏色
- 22. 更改顏色的按鈕
- 23. 在旋轉框中選中時只更改android按鈕顏色
- 24. Android ...單擊時更改按鈕顏色,但我有許多按鈕?
- 25. 如何更改Silverlight中單選按鈕的點的顏色?
- 26. 單擊時如何更改按鈕的顏色,並在下次單擊時恢復爲默認顏色?
- 27. 使用javascript更改點擊按鈕的div的字體顏色
- 28. 如何在android中按下按鈕/文本視圖時更改文本顏色
- 29. 更改臨時按鈕的顏色
- 30. 什麼使按鈕在鼠標點擊時改變顏色?
這將永久性地設置BG,也就是說,如果用戶單擊後退按鈕,背景將變爲紅色。 – methode 2010-10-07 13:33:41
@methode:ok請參閱我已更正oldanswer的新答案 – 2010-10-07 13:39:41