【问题标题】:Android: Increment Button in loop to setVisibilityAndroid:循环增加按钮到 setVisibility
【发布时间】:2015-04-28 00:57:30
【问题描述】:

我制作了一个像 4Pics1Word 这样的游戏。

现在我有 14 个按钮,我想在循环中设置按钮的可见性。

如果答案有 5 个字母,前 5 个按钮应该是可见的

例如这是我的代码:

int lengthTest = dataArr.get(currentQuestion)[0].length(); // Get Length of the word from the array.
        for (int nr = 0; nr <= lengthTest; nr++) { // My Loop doesnt work
            answer[nr].setVisibility(View.VISIBLE);
        }

这就是我现在所拥有的,但是对于 100 张图片,每次都需要很长时间才能写出来

        answer1.setVisibility(View.VISIBLE); //Button1 Visible because the answer length (lengthTest) is 5
        answer2.setVisibility(View.VISIBLE); //Button2 Visible
        answer3.setVisibility(View.VISIBLE); //Button3 Visible
        answer4.setVisibility(View.VISIBLE); //Button4 Visible
        answer5.setVisibility(View.VISIBLE); //Button5 Visible
        answer6.setVisibility(View.GONE); //Button6 GONE
        answer7.setVisibility(View.GONE);
        answer8.setVisibility(View.GONE);
        answer9.setVisibility(View.GONE);
        answer10.setVisibility(View.GONE);
        answer11.setVisibility(View.GONE);

希望你能理解,对不起我的英语不好

谢谢

我得到了它的工作,有一个 Button[] 如果你想要我稍后发布代码。

谢谢大家的帮助


现在我尝试了这个:

int lengthTest = dataArr.get(currentQuestion)[0].length() - 1;
        for (int i=1; i<15; i++){
              int buttonId = this.getResources().getIdentifier("answer"+i, "string", this.getPackageName());
              Button currentGameButton = (Button)findViewById(buttonId);
              //now you can do whatever you need for this button, for example
              currentGameButton.setVisibility(View.VISIBLE);
              // implement checkButtonVisibility to determine whether this button should be VISIBLE or GONE
            }

我收到了这个错误:

02-26 16:08:41.429: E/AndroidRuntime(31838): FATAL EXCEPTION: main

02-26 16:08:41.429: E/AndroidRuntime(31838): Process: com.developer.flagsofnations, PID: 31838 02-26 16:08:41.429: E/AndroidRuntime(31838): java.lang.NullPointerException 02-26 16:08:41.429: E/AndroidRuntime(31838): at com.developer.flagsofnations.FlagsOfNations.showQuestion(FlagsOfNations.java:673) 02-26 16:08:41.429: E/AndroidRuntime(31838): at com.developer.flagsofnations.FlagsOfNations$1.onClick(FlagsOfNations.java:196) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.view.View.performClick(View.java:4480) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.view.View$PerformClick.run(View.java:18686) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.os.Handler.handleCallback(Handler.java:733) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.os.Handler.dispatchMessage(Handler.java:95) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.os.Looper.loop(Looper.java:157) 02-26 16:08:41.429: E/AndroidRuntime(31838): at android.app.ActivityThread.main(ActivityThread.java:5872) 02-26 16:08:41.429: E/AndroidRuntime(31838): at java.lang.reflect.Method.invokeNative(Native Method) 02-26 16:08:41.429: E/AndroidRuntime(31838): at java.lang.reflect.Method.invoke(Method.java:515) 02-26 16:08:41.429: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852) 02-26 16:08:41.429: E/AndroidRuntime(31838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668) 02-26 16:08:41.429: E/AndroidRuntime(31838): at dalvik.system.NativeStart.main(Native Method)

如果我使用调试器,我可以看到 buttonId = 0 和 currentGameButton = null

我认为问题出在: 第 673 行是currentGameButton.setVisibility(View.VISIBLE); 因为这是 0

【问题讨论】:

  • 所有按钮都是在布局xml文件中动态创建或定义的吗?
  • 您应该为此目的动态添加按钮,但您为什么不使用列表视图或网格视图呢?这些小部件非常易于使用,可以为您提供很多帮助。
  • 我在 xml 中创建按钮

标签: android loops button visibility visible


【解决方案1】:

如果您不想动态创建按钮,那么看起来您应该使用三元运算符。

int lengthTest = dataArr.get(currentQuestion)[0].length();
for (int nr = 0; nr < answer.length; nr++) {
    answer[nr].setVisibility((nr < lengthTest)?View.VISIBLE:View.GONE);
}

【讨论】:

  • 您好,如果我尝试您的解决方案,我收到错误消息:无法将答案解析为我认为的变量,因为按钮 ID 是 answer1 而不仅仅是答案。我尝试但它不起作用,因为我在这里问这个。
  • 这是对您发布的第一个代码块的修改。我假设 answer 应该是你的按钮数组。您发布该代码块的目的是什么?
【解决方案2】:

16/3/15 更新(现在应该可以使用):

使用按钮的 Id 字段以便从代码中动态更改它们的设置。使用某种约定(基于this solution)设置按钮的ID。

意思是,例如,按钮应该在你的 layout.xml 中声明如下:

<RelativeLayout...>
  <Button
     android:id="@+id/gameButton1"
     ... />
  <Button
     android:id="@+id/gameButton2"
     ... />
  ...
  <Button
     android:id="@+id/gameButton14"
     ... />
</RelativeLayout>

然后控制按钮的 Java 代码可能类似于:

for (int i=1; i<15; i++){
  int buttonId = this.getResources().getIdentifier("gameButton"+i, "id", this.getPackageName());
  Button currentGameButton = (Button)findViewById(buttonId);
  //now you can do whatever you need for this button, for example
  currentGameButton.setVisibility(checkButtonVisibility(i));
  // implement checkButtonVisibility to determine whether this button should be VISIBLE or GONE
}

如果这有帮助或者您还有其他问题,请告诉我

【讨论】:

  • 您好,很抱歉我回答迟了,直到现在我还在上学。如果我尝试您的解决方案,我会收到以下错误:FATAL EXCEPTION: main java.lang.NullPointerException
  • @Nic 我修好了。 (getIdentifiier 应该使用 'id')现在应该抛出 NPE。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多