Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用单个文本视图创建可单击的循环?_Java_Android - Fatal编程技术网

Java 使用单个文本视图创建可单击的循环?

Java 使用单个文本视图创建可单击的循环?,java,android,Java,Android,多亏了Udacity的课程,我这周才刚刚开始学习XML和Java代码。。。我刚刚完成了一半,但我想做我自己的毫无意义的应用 我在红色背景上有一些黑色文字 当我单击文本时,我告诉Java将文本更改为白色,将背景更改为蓝色,它确实这样做了 当我再次单击文本时,我希望它返回到黑色和红色,但它没有 我知道为什么会发生这种情况,但我不知道如何解决。 这是因为我的文本有一个onClick来更改颜色并显示在屏幕上。因此,每次单击都会调用相同的onClick 我该怎么做才能使它在每次单击时在两个阶段的颜色之间不

多亏了Udacity的课程,我这周才刚刚开始学习XML和Java代码。。。我刚刚完成了一半,但我想做我自己的毫无意义的应用

我在红色背景上有一些黑色文字

当我单击文本时,我告诉Java将文本更改为白色,将背景更改为蓝色,它确实这样做了

当我再次单击文本时,我希望它返回到黑色和红色,但它没有

我知道为什么会发生这种情况,但我不知道如何解决。 这是因为我的文本有一个
onClick
来更改颜色并显示在屏幕上。因此,每次单击都会调用相同的
onClick

我该怎么做才能使它在每次单击时在两个阶段的颜色之间不断交替

我的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>
我做了一些挖掘,发现我可以使用“onClickListener”?但无论我做什么,我都无法让它发挥作用。也许我无法正确理解语法。 有没有其他方法……也许更简单?在我看来,这似乎是一项相当容易的任务,但这项任务确实让我感到困惑

执行以下操作:

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*/));
    }
}

您可以根据文本视图的文本决定设置的文本和背景色。

您可以将显示状态保持为布尔值。类似的(未经测试):


这成功了!谢谢虽然我不得不把它放在公共空间显示(字符串消息)不是公共空间嘿(视图五),但这样做的技巧,谢谢!我想我要去查一些if/else的东西!
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*/));
    }
}
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;
}