目前我有一個fragment_one.xml
其上有5 CardViews
,並且每個卡片上都有一個Button,用於分離XML頁面(Lesson_One,Lesson_Two等等...) )但與我在OneFragment.java
代碼,這兩個按鈕都是開放的Lesson_Two
按鈕都從一個Android片段打開相同的活動
我該如何解決這個問題?這裏是我的代碼
FragmentOne.java
public class OneFragment extends Fragment{
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
intent = new Intent(getActivity(), LessonOne.class);
final Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
intent = new Intent(getActivity(), LessonTwo.class);
final Button button2 = (Button) root.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}
完美!謝謝一堆,我感謝它! –