【发布时间】:2016-09-30 09:59:38
【问题描述】:
对于这个愚蠢的问题,我真的很抱歉,但我一直在阅读各种各样的东西,但似乎无法通过我厚厚的头骨。我在这里真的很绝望,所以请多多包涵。
所以我得到了一个完整的文本片段,比如fragment_chapter_1.xml。其中一个文本需要进一步解释,所以我想在文本(Copyright Act (Amendment) 1997)被点击时(在 xml 布局中未设置为可点击),它将转到fragment_explain_law.xml。
fragment_explain_law.xml 有一个 TextView explainLaw,它将获取 setText(在 ExplainLawFragment.java 中)到它自己对在 fragment_chapter_1.xml 中单击的文本的解释。我使用这种方式是因为如果单击其他文本,它将重复使用相同的fragment_explain_law.xml,但explainLaw 将设置为不同的解释。
这是代码。
-
在
Chapter1Fragment.java中,它将查看fragment_chapter_one.xml。law1是Copyright Act (Amendment) 1997的 ID。@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_chapter_1, container, false); law1 = (TextView) rootView.findViewById(R.id.law1); law1.setOnClickListener(this); // Inflate the layout for this fragment return rootView; } -
当
law1被点击时,fragment_explain_law.xml将被显示。为此,我通过转到ExplainLawFragment.java来实现(我这样做对吗?)。@Override public void onClick(View v) { // when law1 till law11 clicked, go to ExplainLawFragment.java to view fragment_explain_law.xml Fragment fragment = null; String title = null; switch (v.getId()) { case R.id.law1: // view explainLaw fragment = new ExplainLawFragment(); title = "Copyright Act (Amendment) 1997"; break; }}
-
在
ExplainLawFragment.java@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_explain_law, container, false); explainLaw = (TextView) rootView.findViewById(R.id.explainLaw); explainLaw.setText("blahblahblah"); // Inflate the layout for this fragment return rootView; }
结果:什么也没发生。
点击law1时的Logcat:V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 1
我哪里做错了?请帮忙。
【问题讨论】:
标签: java android xml android-fragments