Android 如何在片段中再添加一个按钮
我已经给出了我编写的代码,它工作得很好,但是我不知道如何再添加一个按钮来重定向到另一个活动Android 如何在片段中再添加一个按钮,android,button,android-fragments,android-activity,Android,Button,Android Fragments,Android Activity,我已经给出了我编写的代码,它工作得很好,但是我不知道如何再添加一个按钮来重定向到另一个活动 public class courses extends Fragment { Intent intent; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
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;
} }
在找到真正的答案之前,只有一些提示:
- 使用第一个大写字母作为类的名称
- 不要在ID中使用首字母大写
- 不要像下面那样创建不同的新OnClickListener实例,而是实现接口
- 全局意图变量没有太多意义
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
中,再添加一个具有其他按钮不同id的按钮
<Button
android:id="@+id/hardware"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
}将其添加到
courses.xml中,并像使用按钮一样将其膨胀成碎片。只需添加另一个按钮,该按钮与courses.xml中添加的软件课程按钮相同,只需记住为其指定不同的id,并在创建视图中引用该按钮,该视图与软件课程初始化时相同,并添加ClickListener我按照您所说的操作,但它会显示出来“变量‘button’已在范围中定义这是我在@AndroidWeblineindia View root=inflater.inflate(R.layout.courses,container,false)中所做的;intent=新的intent(getActivity(),hardware\u course.class);最终按钮按钮=(按钮)root.findViewById(R.id.Hardware\u课程);只需给出不同的变量名称,因为“button”已被软件课程按钮使用,所以您可以为硬件课程提供类似“button1”的名称,比如final button button1=(button)root.findViewById(R.id.Hardware_课程);在xml端,everthing is ready是一个带有属性Id(Hardware_Course)和onClick(hc)的按钮,我不知道如何将它调用到程序的java端@shvet谢谢,在做了一些小的编辑之后,代码工作得很好。希望你不会介意,因为我刚刚编辑了你的代码@SHVET代码有效,简单易行。特别感谢你为我这样的新手提供了最有价值的建议@Fondesas说我是新手我在stackoverflow中没有声誉一旦我再获得三个声誉我一定会这么做。。。
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;
}