如何在Android上一次性获取所有按钮ID

时间:2014-03-25 15:32:17

标签: android android-layout android-xml

我的Button中有16个Activity,我必须在onCreate()中初始化这些内容。 有没有办法在一行代码中初始化所有按钮?(循环等) 代码应该从R.id.布局处理所有按钮XML并处理....

7 个答案:

答案 0 :(得分:12)

假设您将按钮命名为button_0, button_1, .. button_15。你可以这样做:

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

答案 1 :(得分:5)

好吧,如果这些按钮中的所有16个都在一个视图或布局中,那么您可以执行以下操作。

ArrayList<View> allButtons; 
allButtons = ((LinearLayout) findViewById(R.id.button_container)).getTouchables();

这假定您的容器(在此示例中为LinearLayout)不包含Touchable的{​​{1}}。

答案 2 :(得分:2)

  1. 使用Butterknife查看注入库
  2. 下载Android StudioIntellij IDEA的{​​{3}}插件 从当前版面初始化所有观看次数 1点击

答案 3 :(得分:1)

此方法采用布局内的所有按钮,希望对您有所帮助。易于实现,几乎可以在每个项目中使用它,而无需任何库。

mxnet  preprocessor definition MXNET_PREDICT_ONLY=1 will enable naiveEngine
mkldnn cmake flag MKLDNN_THREADING=SEQ

示例

public List<Button> getAllButtons(ViewGroup layout){
        List<Button> btn = new ArrayList<>();
        for(int i =0; i< layout.getChildCount(); i++){
            View v =layout.getChildAt(i);
            if(v instanceof Button){
                btn.add((Button) v);
            }
        }
        return btn;
    }

答案 4 :(得分:0)

对于xamarin android:

List<Button>() allButtons = new List<Button>();
for (int i = 1; i < 15; i++)
{
    int id = this.Resources.GetIdentifier("btn" + i.ToString(), "id", this.PackageName);
    Button btn = (Button)FindViewById(id);
    allButtons.Add(btn);

}

答案 5 :(得分:0)

如果您使用的是Kotlin,并且按钮的ID以 button1,button2 ... button16 的形式出现,则可以执行以下操作:

Traceback (most recent call last):
    30: from /usr/local/bin/danger:23:in `<main>'
    29: from /usr/local/bin/danger:23:in `load'
    28: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/bin/danger:5:in `<top (required)>'
    27: from /Library/Ruby/Gems/2.6.0/gems/claide-1.0.3/lib/claide/command.rb:334:in `run'
    26: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/commands/pr.rb:60:in `run'
    25: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/commands/pr.rb:60:in `new'
    24: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/danger_core/environment_manager.rb:30:in `initialize'
    23: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/danger_core/environment_manager.rb:30:in `new'
    22: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/local_git_repo.rb:45:in `initialize'
    21: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/local_git_repo.rb:95:in `found_pull_request'
    20: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/support/pull_request_finder.rb:17:in `call'
    19: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/support/pull_request_finder.rb:31:in `check_if_any_pull_request!'
    18: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/support/pull_request_finder.rb:51:in `pull_request'
    17: from /Library/Ruby/Gems/2.6.0/gems/danger-8.0.4/lib/danger/ci_source/support/pull_request_finder.rb:93:in `remote_pull_request'
    16: from /Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/client/pull_requests.rb:33:in `pull_request'
    15: from /Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/connection.rb:19:in `get'
    14: from /Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/connection.rb:156:in `request'
    13: from /Library/Ruby/Gems/2.6.0/gems/sawyer-0.8.2/lib/sawyer/agent.rb:94:in `call'
    12: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/connection.rb:198:in `get'
    11: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/connection.rb:492:in `run_request'
    10: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/rack_builder.rb:153:in `build_response'
     9: from /Library/Ruby/Gems/2.6.0/gems/faraday-http-cache-2.2.0/lib/faraday/http_cache.rb:115:in `call'
     8: from /Library/Ruby/Gems/2.6.0/gems/faraday-http-cache-2.2.0/lib/faraday/http_cache.rb:135:in `call!'
     7: from /Library/Ruby/Gems/2.6.0/gems/faraday-http-cache-2.2.0/lib/faraday/http_cache.rb:187:in `process'
     6: from /Library/Ruby/Gems/2.6.0/gems/faraday-http-cache-2.2.0/lib/faraday/http_cache.rb:283:in `fetch'
     5: from /Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/middleware/follow_redirects.rb:61:in `call'
     4: from /Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/middleware/follow_redirects.rb:73:in `perform_with_redirection'
     3: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/response.rb:11:in `call'
     2: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/response.rb:65:in `on_complete'
     1: from /Library/Ruby/Gems/2.6.0/gems/faraday-1.0.1/lib/faraday/response.rb:12:in `block in call'
/Library/Ruby/Gems/2.6.0/gems/octokit-4.18.0/lib/octokit/response/raise_error.rb:16:in `on_complete': GET https://api.github.com/repos/MyOwner/MyRepo/pulls/219: 404 - Not Found // See: https://developer.github.com/v3/pulls/#get-a-single-pull-request (Octokit::NotFound)

答案 6 :(得分:0)

如果您只知道容器ID或按钮ID都不同,请尝试以下操作:

活动(在setContentView之前的create方法上)

List<Integer> idButtons= new ArrayList<>();
//container_button is my button container
RelativeLayout containerButton = findViewById(R.id.container_button);
for(int i =0; i < containerButton.getChildCount(); i++){
    View v = containerButton.getChildAt(i);
    if(v instanceof Button){
        idButtons.add(((Button) v).getId());
    }
 }

布局

<RelativeLayout
    android:id="@+id/container_button"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_weight="1"
    android:layout_marginEnd="0dp">

<Button
    android:id="@+id/buttonac"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/red_button"
    android:textColor="@android:color/white"
    android:text="AC"
    android:onClick="pulsacion" />

<Button
    android:id="@+id/buttondel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/buttonac"
    android:layout_alignBottom="@+id/buttonac"
    android:background="@drawable/red_button"
    android:textColor="@android:color/white"
    android:text="DEL"
    android:onClick="pulsacion" />

    [...]
</RelativeLayout>