屏幕移动到下一个问题,即使答案是错误的



我正在编写一个测验应用程序,这里的主要问题是当一个选项被点击问题被改变时,我想如果选项是错误的问题保持相同的选项变成红色..

class Level1 : AppCompatActivity(),View.OnClickListener {
lateinit var binding: ActivityLevel1Binding
var mQuestionList=QuestionsAndAnswer.getQuestions()
lateinit var imageView: ImageView
private var mCurrentPosition: Int = 0 // this question in model class//

private var mSelectedOptionPostion: Int = 0  // this is current option where user clicked for answer///
lateinit var progress:ProgressBar
private lateinit var  tvOptionOne:TextView
private lateinit var tvOptionTwo:TextView
private lateinit var tvOptionThree:TextView
private lateinit var tvOptionFour:TextView
lateinit var progressBar: ProgressBar
lateinit var tvProgressBar: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLevel1Binding.inflate(layoutInflater)
setContentView(binding.root)
mQuestionList=QuestionsAndAnswer.getQuestions()
setQuestion()
binding.tvOptionFour.setOnClickListener(this)
binding.tvOptionThree.setOnClickListener (this)
binding.tvOptionTwo.setOnClickListener (this)
binding.tvOptionOne.setOnClickListener (this)
binding.progressBar
}
private fun setQuestion() {
setDefault()
imageView=binding.quesImage
binding.apply {
val questions= mQuestionList[mCurrentPosition]
progressBar.progress = mCurrentPosition
tvProgressBar.text = "$mCurrentPosition" + "/" + progressBar.max
binding.quesImage.setImageResource(questions.image)
tvOptionOne.text = questions.optionOne
tvOptionTwo.text = questions.optionTwo
tvOptionThree.text = questions.optionThree
tvOptionFour.text = questions.optionFour
}
}
override fun onClick(v: View?) {
when(v?.id){
R.id.tvOptionOne->{
Selected(1,binding.tvOptionOne)
if( mCurrentPosition != mQuestionList.size-1){
mCurrentPosition ++
setQuestion()
val questions=mQuestionList?.get(mCurrentPosition -1)
if (questions?.correctAnswer!=mSelectedOptionPostion){
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
}
else {
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}
}
else {
val intent=Intent(this@Level1,ScoringActivity::class.java)
startActivity(intent)
finish()
}
}
R.id.tvOptionTwo->{
Selected(2,binding.tvOptionTwo)
if( mCurrentPosition != mQuestionList.size-1){
mCurrentPosition ++
setQuestion()
val questions=mQuestionList?.get(mCurrentPosition -1)
if (questions?.correctAnswer!=mSelectedOptionPostion){
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
mCurrentPosition
Toast.makeText(this,"wrong answer",Toast.LENGTH_SHORT).show()
}
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}
else {
val intent=Intent(this@Level1,ScoringActivity::class.java)
startActivity(intent)
finish()
}
}
R.id.tvOptionThree->{
Selected(3,binding.tvOptionThree)
if( mCurrentPosition != mQuestionList.size-1){
mCurrentPosition ++
setQuestion()
val questions=mQuestionList?.get(mCurrentPosition -1)
if (questions?.correctAnswer!=mSelectedOptionPostion){
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
Toast.makeText(this,"wrong answer",Toast.LENGTH_SHORT).show()
}
else{
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}
}
else {
val intent=Intent(this@Level1,ScoringActivity::class.java)
startActivity(intent)
finish()
}
}
// at last question on wrong answer click no action happens like no wrong answer drawable file is called //

R.id.tvOptionFour->{
Selected(4,binding.tvOptionFour)
if( mCurrentPosition != mQuestionList.size-1){
mCurrentPosition ++
setQuestion()
var questions=mQuestionList?.get(mCurrentPosition -1)
if (questions?.correctAnswer!=mSelectedOptionPostion){
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
}
else {
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}
}
else {
val intent=Intent(this@Level1,ScoringActivity::class.java)
startActivity(intent)
finish()
}
}
}
}

// here answers are maatched //

private fun AnswerView(answer: Int, drawAbleView: Int) {
when (answer) {
1 -> {
binding.tvOptionOne.background = ContextCompat.getDrawable(this, drawAbleView)
}
2 -> {
binding.tvOptionTwo.background = ContextCompat.getDrawable(this, drawAbleView)
}
3 -> {
binding.tvOptionThree.background = ContextCompat.getDrawable(this, drawAbleView)
}
4 -> {
binding.tvOptionFour.background = ContextCompat.getDrawable(this, drawAbleView)
}
}
}
private fun Selected(selected: Int, tv: TextView) {
setDefault()
mSelectedOptionPostion = selected
tv.setTextColor(Color.parseColor("#FF03DAC5"))
tv.setTypeface(tv.typeface, Typeface.BOLD)
tv.background = ContextCompat.getDrawable(this,
R.drawable.selected_tex_view)
}
private fun setDefault() {
val options = ArrayList<TextView>()
options.add(0, binding.tvOptionOne)
options.add(1, binding.tvOptionTwo)
options.add(2, binding.tvOptionThree)
options.add(3, binding.tvOptionFour)
for (option in options) {
option.setTextColor(Color.parseColor("#FF03DAC5"))
option.typeface = Typeface.DEFAULT
option.background = ContextCompat.getDrawable(this, R.drawable.round_text_view)
}
}
}

试着像这样改变你的onClick逻辑:

注意,我声明了两个方法来避免代码重复

override fun onClick(v: View?) {
when(v?.id){
R.id.tvOptionOne->{
Selected(1, binding.tvOptionOne)
if(mCurrentPosition < mQuestionList.size){
setQuestion()
checkQuestion()
}
else startScoreActivity()
}
R.id.tvOptionTwo->{
Selected(2,binding.tvOptionTwo)
if(mCurrentPosition < mQuestionList.size){
setQuestion()
checkQuestion()
}
else startScoreActivity()
}
R.id.tvOptionThree->{
Selected(3,binding.tvOptionThree)
if( mCurrentPosition < mQuestionList.size){
setQuestion()
checkQuestion()
}
else startScoreActivity()
}
R.id.tvOptionFour->{
Selected(4,binding.tvOptionFour)
if( mCurrentPosition < mQuestionList.size) {
setQuestion()

}
else startScoreActivity()
}
}
}
private fun checkQuestion() {
var questions=mQuestionList?.get(mCurrentPosition)
if (questions?.correctAnswer!=mSelectedOptionPostion){
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
}
else {
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
mCurrentPosition++
}
}
private fun startScoreActivity() {
val intent=Intent(this@Level1,ScoringActivity::class.java)
startActivity(intent)
finish()
}

我想你应该试着移动

if (questions?.correctAnswer!=mSelectedOptionPostion) {              
AnswerView(mSelectedOptionPostion,R.drawable.wrong_answer)
} else {
mCurrentPosition ++
setQuestion()
AnswerView(mSelectedOptionPostion,R.drawable.correct_answer)
}

相关内容

  • 没有找到相关文章

最新更新