在我的應用程序中,我使用了一些有助於瀏覽不同內容的按鈕。 我建立的佈局完美,但現在我想爲按鈕做鏈接。我可以在我的片段中放置onClickListener?
我的按鈕ID是:buttonSP
我在哪裏可以添加onClickListener在我的代碼片段打開一個名爲新的活動:Lista_Smartphone?
FragmentWithOneImage.java
public class FragmentWithOneImage extends Fragment {
private String title;
private int image;
public static FragmentWithOneImage newInstance(String title, int resImage) {
FragmentWithOneImage fragment = new FragmentWithOneImage();
Bundle args = new Bundle();
args.putInt("image", resImage);
args.putString("title", title);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = getArguments().getInt("image", 0);
title = getArguments().getString("title");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one_img, container, false);
TextView tvLabel = (TextView) view.findViewById(R.id.txtMain);
tvLabel.setText(title);
ImageView imageView = (ImageView) view.findViewById(R.id.imgMain);
imageView.setImageResource(image);
return view;
}
}
變量「騙子」從內部類中訪問,需要被聲明爲final – localsixosix
解決了「CON」有一個錯誤說:在這行unreacheable聲明:按鈕buttonSP =(按鈕)view.findViewById( R.id.buttonSP); – localsixosix