Android批处理设置ImageButton可见性

Android批处理设置ImageButton可见性,android,view,visibility,imagebutton,batch-processing,Android,View,Visibility,Imagebutton,Batch Processing,我想隐藏应用程序启动时的所有图像按钮 我怎样才能存档?代码如下: ImageButton imgBtn[]; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); hideBtn(); } private void hideBtn(){

我想隐藏应用程序启动时的所有图像按钮

我怎样才能存档?代码如下:

ImageButton imgBtn[];

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
                hideBtn();
}

private void hideBtn(){
        for (int i = 0; i < 3; i++) {
            imgBtn[i] = (ImageButton) findViewById("R.id.myBTN"+[i]);
            imgBtn[i].setVisibility(View.GONE);
        }
}
更新: 这是xml代码:

<ImageButton
        android:id="@+id/myBTN1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button" />

<ImageButton
        android:id="@+id/myBTN2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myBTN1"
        android:layout_below="@+id/myBTN1"
        android:layout_marginTop="12dp"
        android:background="@drawable/button" />

<ImageButton
        android:id="@+id/myBTN3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myBTN2"
        android:layout_below="@+id/myBTN2"
        android:layout_marginTop="12dp"
        android:background="@drawable/button" />

findViewById
需要int类型的参数,但您正在传递字符串类型。 您需要首先获取资源id

您可以使用资源类的getIdentifier方法来获取id,请选中此项

你喜欢这样吗

int id=getResources().getIdentifier("myBTN"+[i], "id", "com.mypackage.myapp");
然后


您无法通过这种方式从字符串中获取资源id。请看这里:“myBTN”+[i]应该是“myBTN”+i?@jkjk只需复制粘贴您的代码,它应该是我删除的方块brackets@jkjk很抱歉回复太晚…请发布logcat跟踪…以及xml文件。。。我想可能是你的i值从1到7
<ImageButton
        android:id="@+id/myBTN1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button" />

<ImageButton
        android:id="@+id/myBTN2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myBTN1"
        android:layout_below="@+id/myBTN1"
        android:layout_marginTop="12dp"
        android:background="@drawable/button" />

<ImageButton
        android:id="@+id/myBTN3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/myBTN2"
        android:layout_below="@+id/myBTN2"
        android:layout_marginTop="12dp"
        android:background="@drawable/button" />
int id=getResources().getIdentifier("myBTN"+[i], "id", "com.mypackage.myapp");
imgBtn[i] = (ImageButton) findViewById(id);