【问题标题】:Android batch set ImageButton VisibilityAndroid批量设置ImageButton Visibility
【发布时间】:2013-03-05 21:47:47
【问题描述】:

我想在应用启动时隐藏所有图像 ImageButton;

我怎样才能存档呢?这是代码:

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

更新 我更改了 hideBtn 方法,然后在测试时强制关闭

 -159   private void hideBtn(){
 -160       for (int i = 0; i < 3; i++) {
 -161           int id = getResources().getIdentifier("myBTN"+i,"id", "com.my.app");
 -162           imgBtn[i] = (ImageButton)findViewById(id);
 -163           imgBtn[i].setVisibility(View.GONE);
 -164       }
 -165   }

崩溃日志猫报告:

03-18 16:46:05.125: E/AndroidRuntime(19463):    at dalvik.system.NativeStart.main(Native Method)
03-18 16:46:05.125: E/AndroidRuntime(19463): Caused by: java.lang.NullPointerException
03-18 16:46:05.125: E/AndroidRuntime(19463):    at com.my.app.Main.hideBtn(Main.java:162)
03-18 16:46:05.125: E/AndroidRuntime(19463):    at com.my.app.Main.onCreate(Main.java:61)
03-18 16:46:05.125: E/AndroidRuntime(19463):    at android.app.Activity.performCreate(Activity.java:5108)
03-18 16:46:05.125: E/AndroidRuntime(19463):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 16:46:05.125: E/AndroidRuntime(19463):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-18 16:46:05.125: E/AndroidRuntime(19463):    ... 11 more

更新: 这是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" />

【问题讨论】:

标签: android view visibility imagebutton batch-processing


【解决方案1】:

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

可以使用资源类的getIdentifier方法获取id,查看getIdentifier

这样做

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

然后

imgBtn[i] = (ImageButton) findViewById(id);

【讨论】:

  • "myBTN"+[i] 应该是 "myBTN"+i ?
  • @jkjk 只是复制粘贴您的代码,应该是我删除方括号
  • @jkjk 抱歉回复晚了...请发布 logcat 跟踪...还有 xml 文件...我认为您的 i 值可能是 1 到 7
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
相关资源
最近更新 更多