到目前為止,我的應用程式有 2 個螢屏并且在開發中正常作業。它已經有 2 個按鈕('toss' 和 'startGame' 可以正常作業。現在我添加了一個名為 'balls' 的按鈕,旨在更改 4 個 TextViews 的背景顏色('ball1' 到 'ball4')。但是,按下一次什么也不做,再次按下會導致應用程式停止,并顯示訊息“GC Clicker has stop”或“GC Clicker keep stopping”。這是我的 MainActivity.kt 的第一部分:
package com.example.golfclicker
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import android.graphics.Color
import android.widget.Button
import android.content.Intent
import android.media.AudioManager
import android.media.SoundPool
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val soundPool:SoundPool?
// Link variables to xml views
val player1Name = findViewById<EditText>(R.id.PersonName1)
val handicap1 = findViewById<EditText>(R.id.hpInput1)
val player2Name = findViewById<EditText>(R.id.PersonName2)
val handicap2 = findViewById<EditText>(R.id.hpInput2)
val startGame = findViewById<Button>(R.id.startGame)
val toss = findViewById<Button>(R.id.toss)
val bkgnd1 = findViewById<TextView>(R.id.Background1)
val bkgnd1a = findViewById<TextView>(R.id.Background1a)
val bkgnd1b = findViewById<TextView>(R.id.Background1b)
val bkgnd2 = findViewById<TextView>(R.id.Background2)
val bkgnd2a = findViewById<TextView>(R.id.Background2a)
val bkgnd2b = findViewById<TextView>(R.id.Background2b)
val balls = findViewById<Button>(R.id.balls)
val ball1 = findViewById<TextView>(R.id.ball1)
val ball2 = findViewById<TextView>(R.id.ball2)
val ball3 = findViewById<TextView>(R.id.ball3)
val ball4 = findViewById<TextView>(R.id.ball4)
var priSec = true // True:Primaries False:Secondaries
var tossedP1 = "" //String values to pass to Intent
var tossedHp1 =""
var tossedP2 = ""
var tossedHp2 =""
var tossed = false //has Toss button been used?
//Set up soundpool and load the sound file
soundPool = SoundPool(2,AudioManager.STREAM_MUSIC,0)
val sound1 = soundPool.load(baseContext,R.raw.computer_keyboard,1)
val sound2 = soundPool.load(baseContext,R.raw.coins,1)
//Handle balls button click
balls.setOnClickListener {
if(priSec==true) {
with(ball1) { setBackgroundColor(Color.parseColor("button_blue")) }
with(ball2) { setBackgroundColor(Color.parseColor("red")) }
with(ball3) { setBackgroundColor(Color.parseColor("black")) }
with(ball4) { setBackgroundColor(Color.parseColor("yellow")) }
} else {
with(ball1) { setBackgroundColor(Color.parseColor("green")) }
with(ball2) {setBackgroundColor(Color.parseColor("pink")) }
with(ball3) {setBackgroundColor(Color.parseColor("brown")) }
with(ball4) {setBackgroundColor(Color.parseColor("white")) }
}
priSec = !priSec //toggle primary/secondary
}
//Handle 'Toss' button click
toss.setOnClickListener {
var ran = (0..1).random() //Toss coin
// val ran = 1 //checking name-swapping
歡迎大家提出意見。
uj5u.com熱心網友回復:
Color.parseColor(...)作業方式與你想象的不同。您可以像這樣決議顏色:
Color.parseColor("#ff77bb")
換句話說,您必須傳遞顏色的十六進制代碼
uj5u.com熱心網友回復:
這條線是錯誤的..因為你沒有定義顏色
setBackgroundColor(Color.parseColor("button_blue")
嘗試
setBackgroundColor(Color.parseColor("#39b8db")
要么
context.getResources().getColor(android.R.color.white)
uj5u.com熱心網友回復:
我終于找到了以下使用colors.xml中定義的顏色的作品。我不想在我的主代碼中包含十六進制顏色,因為我可能想在以后編輯它們。
ball1.setBackgroundColor(getResources().getColor(R.color.button_blue))
謝謝尼亞杰;這與您的上一個建議相似。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433899.html
上一篇:如何在反應中獲取地圖回傳按鈕的值
下一篇:QML鍵盤快捷鍵單擊按鈕
