我的 main_activity 中有一个ViewFlipper,并且包含了一些活动(页面)。
<ViewFlipper
android:id="@+id/vf"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/include_news"
layout="@layout/activity_news"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="@+id/include_latest_posts"
layout="@layout/activity_latest_posts"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
android:id="@+id/include_categories"
layout="@layout/activity_categories"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
</ViewFlipper>
The acitvities (pages) have their own java files. 我试着从那里修改一个textView:
public class latest_posts extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View myView = findViewById(R.id.include_latest_posts);
TextView tv = myView.findViewById(R.id.list);
tv.setText("HELLO");
}
}
但是当我运行应用程序时,没有任何反应。如何从page-acitvity的java文件中访问Views?
我还尝试在包含的活动中为Button创建onClick函数。它工作在MainActivity的VievFlipper所在的java文件中。如何在包含的活动的java文件中创建onClick函数?
答案 0 :(得分:0)
试试这个
private TextView _textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_latest_posts);
_textView = (TextView) findViewById(R.id.myTv);
_textView.setText("It works!");
}