安卓:在循环setVisibility增量按钮 [英] Android: Increment Button in loop to setVisibility

查看:303
本文介绍了安卓:在循环setVisibility增量按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作出这样4Pics1Word游戏。

I make a game like 4Pics1Word.

现在我有14个按钮,我想设置在一个循环按钮的知名度。

Now I have 14 buttons and I want to set the visibility of the Buttons in a loop.

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

If the answer has 5 letters, the first 5 Buttons should be Visible

例如,这是我的code:

For example this is my code:

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照片管理它take's长期每次写

And that is what I have now, but for 100 Pics it take´s to long to write it every time

        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);

我希望你明白这对不起我的英文不好

I hope you understand it sorry for my bad english

感谢您

我得到了它的工作,用按钮[]如果你想我后来张贴code。

I got it work, with a Button[] If you want I post the code later.

感谢大家的帮助。

现在我想这样的:

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
            }

我得到这个错误:

I got this error:

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

16 02-26:08:41.429:E / AndroidRuntime(31838):工艺: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):在com.developer.flagsofnations.FlagsOfNations.showQuestion(FlagsOfNations.java:673)
02-26 16:08:41.429:E / AndroidRuntime(31838):在com.developer.flagsofnations.FlagsOfNations $ 1.onClick(FlagsOfNations.java:196)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.view.View.performClick(View.java:4480)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.view.View $ PerformClick.run(View.java:18686)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.os.Handler.handleCallback(Handler.java:733)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.os.Handler.dispatchMessage(Handler.java:95)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.os.Looper.loop(Looper.java:157)
02-26 16:08:41.429:E / AndroidRuntime(31838):在android.app.ActivityThread.main(ActivityThread.java:5872)
02-26 16:08:41.429:E / AndroidRuntime(31838):在java.lang.reflect.Method.invokeNative(本机方法)
02-26 16:08:41.429:E / AndroidRuntime(31838):在java.lang.reflect.Method.invoke(Method.java:515)
02-26 16:08:41.429:E / AndroidRuntime(31838):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:852)
02-26 16:08:41.429:E / AndroidRuntime(31838):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
02-26 16:08:41.429:E / AndroidRuntime(31838):在dalvik.system.NativeStart.main(本机方法)

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

If I use the debugger I can see that buttonId = 0 and currentGameButton = null

我认为这个问题是在这里:
673线是 currentGameButton.setVisibility(View.VISIBLE);
因为这是0

I think the problem is here: Line 673 is currentGameButton.setVisibility(View.VISIBLE); because this is 0

推荐答案

更新于16/3/15 (现在应该工作)

Updated on 16/3/15 (should work now):

使用您的按钮的ID字段,以动态它们的设置从code改变。使用某种约定的设置按钮的标识(基于<一个href=\"http://stackoverflow.com/questions/14647810/easier-way-to-get-views-id-string-by-its-id-int\">this解决方案)。

Use the Id field of your buttons in order to dynamically change the setting of them from the code. Set the button's IDs using some kind of convention (based on this solution).

的含义,例如,按钮,应在layout.xml声明如下:结果

Meaning, for example, the buttons should be declared in your layout.xml as following:

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

然后你的java code控制您的按钮可以是这样的:

And then your java code that control your buttons can be something like:

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
}

让我知道如果这能帮助或者您有其他问题

Let me know if this helps or you have additional questions

这篇关于安卓:在循环setVisibility增量按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆