【问题标题】:Call TextView from MainActivity to Fragment in Android在Android中从MainActivity调用TextView到Fragment
【发布时间】:2016-01-23 05:45:46
【问题描述】:

是否可以调用MainActivity控制TextViewFragment?我不知道这是否可能。

DrawerLayout,它包含TextView。验证布局包含TextView

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@color/lightish" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/lightish" />

        <include layout="@layout/validation" />
    </RelativeLayout>

    <ListView
        android:id="@+id/lvSlider"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/dark_gray"
        android:choiceMode="singleChoice"
        android:divider="@color/dark_gray"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_item_selector" />

</android.support.v4.widget.DrawerLayout>

现在,当有人单击滑块菜单项时,我在 FrameLayout 中调用 Fragment。我想在 Fragment 中找到 TextView 控件。有可能吗,如果是,那怎么办?就像在WebForm 中调用MasterPage 控件在Asp.net 中一样。

【问题讨论】:

  • getActivity().findViewById(R.id.textId)。您可以使用getActivity() 在片段中找到活动视图
  • in onViewCreated(View v){ v.findViewById(...);}

标签: android android-fragments android-framelayout


【解决方案1】:

是的,这是可能的。
你可以这样试试:

TextView textview = (TextView) getActivity().findViewById(R.id.txtpoint);
textview.setText("Something you write");

【讨论】:

  • 谢谢。它解决了我的问题。现在,当我从 Slider 调用 OptionsMenu 时,它不会在 TextView 中显示任何内容。我应该为此做些什么改变?
猜你喜欢
  • 2021-12-24
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多