我创建了3个活动及其类,我想在按下按钮时打开它们,按钮在Fragment中,我已经成功实现了这个单按钮,并且有效。但是我想为这两个按钮做同样的事情,我怎么能让这两个按钮在这个类中打开不同的活动。 我已经包含了代码..
Profilefragment.class
public class ProfileFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.profile, container, false);
Button newPage = (Button) v.findViewById(R.id.button2);
newPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), parseactivity.class);
startActivity(intent);
}
});
return v;
}
profilefragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:weightSum="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button1"
android:id="@+id/button1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button2"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button3"
android:id="@+id/button3"
android:layout_gravity="center_horizontal" />
答案 0 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.profile, container, false);
Button newPage = (Button) v.findViewById(R.id.button2);
newPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), parseactivity.class);
startActivity(intent);
}
});
Button newPage1 = (Button) v.findViewById(R.id.button1);
newPage1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), secondActivity.class);
startActivity(intent);
}
});
Button newPage2 = (Button) v.findViewById(R.id.button3);
newPage2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ThirdActivity.class);
startActivity(intent);
}
});
return v;
}
答案 1 :(得分:0)
在此确定你的清单包括所有活动的定义。 重复你为id = button2
的按钮做的事情Button new2Page = (Button) v.findViewById(R.id.button1);
newPage2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), other.class);
startActivity(intent);
}
});
答案 2 :(得分:0)
public class ProfileFragment extends Fragment implements View.OnClickListener {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.profile, container, false);
Button button1 = (Button) v.findViewById(R.id.button1);
button1.setOnClickListener(this);
Button button2 = (Button) v.findViewById(R.id.button2);
button2.setOnClickListener(this);
Button button3 = (Button) v.findViewById(R.id.button3);
button3.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// launch Activity1
break;
case R.id.button2:
// launch Activity2
break;
case R.id.button3:
// launch Activity3
break;
}
}
}