英文:
How can I change the color of a button that is clicked but make it so that only one buttons' color can be changed at the same time?
问题
如何使得在点击按钮时,不显示文本,而是更改每个按钮的背景颜色,且同一时间只有一个按钮可以具有该颜色?
package com.example.scrolltes1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.KeyEventDispatcher;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static android.graphics.Color.BLUE;
import static android.graphics.Color.GRAY;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
boolean constrain = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
button1.setBackgroundColor(Color.RED); // Change background color
button2.setBackgroundColor(GRAY); // Reset other button colors
button3.setBackgroundColor(GRAY);
break;
case R.id.button2:
button1.setBackgroundColor(GRAY);
button2.setBackgroundColor(Color.GREEN);
button3.setBackgroundColor(GRAY);
break;
case R.id.button3:
button1.setBackgroundColor(GRAY);
button2.setBackgroundColor(GRAY);
button3.setBackgroundColor(Color.BLUE);
break;
}
}
}
英文:
<h1>How can I make it so that instead of displaying text whenever a button is clicked, instead the background color of each button changes and only one button can be that color at the same time? </h1>
package com.example.scrolltes1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.KeyEventDispatcher;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static android.graphics.Color.BLUE;
import static android.graphics.Color.GRAY;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
boolean constrain = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "Button 3 clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
<p1>would it just be possible to replace everything inside of the Toast.makeText parenthesis with something that changes background color?</p1>
答案1
得分: 0
你可以为每个按钮创建一个特定颜色的自定义布局:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Toast ToastMessage = Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.layout.custom_color);
ToastMessage.show();
break;
// 更多情况
}
}
查看这篇文章获取更多关于解决方案的详细信息:https://www.android-examples.com/change-toast-message-background-color-in-android/
如果这对你有帮助,请毫不犹豫地接受它作为答案
英文:
you could create a custom layout with a specific color for each button :
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1
Toast ToastMessage = Toast.makeText(this,"Button 1 clicked", Toast.LENGTH_SHORT);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.layout.custom_color);
ToastMessage.show();
break;
// more cases
}
}
See this article for more details about the solution : https://www.android-examples.com/change-toast-message-background-color-in-android/
If this helps, don't hesitate to accept it as an answer
专注分享java语言的经验与见解,让所有开发者获益!
评论