Как добавить еще одну кнопку В фрагменты
Я дал код, который я разработал, и он работает нормально, но я не знаю, как добавить еще одну кнопку для перенаправления на другую деятельность
public class courses extends Fragment {
Intent intent;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.courses, container, false);
intent = new Intent(getActivity(), software_course.class);
final Button button = (Button) root.findViewById(R.id.Software_Course);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
} }
2 ответа:
Только несколько советов перед реальным ответом:
- Используйте первую заглавную букву для названия классов
- Не используйте первую заглавную букву для идентификаторов
- Не создавайте differents новый экземпляр OnClickListener, как показано ниже, вместо этого реализуйте интерфейс
- глобальная переменная intent не имеет большого смысла
Теперь вот "быстрый" ответ:
public class Courses extends Fragment { Intent intent, anotherIntent; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.courses, container, false); intent = new Intent(getActivity(), Software_course.class); anotherIntent = new Intent(getActivity(), YourSecondActivityName.class); final Button button = (Button) root.findViewById(R.id.software_Course); final Button button2 = (Button) root.findViewById(R.id.software_Course2); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(intent); } }); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(anotherIntent); } }); return root; } }
Вот что я рекомендую:
public class Courses extends Fragment implements View.OnClickListener { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.courses, container, false); root.findViewById(R.id.software_Course).setOnClickListener(this); root.findViewById(R.id.software_Course2).setOnClickListener(this); return root; } public void onClick(View v) { switch(v.getId()) { case R.id.software_Course: startActivity(new Intent(getActivity(), Software_course.class)); break; case R.id.software_Course2: startActivity(new Intent(getActivity(), YourSecondActivityName.class)); break; } } }
В свой
courses.xml
Добавьте еще одну кнопку с другим идентификатором другой кнопки.<Button android:id="@+id/hardware" android:layout_width="wrap_content" android:layout_height="wrap_content" />
В вашем
courses.java
инициализируйте кнопку и используйтеsetonClickListener
.public class courses extends Fragment { Intent intent; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.courses, container, false); intent = new Intent(getActivity(), software_course.class); final Button button = (Button) root.findViewById(R.id.Software_Course); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(intent); } }); Button Hardware=(Button)root.findViewById(R.id.Hardware_Course); Hardware.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do stuff you want to do on click of button intent = new Intent(getActivity(), hardware_course.class); startActivity(intent); } }); return root; }
}