具有多个按钮的片段-都具有相同的ID-如何隐藏所有按钮?

时间:2018-08-02 12:12:06

标签: java android android-fragments

我有一个片段,并且在其中填充了另一个布局文件。这意味着页面上有多个具有相同ID的按钮:

<Button android:layout_height="30dp"
        android:layout_width="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        android:text="Delete"
        android:id="@+id/delete_seed" tools:layout_editor_absoluteX="0dp"
        android:textSize="10sp" android:paddingTop="5dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="5dp"
        android:padding="5dp"/>

所以我打电话给

Button deletebutton = rootView.findViewById(R.id.delete_seed);
deletebutton.setVisibility(View.GONE);

它仅删除第一个。如何定位所有按钮?

3 个答案:

答案 0 :(得分:0)

您已在一个片段中包含一个布局。要定位该按钮,您需要找到相对于所包含布局而不是rootView的按钮ID。

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
    }
}

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.14'
}

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
    maven { url 'https://jitpack.io' }
    maven { url 'https://ci-artifactory.corda.r3cev.com/artifactory/corda-releases' }
}

apply plugin: 'java'
apply plugin: 'idea' //legacy technique of adding plugin
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'net.corda.plugins.cordapp'
apply plugin: 'net.corda.plugins.cordformation'
apply plugin: 'net.corda.plugins.quasar-utils'


lombok {
    version = '1.18.0'
    sha256 = ""
}

sourceSets {
    main {
        resources {
            srcDir "config/dev"
        }
    }
    test {
        resources {
            srcDir "config/test"
        }
    }
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/integration-test/java')
        }
    }
}

tasks.withType(JavaCompile) {
    options.compilerArgs << "-parameters" // Required for passing named arguments to your flow via the shell.
}

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    compile("org.postgresql:postgresql")
    compile("com.graphql-java:graphql-spring-boot-starter:4.2.0")
    compile("com.graphql-java:graphiql-spring-boot-starter:4.2.0")
    compile("com.graphql-java:graphql-java-tools:5.2.0")
    compile("com.fasterxml.jackson.core:jackson-databind")
    //compile("org.postgresql:postgresql:9.4-1206-jdbc42")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile group: 'junit', name: 'junit', version: '4.12'



    cordaCompile "$corda_release_group:corda-core:$corda_release_version"
    cordaCompile "$corda_release_group:corda-finance:$corda_release_version"
    cordaCompile "$corda_release_group:corda-jackson:$corda_release_version"
    cordaCompile "$corda_release_group:corda-rpc:$corda_release_version"
    cordaCompile "$corda_release_group:corda-node-api:$corda_release_version"
    cordaRuntime "$corda_release_group:corda:$corda_release_version"

    testCompile "$corda_release_group:corda-node-driver:$corda_release_version"

    // CorDapp dependencies
    // Specify your CorDapp's dependencies below, including dependent CorDapps.
    // We've defined Cash as a dependent CorDapp as an example.
    cordapp project(":cordapp-contracts-states")
}

答案 1 :(得分:0)

您可以使用tag而不是id并遍历这里解释的所有内容:https://stackoverflow.com/a/16262479/1243048

答案 2 :(得分:0)

解决方法:使用id进行操作不是一个好习惯。最好使用标签。但要解决此问题,请使用以下代码

private void hideViews(int id, ViewGroup view) {
    for (int i = 0; i < view.getChildCount(); i++) {
        View v = view.getChildAt(i);
        if (v instanceof Button && v.getId() == id) {
           v.setVisibility(View.GONE)
        } else if (v instanceof ViewGroup) {
            this.loopViews((ViewGroup) v);
        }
    }
}