当我点击某个特定按钮时,按钮背景的颜色会改变,而其他按钮保持其默认颜色。但是,在运行应用程序时,虽然代码中的其余逻辑运行良好,但按钮不会更改其颜色。我仍然无法弄清楚出了什么问题。任何帮助将不胜感激。感谢。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer_test);
adult = (Button) findViewById(R.id.btnAdult);
peer = (Button) findViewById(R.id.btnPeer);
material = (Button) findViewById(R.id.btnMaterial);
physical = (Button) findViewById(R.id.btnPhysical);
//Set button disabled, when new activity is turned on
flag.setEnabled(false);
alarm = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
...
OnClickListener codes :
if (textDemo.getText().toString() == "Record") {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(physical)) {
// do something
physical.setText("PHYSICAL ON CLICK WORKS");
physicalPrompt = "Yes";
physical.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
adult.setBackgroundResource(R.drawable.button_selector);
peer.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(material)) {
// do something
material.setText("MATERIAL ON CLICK WORKS");
materials = "Yes";
material.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
peer.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
physical.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(peer)){
peer.setText("PEER ON CLICK WORKS");
peers = "Yes";
peer.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
physical.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(adult)){
adult.setText("ADULT ON CLICK WORKS");
peers = "Yes";
peer.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
physical.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else {
// do something else
textDemo.setText("Error!");
}
}
};
physical.setOnClickListener(listener);
material.setOnClickListener(listener);
peer.setOnClickListener(listener);
adult.setOnClickListener(listener);
}
}
}
在value / colors.xml中,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="buttonNotPressed">#2196F3</color>
<color name="buttonPressed">#1976D2</color>
<!--<color name="colorHighlightFAB">#B6B6B6</color>-->
<color name="ColorPrimary">#E91E63</color>
</resources>
额外代码:
public class Timer_Test extends ActionBarActivity {
//Declare widgets
Button buttonStart, flag, back, timeButton, adult, peer, material, physical;
TextView timerTextView, tvTest, interval, child, status, id, session,
passName, tvStartTime, textDemo;
答案 0 :(得分:0)
首先尝试此解决方案并让我知道它是否正常工作 -
private View.OnClickListener mFirstListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.red);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mSecondListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.blue);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mThirdListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.green);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mFourthListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.light_blue);
((Button)v).setText("New Text");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(mFirstListener);
button2.setOnClickListener(mSecondListener);
button3.setOnClickListener(mThirdListener);
button4.setOnClickListener(mFourthListener);
}
推荐方式(仅限使用XML) -
在drawable文件夹中创建一个名为button_states的文件,并将以下代码放在 -
中<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/buttonPressed" /> <!--Pressed State-->
<item android:state_focused="true" android:drawable="@color/buttonPressed" /> <!--Focused State-->
<item android:drawable="@color/grey" /> <!--Normal State-->
</selector>
现在在您的xml中设置按钮背景,如下所示 -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:padding="10dp"
**android:background="@drawable/button_states"**
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="42dp"/>
然后你根本不需要改变代码中的颜色,它会对你有用。
如果你使用推荐的方式摆脱所有听众的这一行 - v.setBackgroundResource(R.color.blue);
但是对于文本更改,你仍然需要保留以下行((Button)v).setText("New Text");
。
希望它有所帮助。
答案 1 :(得分:0)
我没有工作,因为我设置了exitFadeDuration
和enterFadeDuration
:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="40"
android:enterFadeDuration="120">
删除后,一切都很好!
答案 2 :(得分:-1)
按钮比仅改变一种颜色更复杂......
Android按钮是使用相当多的9个补丁和状态drawables制作的。只是尝试设置背景颜色可能不会重置以前设置的资源样式。尝试使用更改的背景颜色创建另一个状态可绘制资源,并将其指定给按钮。