[英]Change the color of a button temporary
I programmed a quiz, now I want that every time the correct answer/button( mButtonChoice
) is pressed the background of the button will be colored green, later it will be set as default. 我编写了一个测验,现在我希望每次按正确的答案/按钮( mButtonChoice
)时,按钮的背景将变为绿色,以后将其设置为默认值。
So the correct buttons should be "colored" green and then later as before. 因此,正确的按钮应“涂成绿色”,然后再像以前一样。 How do I can manage to do that? 我如何才能做到这一点?
QuizActicity (Here are the questions and the choices):- QuizActicity(以下是问题和选择):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
createDialog();
Button dialogButton = (Button) findViewById(R.id.dialogbtn);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView shareTextView = (TextView) findViewById(R.id.share);
shareTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello!");
myIntent.putExtra(Intent.EXTRA_TEXT, "My highscore in Quizzi is very high! I bet you can't beat me except you are cleverer than me. Download the app now!");
startActivity(Intent.createChooser(myIntent, "Share with:"));
}
});
mQuestionLibrary.shuffle();
setSupportActionBar((Toolbar) findViewById(R.id.nav_action));
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Able to see the Navigation Burger "Button"
((NavigationView) findViewById(R.id.nv1)).setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_stats:
startActivity(new Intent(QuizActivity.this, Menu2.class));
break;
case R.id.nav_about:
startActivity(new Intent(QuizActivity.this, Menu3.class));
break;
}
return true;
}
});
mScoreView = (TextView) findViewById(R.id.score_score);
mQuestionView = (TextView) findViewById(R.id.question);
mButtonChoice1 = (Button) findViewById(R.id.choice1);
mButtonChoice2 = (Button) findViewById(R.id.choice2);
mButtonChoice3 = (Button) findViewById(R.id.choice3);
List<Button> choices = new ArrayList<>();
choices.add(mButtonChoice1);
choices.add(mButtonChoice2);
choices.add(mButtonChoice3);
updateQuestion();
for (final Button choice : choices) {
choice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (choice.getText().equals(mAnswer)) {
updateScore();
updateQuestion();
Toast.makeText(QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(QuizActivity.this, "Wrong... Try again!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score", mScore); // pass score to Menu2
startActivity(intent);
}
}
});
}
}
private void updateQuestion() {
if (mQuestionNumber < mQuestionLibrary.getLength()) {
mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));
mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber++);
} else {
Toast.makeText(QuizActivity.this, "Last Question! You are very intelligent!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(QuizActivity.this, Menu2.class);
intent.putExtra("score", mScore);
startActivity(intent);
}
}
private void updateScore() {
mScoreView.setText(String.valueOf(++mScore));
SharedPreferences mypref = getPreferences(MODE_PRIVATE);
int highScore = mypref.getInt("highScore", 0);
if (mScore > highScore) {
SharedPreferences.Editor editor = mypref.edit();
editor.putInt("highScore", mScore);
editor.apply();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return mToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
private void createDialog() {
dialog = new Dialog(this);
dialog.setTitle("Tutorial");
dialog.setContentView(R.layout.popup_menu1_1);
closeButton = (TextView) dialog.findViewById(R.id.closeTXT);
}
}
As a Java developer you should get used to use the online documentation of the components you use. 作为Java开发人员,您应该习惯于使用所使用组件的在线文档 。
When you look at the API description of Androids Button class ( https://developer.android.com/reference/android/widget/Button.html ) you might find that it inherits the method setBackgroundColor(int color)
from Androids View
class ( https://developer.android.com/reference/android/view/View.html#setBackgroundColor(int) ) 当您查看Androids Button类的API描述( https://developer.android.com/reference/android/widget/Button.html )时,您可能会发现它继承了Androids View
类的setBackgroundColor(int color)
方法( https://developer.android.com/reference/android/view/View.html#setBackgroundColor(int) )
Then you only need to call that method at the two correct places in your program. 然后,您只需要在程序的两个正确位置调用该方法。
Setting the "background color" for a Button
is a little bit tricky. 为Button
设置“背景色”有点棘手。 By default, every Button
has a background managed by the Android system that includes things like rounded corners, colors only within a certain area, etc. If you simply call setBackgroundColor()
, you will lose all of that sophisticated behavior and be left with a flat rectangle. 默认情况下,每个Button
都有一个由Android系统管理的背景,其中包括圆角,仅在特定区域内的颜色等内容。如果仅调用setBackgroundColor()
,则将失去所有这些复杂的行为,并留下一个平面矩形。
I assume you're extending from AppCompatActivity
here. 我假设您是从AppCompatActivity
扩展而来的。 If not, this answer won't work. 如果没有,此答案将无效。
To change a Button
's background color while still keeping the rounded shape and the ripple effect, use ViewCompat.setBackgroundTintList()
as follows: 若要更改Button
的背景颜色,同时仍保持圆形和波纹效果,请使用ViewCompat.setBackgroundTintList()
,如下所示:
Button dialogButton = (Button) findViewById(R.id.dialogbtn);
int color = ContextCompat.getColor(this, R.color.yourColorResourceHere);
ColorStateList tintList = ColorStateList.valueOf(color);
ViewCompat.setBackgroundTintList(dialogButton, tintList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.