我正在构建一个带有三个切换按钮的应用程序。当每个被按下时,它会将同一活动中的 textView 更改为三种不同的颜色。当两个切换按钮同时按下时,它会将相同的 textview 更改为其他颜色,当所有三个按钮同时按下时,它将 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>
请帮助任何帮助将不胜感激!