简体   繁体   中英

Make a clickable loop with a single TextView?

I only just started learning XML and Java code this week, thanks to Udacity's course... I'm barely halfway through but I felt like making my own pointless app..

So I have some black text on a red background.

When I click the text, I tell Java to change the text to white and background to blue, and it does.

When I click the text again, I want it to go back to the black and red, but it doesn't.

I know why this happens but I don't know how to fix this. It happens because my text has an onClick to change the colors and display to screen. So naturally, the same onClick is being called on each click.

What do I do to make it constantly alternate between the two stages of colors every time it's clicked?

My XML is :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c04"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textDisplay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:background="#c04"
        android:fontFamily="sans-serif-smallcaps"
        android:gravity="center"
        android:onClick="Hey"
        android:padding="30dp"
        android:text="Hey!"
        android:textAllCaps="true"
        android:textColor="#000"
        android:textSize="150sp"
        android:textStyle="bold" />

</LinearLayout>

And the Java code is

public void displaying(String message) {
    TextView whatever = (TextView) findViewById(R.id.textDisplay);
    whatever.setText(message);
    whatever.setTextColor(Color.rgb(255, 255, 255));
    whatever.setBackgroundColor(Color.rgb(82, 218, 199));
}

public void Hey(View v) {
    displaying("Ho!");
}

I did some digging and found I could use the "onClickListener"? But no matter what I do I just couldn't get it to work. Maybe I'm unable to get the syntax right. Is there another method ..perhaps simpler? It seems like a fairly easy task in my head but this one really has me stumped.

Do the following:

public void Hey(View v) {
    TextView whatever = (TextView) findViewById(R.id.textDisplay);
    if(whatever.getText().toString().equals("Hey!")){
         whatever.setText("Ho!");
         whatever.setTextColor(Color.rgb(255, 255, 255));
         whatever.setBackgroundColor(Color.rgb(82, 218, 199));
    } else if (whatever.getText().toString().equals("Ho!"){
         whatever.setText("Hey!");
         whatever.setTextColor(Color.rgb(0, 0, 0));
         whatever.setBackgroundColor(Color.rgb(/*red RGB*/));
    }
}

You decide wich text and background color you set based on the text of your TextView.

You can keep the state of your display in a boolean. Something like that (untested):

boolean stateOfMyTextView = true;

public void displaying(String message) {
    TextView whatever = (TextView) findViewById(R.id.textDisplay);
    whatever.setText(message);
    if (stateOfMyTextView == true) {
        // First color
        whatever.setTextColor(Color.rgb(255, 255, 255));
        whatever.setBackgroundColor(Color.rgb(82, 218, 199));
    }
    else {
        // Other color
        whatever.setTextColor(Color.rgb(0, 0, 0));
        whatever.setBackgroundColor(Color.rgb(255, 0, 0)); 
    }
    // change state of the boolean
    stateOfMyTextView = !stateOfMyTextView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM