按两次按钮使其工作

时间:2017-12-01 09:19:17

标签: android xml button words

我是Android的初学者,我正在创建一个与SpellBee相关的应用程序。问题是我在屏幕上有2个按钮,一个用于下一个字,一个用于前一个字。在a default first word shows on screen开始。在第一次按下一个单词后,它将显示数据库列表中的下一个单词。但是当我按下前一个按钮时,在第一次单击时它什么都不做,在第二次单击时它显示前一个单词,然后单击下一个单词按钮它什么都不做,在第二次单击时它移动到下一个单词。我怎样才能解决这个两次按钮问题This is my XML for Buttons

按钮代码

public void next_click(View view) {
  if(iterator <= wordList.size()) {
    word.setText(wordList.get(iterator));
    definition.setText(definitionList.get(iterator));
    usage.setText(usageList.get(iterator));
    iterator++;
    /*if (iterator == wordList.size()) {
      Toast.makeText(Level1Activity.this, "Level 1 Completed", 
      Toast.LENGTH_SHORT).show();
      next_word.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent i = new Intent(Level1Activity.this, 
          SelectLevelActivity.class);
          startActivity(i);
       }
     });
    }*/
  }
}

public void previous_click(View view) {
  if(iterator != 0) {
    --iterator;
    word.setText(wordList.get(iterator));
    definition.setText(definitionList.get(iterator));
    usage.setText(usageList.get(iterator));
  }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.msiprestige.spellbee.Level1Activity"
android:background="@drawable/bookshelf"
android:alpha="0.9">

<TextView
    android:id="@+id/textView10"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="@string/level_1"
    android:textColor="#fff"
    android:textSize="50sp"
    android:visibility="visible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/word"
    android:layout_width="261dp"
    android:layout_height="54dp"
    android:layout_marginTop="23dp"
    android:ems="10"
    android:gravity="center_horizontal"
    android:textColor="#fff"
    android:textColorLink="#fff"
    android:textSize="22sp"
    android:textStyle="bold"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView10" />

<TextView
    android:id="@+id/textView12"
    android:layout_width="80dp"
    android:layout_height="24dp"
    android:layout_marginStart="4dp"
    android:text="@string/definition"
    android:textColor="#fff"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.36" />

<TextView
    android:id="@+id/textView13"
    android:layout_width="54dp"
    android:layout_height="24dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="109dp"
    android:text="@string/usage"
    android:textColor="#fff"
    android:textSize="18sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView12" />



<TextView
    android:id="@+id/definition"
    android:layout_width="277dp"
    android:layout_height="106dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="48dp"
    android:width="0dip"
    android:ems="10"
    android:textColor="#fff"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/word" />

<TextView
    android:id="@+id/usage"
    android:layout_width="277dp"
    android:layout_height="106dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="27dp"
    android:width="0dip"
    android:ems="10"
    android:textColor="#fff"
    android:textSize="15sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/definition" />

<Button
    android:id="@+id/previous_word"
    android:layout_width="154dp"
    android:layout_height="56dp"
    android:layout_marginBottom="38dp"
    android:layout_marginLeft="36dp"
    android:onClick="previous_click"
    android:text="Previous Word"
    android:focusable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" />

<Button
    android:id="@+id/next_word"
    android:layout_width="154dp"
    android:layout_height="56dp"
    android:layout_marginBottom="36dp"
    android:layout_marginRight="28dp"
    android:onClick="next_click"
    android:text="@string/next_word"
    android:focusable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

我认为如果您将代码更改为此代码,您的问题将会得到解决! 要知道您的错误,您在将单词设置为textview后更改了迭代器变量。并且在if next_click 中,它应该小于 wordList.size() - 1 以避免 IndexOutOfBound 异常!

public void next_click(View view) {
  if(iterator < wordList.size()-1) {
    iterator++;
    word.setText(wordList.get(iterator));
    definition.setText(definitionList.get(iterator));
    usage.setText(usageList.get(iterator));
    startAnotherActivity(iterator);
  }
}

public void previous_click(View view) {
  if(iterator != 0) {
    iterator--;
    word.setText(wordList.get(iterator));
    definition.setText(definitionList.get(iterator));
    usage.setText(usageList.get(iterator));
    startAnotherActivity(iterator);
  }
}

private void startAnotherActivity(int your_extra_here) {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("key_for_extra", your_extra_here);
    startActivity(intent);
}

我觉得你需要将迭代器传递给另一个活动,因此我在这里为你提供了额外的帮助。如果你不需要它只是省略那部分!!!

答案 1 :(得分:0)

这是合乎逻辑的错误。当您的列表索引以0 开头时,当您显示第一个单词时,我猜迭代器索引已变为1 。现在,当您单击下一步时,在索引1显示单词并增加迭代器。现在迭代器是2

现在,点击上一个按钮。这里第一个迭代器递减,即迭代器为1 。并显示已在屏幕上显示的单词索引1 。所以你觉得它什么都不做,但它实际上执行你的逻辑。只是你的逻辑设置相同的单词。现在再次单击上一个按钮,迭代器将为0 ,并在索引0处显示单词。

下一个按钮的情况相同。现在,点击下一步按钮。你首先设置单词,即在索引0显示单词,它已经显示,并增加迭代器,它将变为1 。现在再次单击下一个按钮,它会在索引1显示单词,并将迭代器增加到2

确保您的计数器,即迭代器始终指向当前显示的当前单词索引。当单击next时,看到iterator + 1在单词列表的范围内,如果是,则递增它并在该索引处显示单词。类似地,在显示前一个单词时,检查index-1是否在单词列表的范围内,如果是,则递减它然后在该索引处显示单词。希望你明白。

相关问题