0

我正在构建一个带有三个切换按钮的应用程序。当每个被按下时,它会将同一活动中的 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>

请帮助任何帮助将不胜感激!

4

4 回答 4

2

您需要在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));
        }
    }
}
于 2015-08-21T15:39:58.573 回答
1

用这个替换你的整个代码:

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);
    }
}
于 2015-08-21T15:47:10.730 回答
0

你需要一个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);
}

查看官方文档以获取更多信息。

于 2015-08-21T15:29:33.667 回答
0

我能够弄清楚。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>

谢谢大家的帮助:)

于 2015-08-21T16:03:44.807 回答