Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在视图中存储对其他视图的引用?_Android - Fatal编程技术网

Android 如何在视图中存储对其他视图的引用?

Android 如何在视图中存储对其他视图的引用?,android,Android,我正在尝试用可扩展的内容创建“信息”文本视图。用户看到一个显示“信息”的文本视图,点击/点击,另一个包含信息的文本视图(直到现在才被隐藏)出现 以下是XML: <!-- Information expandable block--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"

我正在尝试用可扩展的内容创建“信息”文本视图。用户看到一个显示“信息”的文本视图,点击/点击,另一个包含信息的文本视图(直到现在才被隐藏)出现

以下是XML:

<!-- Information expandable block-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/element_background"
            android:orientation="vertical"
            android:layout_marginTop="@dimen/home_screen_elements_spacing">
            <!-- Information block title -->
            <TextView
                android:id="@+id/app_schedule_fri_information"
                android:tag="app_schedule_fri_information_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="8dp"
                style="@style/AppTheme"
                android:text="@string/app_home_information"
                android:textColor="@color/colorTextBody"
                android:background="@android:color/transparent"
                android:clickable="true"
                android:focusable="true"
                android:onClick="toggle_contents"/>
            <!-- Information block content to hide/show -->
            <TextView
                android:id="@+id/app_schedule_fri_information_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="15dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="8dp"
                android:background="@android:color/transparent"
                style="@style/AppTheme"
                android:text="@string/app_home_information_text"
                android:textColor="@color/colorTextBody"/>
        </LinearLayout>

以下是单击时调用的“切换内容”功能:

public void toggle_contents(View v){
    TextView content = <GET_REF_TO_CONTENT>;
    content.setVisibility( content.isShown()
            ? View.GONE
            : View.VISIBLE );
}
public void toggle_内容(视图v){
TextView内容=;
content.setVisibility(content.isShown()
“视图”消失了
:View.VISIBLE);
}
我需要什么:

正如你所看到的,我被卡住了。我正在寻找一种在title Textview中存储对content Textview的引用的方法。在被调用时,toggle_content()将从title视图(例如v.getTag())恢复该引用,并使用它来定位内容并进行切换

我的尝试:


我试图将内容textview的id存储在title textview上的标记中(实际存在于上面的xml中),但未能将标记转换为id。

这不性感,但有效:

public void toggle_contents(View v){
    int id = getResources().getIdentifier(v.getTag().toString(), "id", this.getPackageName());
    TextView content = findViewById(id);
    content.setVisibility( content.isShown()
            ? View.GONE
            : View.VISIBLE );
}

希望有人能提供更好的答案,因为这看起来像是未来更新会破解的黑客攻击。

在第一个文本视图上使用单击侦听器,并通过将其可见性设置为VISIBLE/GONE,通过切换使第二个文本视图每隔一次单击就出现/消失:

TextView information = (TextView) findViewById(R.idapp_schedule_fri_information);
TextView informationText = (TextView) findViewById(app_schedule_fri_information_text);

information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (informationText.isVisible()) {
                   informationText.setVisibility(View.GONE); 
                } else {
                   informationText.setVisibility(View.VISIBLE);
                }
            }
        });

您不需要id。
如果您已经在
信息
的标记中存储了
信息文本
的引用,如:

information.setTag(informationText);
然后您可以像这样检索它:

public void toggle_contents(View v){
    TextView content = ((TextView) v).getTag();
    content.setVisibility( content.isShown()
            ? View.GONE
            : View.VISIBLE );
}

因此,您可以存储对
TextView
本身的引用,而不必在
标记中存储
TextView
的名称,而不用费心查找它的id。

您应该在它们的公共父级中处理这些内容,父级总是知道它的子级。您可以使用styleable-+复合视图组来解决问题。