Okey所以我创建了一些布尔值,根据它们我想设置一个按钮的颜色。 所以我要说我有
boolean test1 = true;
boolean test2 = false;
True为绿色,false为红色。 现在我希望我的程序根据布尔值将颜色设置为按钮。
package com.example.kamil.tmpsadmin;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String button1nazwa = "Kamil", button1register = "SJZ-RG78",
button2nazwa = "Daniel", button2register = "SJZ-7782",
button3nazwa = "Kajetan", button3register = "SJZ-6669",
button4nazwa = "Szymon", button4register = "SJZ-GRA3",
button5nazwa = "Bartek", button5register = "SJZ-MET2",
button6nazwa = "Paweeł", button6register = "SJZ-KOZAK";
Boolean button1dostepnosc = true, button2dostepnosc = true, button3dostepnosc = false, button4dostepnosc = false, button5dostepnosc = false, button6dostepnosc = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(button1nazwa + "\n" + button1register + "\n");
TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText(button2nazwa + "\n" + button2register + "\n");
TextView textView3 = (TextView) findViewById(R.id.textView3);
textView3.setText(button3nazwa + "\n" + button3register + "\n");
TextView textView4 = (TextView) findViewById(R.id.textView4);
textView4.setText(button4nazwa + "\n" + button4register + "\n");
TextView textView5 = (TextView) findViewById(R.id.textView5);
textView5.setText(button5nazwa + "\n" + button5register + "\n");
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText(button6nazwa + "\n" + button6register + "\n");
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(MainActivity.this, Zmiana.class));
}
});
}
答案 0 :(得分:0)
我认为你可以用if-then-else语句来做到这一点。
if(test1) {
//set button1 color to green
}
else {
//set button1 color to red
}
if(test2) {
//set button2 color to green
}
else {
//set button2 color to red
}
这将是一个带阵列的解决方案。请注意,colorBool
中的所有值均默认为false。
int count = 10; //leads to ten buttons
boolean[] colorBool = new boolean[count];
Button[] buttons = new Button[count];
for(int i = 0; i < colorBool.length; i++) {
if(colorBool[i]) {
buttons[i].setBackgroundColor(Color.GREEN);
}
else {
buttons[i].setBackgroundColor(Color.RED);
}
}
答案 1 :(得分:0)
你只需要检查一下:
if(test1 == true)
{
button.setBackgroundColor(Color.GREEN);
}
if(test2 == true)
{
button.setBackgroundColor(Color.RED);
}