我已经给出了我解决的代码并且它工作正常但我不知道如何再添加一个按钮来重定向到另一个活动
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;
} }
答案 0 :(得分:3)
在真正答案之前只有一些提示:
现在这里是快速的'回答:
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;
}
}
}
答案 1 :(得分:1)
在courses.xml
添加另一个按钮,其中包含其他ID的其他按钮。
<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;
}
}