简体   繁体   English

使用单个TextView进行可点击的循环?

[英]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.. 由于Udacity的课程,我本周才刚刚开始学习XML和Java代码。我还没走到一半,但是我感觉自己做了一个毫无意义的应用程序。

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. 单击文本时,我告诉Java将文本更改为白色,将背景更改为蓝色,并且确实如此。

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. 发生这种情况是因为我的文本具有onClick来更改颜色并显示在屏幕上。 So naturally, the same onClick is being called on each click. 因此,自然而然,每次点击都会调用相同的onClick

What do I do to make it constantly alternate between the two stages of colors every time it's clicked? 如何使它每次单击时都在两个颜色阶段之间不断交替显示?

My XML is : 我的XML是:

<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 Java代码是

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"? 我做了一些挖掘,发现可以使用“ 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. 您可以根据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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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