【问题标题】:press button twice to make it work按两次按钮使其工作
【发布时间】:2017-12-01 09:19:17
【问题描述】:

我是 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>

【问题讨论】:

  • 发布按钮点击的代码,也不是图片格式。发布为文本
  • 你能发布你的点击执行代码吗?和 Button 在 xml 中不需要可聚焦的属性
  • 贴出你的整个xml布局,很有可能焦点被父视图偷走了
  • 我已经尝试将焦点设置为 false。还是不行

标签: android xml button words


【解决方案1】:

我认为,如果您将代码更改为此代码,您的问题将得到解决! 要知道您的错误,您在将单词设置为 textview 后更改了 iterator 变量。并且在 next_click 的 if 中,它应该小于 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);
}

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

【讨论】:

  • 是的。成功了。
  • 是的,我想触发另一个活动。请帮我做。
  • 这可能有效,但我只想在迭代器到达我的 wordlist 的最后一个单词时触发一个新活动。假设我有 10 个单词,当我再次点击达到第 10 个单词时,它应该带我去另一个活动
  • 您只需要添加一个 if 语句并检查它何时达到列表大小的末尾。然后在那个 if 语句中调用 startAnotherActivity.
【解决方案2】:

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

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

与下一步按钮相同。现在,单击下一步按钮。您首先设置单词,即 在索引 0 处显示单词,它已经显示并递增 iterator 将变为 1。现在再次单击下一个按钮,它会显示 index 1 处的单词 并将 iterator 增加到 2

确保您的计数器(即迭代器)始终指向当前显示的当前单词索引。单击下一步时,看到 iterator+1 在单词列表的范围内,如果是,则将其递增并显示该索引处的单词。同样,在显示前一个单词时,检查 index-1 是否在单词列表的范围内,如果是,则将其递减,然后显示该索引处的单词。希望你能理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 2020-06-29
    相关资源
    最近更新 更多