Android: 如何使用条件语句更改 TextColor

Android: How to change TextColor with a conditional statement

我正在尝试制作一个简单的人数计数器应用程序,其中的按钮将 increment/decrement 人数。我试图设定一个条件,如果人数(计数)超过 15,则将文本设为红色。我尝试执行 text_name!!.setTextColor(Color.Red),但它似乎不起作用。我在我的项目中附加了文件。我正在 MVP 架构下制作应用程序。

Contract.kt

package com.example.peoplecounter

import android.graphics.Color

interface Contract {
    interface View {
        fun setCount(count: String)
        fun setTotalCount(count: String)
        fun changeTextColor()
        fun hideButton()
        fun viewButton()

    }

    interface Model {
        fun getCount(): String
        fun getFlag(): Boolean
       // fun getZeroFlag() : Boolean
        fun getTotalCount(): String
        fun reset()
        fun inc()
        fun decr()
        //fun Zero()

    }

    interface Presenter {
        fun onAddCountButtonPressed()
        fun onMinusCountButtonPressed()
        fun onResetButtonPressed()
        fun onOverCapacity()
        fun isZero()
        fun getCount() :Int
    }
} 

Model.kt

class Model : Contract.Model {
    private var count = 0
    private var totalCount = 0
    private var overcapacity = false
    //private var isZero = false

    override fun getCount(): String = count.toString()
    override fun getFlag(): Boolean = overcapacity
    override fun getTotalCount(): String = totalCount.toString()
   // override fun getZeroFlag() : Boolean = isZero

    override fun reset() {
        count = 0
        totalCount = 0
    }

    override fun inc() {
        count += 1
        totalCount += 1
        if(count > 15){
            overcapacity = true;
        }

    }

    override fun decr() {
        count -= 1
        if(count<0){
            count = 0
        }
    }


}

Presenter.kt

package com.example.peoplecounter

import com.example.peoplecounter.Contract

class Presenter (private val model: Contract.Model, private val view: Contract.View): Contract.Presenter {

    override fun onAddCountButtonPressed() {
        model.inc()
        view.setCount(model.getCount())
        view.setTotalCount(model.getTotalCount())
    }

    override fun onMinusCountButtonPressed() {
        model.decr()
        view.setCount(model.getCount())
    }

    override fun onResetButtonPressed() {
        model.reset()
        view.setCount(model.getCount())
        view.setTotalCount(model.getTotalCount())
    }

    override fun onOverCapacity(){
        if(model.getFlag() == true){
            view.changeTextColor()
        }
    }

    override fun isZero(){
        if(Integer.parseInt(model.getCount())  == 0){
            view.hideButton();
        }
        else{
            view.viewButton();
        }
    }

    override fun getCount(): Int {
        return Integer.parseInt(model.getCount())
    }
}

MainActivity.kt

package com.example.peoplecounter

import android.content.Context
import android.content.SharedPreferences
import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity(), Contract.View {
    private lateinit var presenter: Contract.Presenter
    private var peoplecount: TextView? = null
    private var totalcount: TextView? = null
    private var plusbutton: Button? = null
    private var minusbutton: Button? = null
    private var resetbutton: Button? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        peoplecount = findViewById(R.id.people_count_number)
        totalcount = findViewById(R.id.total_number_text)
        plusbutton = findViewById(R.id.increment_button)
        minusbutton = findViewById(R.id.decrement_button)
        resetbutton = findViewById(R.id.reset_button)

        presenter = Presenter(Model(), this)
        setOnClickListeners()
        presenter.isZero()
        presenter.onOverCapacity()
    }

    private fun setOnClickListeners() {
        plusbutton!!.setOnClickListener {
            presenter.onAddCountButtonPressed()
        }
        minusbutton!!.setOnClickListener {
            presenter.onMinusCountButtonPressed()
        }
        resetbutton!!.setOnClickListener {
            presenter.onResetButtonPressed()
        }
    }


    override fun setCount(count: String) {
        people_count_number!!.text = count
    }

    override fun setTotalCount(count: String) {
        total_number_text!!.text = count
    }

    override fun changeTextColor() {
        people_count!!.setTextColor(Color.CYAN)
        people_count_number!!.setTextColor(Color.CYAN);
    }

    override fun hideButton() {
        decrement_button?.visibility = View.GONE
    }

    override fun viewButton() {
        decrement_button?.visibility = View.VISIBLE
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/total_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="Total: "
            android:textSize="26sp" />

        <TextView
            android:id="@+id/total_number_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textSize="26sp" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+id/reset_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:background="@android:color/black"
            android:text="RESET"
            android:textColor="@android:color/white" />
    </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal" >

    <TextView
        android:id="@+id/people_count_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="0"
        android:textSize="35sp"
        app:layout_constraintRight_toLeftOf="@id/people_count"/>

        <TextView
            android:id="@+id/people_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text=" People"
            android:textSize="35sp" />

    </LinearLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:panel="http://schemas.android.com/apk/res/it.kronplatz"
        android:id="@+id/MainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="50dp"
        android:layout_marginStart="50dp"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    <RelativeLayout
        android:id="@+id/ButtonLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_centerHorizontal="true"
        android:baselineAligned="false" >

    <Button
        android:id="@+id/decrement_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="left"
        android:background="@android:color/holo_purple"
        android:text="-"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/increment_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:background="@android:color/holo_purple"
        android:text="+"
        android:textColor="@android:color/white" />

    </RelativeLayout>

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我们关注的方法是 Presenter.kt 中的 OnOverCapacity()、MainActivity.kt 中的 changeTextColor() 和 Model.kt

中的 getFlag()

提前致谢!

我觉得可以把onOverCapacity()放在函数onAddCountButtonPressed()里面,如果超过15,就继续调用onOverCapacity(),即

override fun onAddCountButtonPressed() {
   ...
   ...
   ...
   if (model.getCount() > 15) {
      onOverCapacity()
   }
}

此外,我认为如果您重置计数或减去计数且计数不超过 15,则需要调用一个函数来恢复颜色