如何让所有按钮的ID在同一时间在Android [英] How to get all Buttons ID's in one time on Android

查看:210
本文介绍了如何让所有按钮的ID在同一时间在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有16 按钮在我的活动,我必须初始化这些里面的onCreate()。
有什么办法来初始化一个单行code所有按钮?(循环等)
code应采取一切按钮 R.id。 XML 布局和流程....

I have 16 Buttons in my Activity and I have to initialize those inside onCreate(). Is there any way to initialize all buttons in one single line code?(loops etc.) Code should take all buttons R.id. from XML Layout and process....

推荐答案

比方说,你叫你的按钮 button_0,button_1 .. button_15 。你可以这样做:

Let's say you named your button button_0, button_1, .. button_15. You can do:

for (int i = 0; i < 16; i++) {
    int id = getResources().getIdentifier("button_"+i, "id", getPackageName());
    button[i] = (Button) findViewById(id);
}

这篇关于如何让所有按钮的ID在同一时间在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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