Android: Increment Button in loop to setVisibility

强颜欢笑 提交于 2019-12-12 05:18:23

问题


I make a game like 4Pics1Word.

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

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

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

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

Thank you

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

Thank you all for your help


Now I tried this:

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

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)

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

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


回答1:


If you don't want to dynamically create the buttons, then it looks like you should be using a ternary operator.

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



回答2:


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

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

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>

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



来源:https://stackoverflow.com/questions/28729067/android-increment-button-in-loop-to-setvisibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!