设置不同的 ToggleButtons 来改变同一个 textviewcolor activity
set different ToggleButtons to change one textviewcolor on the same activity
我正在构建一个带有三个切换按钮的应用程序。当按下每个按钮时,它会将相同 activity 中的 textView 更改为三种不同的颜色。当同时按下两个切换按钮时,会将相同的文本视图更改为其他颜色,当同时按下所有三个按钮时,会将文本视图更改为白色。我怎样才能做到这一点?我尝试使用 switch 语句和 if (isChecked),但它有错误
这是我的 MainActivity.java 文件
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
if (redtoggleButton.isChecked()){
june.setTextColor(Color.RED);
}else if (bluetoggleButton.isChecked()){
june.setTextColor(Color.BLUE);
}else if (greentoggleButton.isChecked()){
june.setTextColor(Color.GREEN);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和我的activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="June 2015"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton3"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:minWidth="150dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="60dp"
android:layout_marginTop="150dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Top Rated"
android:id="@+id/button2"
android:minWidth="160dp" />
<Button
android:layout_width="137dp"
android:layout_height="match_parent"
android:text="Random"
android:id="@+id/button3"
android:layout_marginLeft="25dp"
android:minWidth="160dp" />
</LinearLayout>
</LinearLayout>
请帮忙
如有任何帮助,我们将不胜感激!
您的按钮上需要 OnCheckedChangeListener
。试试这个:
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
public void changeColor()
{
if (redtoggleButton.isChecked())
june.setTextColor(Color.RED);
else if (bluetoggleButton.isChecked())
june.setTextColor(Color.BLUE);
else if (greentoggleButton.isChecked())
june.setTextColor(Color.GREEN);
}
查看 Official docs 了解更多信息。
您需要向切换按钮添加 OnCheckedChangedListener
。当您切换其中一个按钮时,将始终执行 onCheckedChanged 中的代码。
int red = 0;
int blue = 0;
int green = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.addOnCheckedChangedListener(toggleListener);
bluetoggleButton.addOnCheckedChangedListener(toggleListener);
greentoggleButton.addOnCheckedChangedListener(toggleListener);
}
private OnCheckedChangedListener toggleListener = new OnCheckedChangedListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
red = redtoggleButton.isChecked() ? 255 : 0;
blue = bluetoggleButton.isChecked() ? 255 : 0;
green = greentoggleButton.isChecked() ? 255 : 0;
if (redtoggleButton.isChecked() && bluetoggleButton.isChecked()
&& greentoggleButton.isChecked()) {
june.setTextColor(Color.WHITE)
} else {
june.setTextColor(Color.rgb(red,blue,green));
}
}
}
将您的整个代码替换为:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton = (ToggleButton) findViewById(R.id.toggleButton);
bluetoggleButton = (ToggleButton) findViewById(R.id.toggleButton2);
greentoggleButton = (ToggleButton) findViewById(R.id.toggleButton3);
june = (TextView) findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
}
@Override
protected void onResume() {
super.onResume();
changeTextColor();
}
public void changeTextColor() {
boolean redToggle = redtoggleButton.isChecked();
boolean greenToggle = greentoggleButton.isChecked();
boolean blueToggle = bluetoggleButton.isChecked();
int redValue;
int greenValue;
int blueValue;
if (redToggle) {
redValue = 255;
} else {
redValue = 0;
}
if (greenToggle) {
greenValue = 255;
} else {
greenValue = 0;
}
if (blueToggle) {
blueValue = 255;
} else {
blueValue = 0;
}
june.setTextColor(Color.rgb(redValue, greenValue, blueValue));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我想通了。
MainActivity.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import static android.graphics.Color.*;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if (!bluetoggleButton.isChecked()&& !greentoggleButton.isChecked()) {
june.setTextColor(RED);
}else if (bluetoggleButton.isChecked()&&!greentoggleButton.isChecked()) {
june.setTextColor(MAGENTA);
}else if (!bluetoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(YELLOW);
}else if (bluetoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(!redtoggleButton.isChecked()&&!greentoggleButton.isChecked()){
june.setTextColor(BLUE);
}else if (redtoggleButton.isChecked()&&!greentoggleButton.isChecked()){
june.setTextColor(MAGENTA);
}else if (!redtoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(CYAN);
}else if (redtoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(!bluetoggleButton.isChecked()&&!redtoggleButton.isChecked()){
june.setTextColor(GREEN);
}else if (bluetoggleButton.isChecked()&&!redtoggleButton.isChecked()){
june.setTextColor(CYAN);
}else if (!bluetoggleButton.isChecked()&&redtoggleButton.isChecked()){
june.setTextColor(YELLOW);
}else if (bluetoggleButton.isChecked()&&redtoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="June 2015"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton3"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:minWidth="150dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="60dp"
android:layout_marginTop="150dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Top Rated"
android:id="@+id/button2"
android:minWidth="160dp" />
<Button
android:layout_width="137dp"
android:layout_height="match_parent"
android:text="Random"
android:id="@+id/button3"
android:layout_marginLeft="25dp"
android:minWidth="160dp" />
</LinearLayout>
</LinearLayout>
感谢大家的帮助:)
我正在构建一个带有三个切换按钮的应用程序。当按下每个按钮时,它会将相同 activity 中的 textView 更改为三种不同的颜色。当同时按下两个切换按钮时,会将相同的文本视图更改为其他颜色,当同时按下所有三个按钮时,会将文本视图更改为白色。我怎样才能做到这一点?我尝试使用 switch 语句和 if (isChecked),但它有错误
这是我的 MainActivity.java 文件
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
if (redtoggleButton.isChecked()){
june.setTextColor(Color.RED);
}else if (bluetoggleButton.isChecked()){
june.setTextColor(Color.BLUE);
}else if (greentoggleButton.isChecked()){
june.setTextColor(Color.GREEN);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和我的activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="June 2015"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton3"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:minWidth="150dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="60dp"
android:layout_marginTop="150dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Top Rated"
android:id="@+id/button2"
android:minWidth="160dp" />
<Button
android:layout_width="137dp"
android:layout_height="match_parent"
android:text="Random"
android:id="@+id/button3"
android:layout_marginLeft="25dp"
android:minWidth="160dp" />
</LinearLayout>
</LinearLayout>
请帮忙 如有任何帮助,我们将不胜感激!
您的按钮上需要 OnCheckedChangeListener
。试试这个:
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeColor();
}
});
public void changeColor()
{
if (redtoggleButton.isChecked())
june.setTextColor(Color.RED);
else if (bluetoggleButton.isChecked())
june.setTextColor(Color.BLUE);
else if (greentoggleButton.isChecked())
june.setTextColor(Color.GREEN);
}
查看 Official docs 了解更多信息。
您需要向切换按钮添加 OnCheckedChangedListener
。当您切换其中一个按钮时,将始终执行 onCheckedChanged 中的代码。
int red = 0;
int blue = 0;
int green = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.addOnCheckedChangedListener(toggleListener);
bluetoggleButton.addOnCheckedChangedListener(toggleListener);
greentoggleButton.addOnCheckedChangedListener(toggleListener);
}
private OnCheckedChangedListener toggleListener = new OnCheckedChangedListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
red = redtoggleButton.isChecked() ? 255 : 0;
blue = bluetoggleButton.isChecked() ? 255 : 0;
green = greentoggleButton.isChecked() ? 255 : 0;
if (redtoggleButton.isChecked() && bluetoggleButton.isChecked()
&& greentoggleButton.isChecked()) {
june.setTextColor(Color.WHITE)
} else {
june.setTextColor(Color.rgb(red,blue,green));
}
}
}
将您的整个代码替换为:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton = (ToggleButton) findViewById(R.id.toggleButton);
bluetoggleButton = (ToggleButton) findViewById(R.id.toggleButton2);
greentoggleButton = (ToggleButton) findViewById(R.id.toggleButton3);
june = (TextView) findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeTextColor();
}
});
}
@Override
protected void onResume() {
super.onResume();
changeTextColor();
}
public void changeTextColor() {
boolean redToggle = redtoggleButton.isChecked();
boolean greenToggle = greentoggleButton.isChecked();
boolean blueToggle = bluetoggleButton.isChecked();
int redValue;
int greenValue;
int blueValue;
if (redToggle) {
redValue = 255;
} else {
redValue = 0;
}
if (greenToggle) {
greenValue = 255;
} else {
greenValue = 0;
}
if (blueToggle) {
blueValue = 255;
} else {
blueValue = 0;
}
june.setTextColor(Color.rgb(redValue, greenValue, blueValue));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我想通了。 MainActivity.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import static android.graphics.Color.*;
public class MainActivity extends Activity {
ToggleButton redtoggleButton;
ToggleButton bluetoggleButton;
ToggleButton greentoggleButton;
TextView june;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redtoggleButton=(ToggleButton)findViewById(R.id.toggleButton);
bluetoggleButton=(ToggleButton)findViewById(R.id.toggleButton2);
greentoggleButton=(ToggleButton)findViewById(R.id.toggleButton3);
june=(TextView)findViewById(R.id.textView);
redtoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if (!bluetoggleButton.isChecked()&& !greentoggleButton.isChecked()) {
june.setTextColor(RED);
}else if (bluetoggleButton.isChecked()&&!greentoggleButton.isChecked()) {
june.setTextColor(MAGENTA);
}else if (!bluetoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(YELLOW);
}else if (bluetoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
bluetoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(!redtoggleButton.isChecked()&&!greentoggleButton.isChecked()){
june.setTextColor(BLUE);
}else if (redtoggleButton.isChecked()&&!greentoggleButton.isChecked()){
june.setTextColor(MAGENTA);
}else if (!redtoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(CYAN);
}else if (redtoggleButton.isChecked()&&greentoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
greentoggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
if(!bluetoggleButton.isChecked()&&!redtoggleButton.isChecked()){
june.setTextColor(GREEN);
}else if (bluetoggleButton.isChecked()&&!redtoggleButton.isChecked()){
june.setTextColor(CYAN);
}else if (!bluetoggleButton.isChecked()&&redtoggleButton.isChecked()){
june.setTextColor(YELLOW);
}else if (bluetoggleButton.isChecked()&&redtoggleButton.isChecked()){
june.setTextColor(WHITE);
}
}else june.setTextColor(BLACK);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="June 2015"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:textSize="50dp" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton2"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New ToggleButton"
android:id="@+id/toggleButton3"
android:layout_gravity="center_horizontal"
android:textOff="Off"
android:textOn="On"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:minWidth="150dp"
android:textSize="20dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minHeight="60dp"
android:layout_marginTop="150dp">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Top Rated"
android:id="@+id/button2"
android:minWidth="160dp" />
<Button
android:layout_width="137dp"
android:layout_height="match_parent"
android:text="Random"
android:id="@+id/button3"
android:layout_marginLeft="25dp"
android:minWidth="160dp" />
</LinearLayout>
</LinearLayout>
感谢大家的帮助:)