【问题标题】:while loop directs to black mode screen of colorful activity screenwhile 循环指向彩色活动屏幕的黑色模式屏幕
【发布时间】:2021-12-05 06:58:54
【问题描述】:

每次打开 Kotlin 循环,只是简单的循环,它就以黑屏模式出现,不起作用,请给点建议?我的理想是用handler post delay来迭代Star Quiz() fun方法,主要问题点是do while looping,它不起作用iterate,只是黑屏,没有bug错误,请给我任何建议,没有do while looping,它工作正常

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quizy_app)

    milisecond = 0

    rightAnswer = findViewById(R.id.txtRightAnswer)

    txtQuestion = findViewById(R.id.txtQuestion)

    edtAnswer = findViewById(R.id.edtAnswer)

    progressbar = findViewById(R.id.progresscircle)

    txtProgress = findViewById(R.id.textprogress)

    var i =5

     StarQuizy()

    handler = Handler()

    do{
            handler?.postDelayed({StarQuizy()},17000)
    }while (i==5)

}
fun StarQuizy(){

        random = Random

        //   rightAnswer?.visibility = View.INVISIBLE

        question = Question[random.nextInt(Question.size)]
        txtQuestion?.text = mixLetter(question)
        countDownTimer()
}
fun countDownTimer(){

    cTimer = object :CountDownTimer(15000, 1000){
        override fun onTick(millisUntilFinished: Long) {

            milisecond  = millisUntilFinished
            rightAnswer?.setText(""+milisecond!!/1000)
        }

        override fun onFinish() {

        }

    }.start()
}

【问题讨论】:

  • 你定义了 i 迭代器值,但你没有在任何地方增加它?!!
  • 据我所知,do while 定义会一次又一次地重复,只要它处于预定义的条件下,这种无限循环,没有结束,所以 i = 5,
  • do { module statement } while(i==5) ,它会一次又一次地迭代,我猜你的评论可能会纠正'for looping'
  • 我不明白为什么你有一个无限循环?你的目的是什么?
  • 好的,测验将在下一个测验后每 30 秒更新一次,持续时间为 30 秒,依此类推……只要应用程序打开,它就会继续运行,是无限循环吗?

标签: java android kotlin


【解决方案1】:
<gradient android:startColor = "#80F40303"
    android:centerColor = "#8B055798"
    android:endColor = "#8000FF0C"
    android:angle = "90"
    android:type = "linear"/>
<RelativeLayout
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"
tools:context=".MainActivity">

<VideoView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/videoView"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Quizy"
    android:textSize="40sp"
    android:textStyle="bold"
    android:fontFamily="cursive"
    android:layout_marginTop="30sp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Started"
    android:id="@+id/start"
    android:layout_centerInParent="true"/>
class MainActivity : AppCompatActivity() {

var videoPlay:VideoView?=null
var start: Button?=null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    videoPlay = findViewById(R.id.videoView)

    start = findViewById(R.id.start)

    var videoUri = Uri.parse("android.resource://"+packageName+"/"+R.raw.backgroundvideo)

    videoPlay?.setVideoURI(videoUri)

    videoPlay?.start()

    start?.setOnClickListener {
        var intent = Intent(this,quizy_app::class.java)
        startActivity(intent)
    }
}

}

<RelativeLayout
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"
tools:context=".quizy_app">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/background"
    android:scaleType="centerCrop"/>



<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#A6000000"/>
<TextView
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Right Answer"
    android:textSize="30sp"
    android:id="@+id/txtRightAnswer"
    android:textColor="@color/white"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/txtRightAnswer"
    android:layout_above="@id/container">
    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:id="@+id/progresscircle"
        android:max="15000"
        android:progress="0"
        android:layout_alignParentBottom="true"
        android:progressDrawable="@drawable/circular"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/textprogress"/>

</RelativeLayout>

<LinearLayout
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/container"
    android:layout_centerInParent="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Question"
        android:textSize="20sp"
        android:id="@+id/txtQuestion1"
        android:padding="5dp"
        android:background="#CC0A0A"
        android:textColor="@color/white"/>
    <TextView
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtQuestion"
        android:hint="Your Question is Here"
        android:textColorHint="@color/white"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:padding="10dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Your Answer"
        android:textColorHint="@color/white"
        android:textSize="20sp"
        android:id="@+id/edtAnswer"
        android:textColor="@color/white"
        android:textAlignment="center"/>

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">

    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:onClick="showAnswer"
        android:text="SHOW" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="nextQuestion"
        android:text="NEXT" />

    <Button
        android:id="@+id/btnCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:onClick="checkAnswer"
        android:text="CHECK" />
</RelativeLayout>
class quizy_app : AppCompatActivity() {

var Question = arrayOf("Web Development", "Android Development", "NetWorking",
        "Graphic Desginer", "Programm Desginer")

var rightAnswer : TextView? = null

var txtQuestion : TextView? = null

var edtAnswer : EditText? = null

lateinit var random: Random

lateinit var question: String

var handler : Handler?=null

var view : View?=null


// CountDown Timer

var cTimer:CountDownTimer?=null

// Progress Bar

var progressbar : ProgressBar?=null

var txtProgress : TextView?=null

var milisecond : Long?= null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quizy_app)

    milisecond = 0

    rightAnswer = findViewById(R.id.txtRightAnswer)

    txtQuestion = findViewById(R.id.txtQuestion)

    edtAnswer = findViewById(R.id.edtAnswer)

    progressbar = findViewById(R.id.progresscircle)

    txtProgress = findViewById(R.id.textprogress)

    var i =5

     StarQuizy()

    handler = Handler()

    do{
            handler?.postDelayed({StarQuizy()},17000)
    }while (i==5)

}
fun StarQuizy(){

        random = Random
        rightAnswer?.visibility = View.INVISIBLE
        question = Question[random.nextInt(Question.size)]
        txtQuestion?.text = mixLetter(question)
        countDownTimer()

}
fun countDownTimer(){

    cTimer = object :CountDownTimer(15000, 1000){
        override fun onTick(millisUntilFinished: Long) {

            milisecond  = millisUntilFinished
            rightAnswer?.setText(""+milisecond!!/1000)
        }

        override fun onFinish() {

        }

    }.start()


}

fun showAnswer(view: View){
   rightAnswer?.visibility = View.VISIBLE
    rightAnswer?.text = question
}
fun nextQuestion(view: View){
    StarQuizy()
}
fun checkAnswer(view: View){
  if(txtQuestion?.text.toString().equals(question, ignoreCase = true)){
      RightAnswer()
      Toast.makeText(this, "Correct Answer", Toast.LENGTH_LONG).show()
  }else{
      WrongAnswer()
      Toast.makeText(this, "Incorrect Answer", Toast.LENGTH_LONG).show()
  }
}
fun RightAnswer(){
    rightAnswer?.visibility = View.VISIBLE
    rightAnswer?.text = question
    rightAnswer?.setBackgroundColor(Color.GREEN)

    var handler = Handler()

    handler?.postDelayed({
        StarQuizy()
    }, 3000)
}
fun WrongAnswer(){
    rightAnswer?.visibility = View.VISIBLE
    rightAnswer?.text = question
    rightAnswer?.setBackgroundColor(Color.RED)

    var handler = Handler()

    handler?.postDelayed({
        StarQuizy()
    }, 3000)
}

fun mixLetter(word: String):String{
    var character = word.toCharArray()
    for(i in character.indices){
        var randomIndex = (Math.random()*character.size).toInt()
        var temp = character[i]
        character[i] = character[randomIndex]
        character[randomIndex] = temp
    }
    return String(character)
}

}

【讨论】:

    猜你喜欢
    • 2016-04-27
    • 1970-01-01
    • 2017-09-24
    • 2011-08-21
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多