这是PagesFragment文件。
public class PagesFragment extends Fragment {
public PagesFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(listener);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(listener2);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(listener3);
return rootView;
}
ImageButton.OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
};
public void speak_1 (View theButton)
{
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
}
这是xml文件
<ImageButton
android:id="@+id/speakers_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtLabel"
android:layout_marginLeft="17dp"
android:layout_marginTop="17dp"
android:onClick="speak_1"
android:src="@drawable/speakers_1" />
如何拨打speak_1?我还想在单击ImageButton时切换到另一个xml。谢谢。
答案 0 :(得分:1)
分别为每个ImageButton传递不同的侦听器非常奇怪。尝试这样做:
public class PagesFragment extends Fragment implements View.OnClickListener {
public PagesFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(this);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(this);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
// here you can handle your ImageButton clicks
int id = v.getId();
if (id == R.id.speakers_1) {
speak_1();
} else if (id == R.id.speakers_2) {
speak_2();
} // etc...
}
public void speak_1() {
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
public void speak_2() {
Intent i = new Intent(this.getActivity(),speaker2.class);
startActivity(i);
}
}
制作类名首字母的第一个字母也是一种好习惯,它是标准的命名约定(Speaker1而不是speaker1)。
答案 1 :(得分:0)
要调用方法,请使用speak_1(arg0)
onClick()
示例强>
ImageButton.OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
speak_1(arg0);
}
};
答案 2 :(得分:0)
这样做:
public class PagesFragment extends Fragment implements View.OnClickListener{
public PagesFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
ImageButton s1 = (ImageButton)rootView.findViewById(R.id.speakers_1);
s1.setOnClickListener(this);
ImageButton s2 = (ImageButton)rootView.findViewById(R.id.speakers_2);
s2.setOnClickListener(this);
ImageButton s3 = (ImageButton)rootView.findViewById(R.id.speakers_3);
s3.setOnClickListener(this);
return rootView;
}
public void speak_1(View theButton)
{
Intent i = new Intent(this.getActivity(),speaker1.class);
startActivity(i);
}
@Override
public void onClick(View view) {
if(view instance of ImageButton)
speak_1(view);
}
}