我在片段中有一个按钮,对于那个按钮,我重写了onClick()方法,但它不起作用。 当按钮被点击时,我也有一个Taost和一个日志。片段中的按钮不起作用点击时
public class DataShown extends Fragment implements OnClickListener{
Button tv;
TextView textview;
Activity activity=getActivity();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("3", "started");
View rootView = inflater.inflate(R.layout.datashown, container, false);
Log.d("3", "closed");
textview=(TextView) rootView.findViewById(R.id.textView1);
tv = (Button) rootView.findViewById(R.id.configButton);
tv.setOnClickListener((OnClickListener) activity);
return rootView;
}//onCtreate
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick","1");
Toast.makeText(activity, "on click", Toast.LENGTH_SHORT).show();
}
}
为什么它的发生,我不知道,可能是它的愚蠢的错误。 现在吐司显示错误,接受上下文对象,所以我提供了活动,不工作。
使用'tv.setOnClickListener(this);'。片段实现'OnClickListener'。在片段附加到活动后也使用'getActivity()'。 – Raghunandan 2014-09-22 06:57:33