我制作了像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);
我希望你能理解我的坏英语
谢谢
我得到了它,使用了按钮[]如果你想我稍后发布代码。
谢谢大家的帮助
现在我尝试了这个:
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
答案 0 :(得分:0)
如果您不想动态创建按钮,那么您应该使用三元运算符。
int lengthTest = dataArr.get(currentQuestion)[0].length();
for (int nr = 0; nr < answer.length; nr++) {
answer[nr].setVisibility((nr < lengthTest)?View.VISIBLE:View.GONE);
}
答案 1 :(得分:0)
于2015年3月16日更新 (现在应该可以使用):
使用按钮的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
}
如果这有帮助,或者您有其他问题,请告诉我