我的一个碎片中有一个按钮。但它似乎是“不可点击”。没有Log.d.单击按钮时的消息(在另一个片段(相同的onclicklistenercode)中一切都很好。 为了更好的概述,我添加了整个类和布局文件的一部分,用于定义按钮。
public class ListViewFragment extends Fragment {
DbHelper mydb;
Button buttondeletedb;
Button buttonexport;
private EditText roomnr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
Context context = getContext();
mydb = new DbHelper(context);
buttondeletedb = (Button) view.findViewById(R.id.button_deletelist_list);
buttonexport = (Button) view.findViewById(R.id.button_export);
String dataList = mydb.getAllElements();
String [] dataListArray = dataList.split("\n");
List<String> dataListFinal = new ArrayList<>(Arrays.asList(dataListArray));
ArrayAdapter<String> dataListAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_datalist,
R.id.list_item_datalist_textview,
dataListFinal);
View rootView=inflater.inflate(R.layout.fragment_listview, container, false);
ListView dataListListView = (ListView) rootView.findViewById(R.id.datalist);
dataListListView.setAdapter(dataListAdapter);
buttondeletedb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("buttondeletelist", "clicked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
mydb.deleteAll();
}
});
return rootView;
}
}
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/deletelist"
android:id="@+id/button_deletelist_list"
android:layout_gravity="center_horizontal" />
答案 0 :(得分:2)
您无法点击该按钮,因为片段的根视图不是视图,您返回 rootView 。该按钮是视图的子视图。
将return rootView;
更改为return view;
。
应该有效
答案 1 :(得分:1)
//删除rootView并返回视图而不是rootView
public class ListViewFragment extends Fragment {
DbHelper mydb;
Button buttondeletedb;
Button buttonexport;
private EditText roomnr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
Context context = getContext();
mydb = new DbHelper(context);
buttondeletedb = (Button) view.findViewById(R.id.button_deletelist_list);
buttonexport = (Button) view.findViewById(R.id.button_export);
String dataList = mydb.getAllElements();
String [] dataListArray = dataList.split("\n");
List<String> dataListFinal = new ArrayList<>(Arrays.asList(dataListArray));
ArrayAdapter<String> dataListAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_datalist,
R.id.list_item_datalist_textview,
dataListFinal);
// Change made here. replace view instead of rootView and remove rootView
ListView dataListListView = (ListView) view.findViewById(R.id.datalist);
dataListListView.setAdapter(dataListAdapter);
buttondeletedb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("buttondeletelist", "clicked!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
mydb.deleteAll();
}
});
// Replace rootView with view.
return view;
}
}
答案 2 :(得分:0)
XML文件和Element(Button)的Class文件之间的链接不存在。在onCreateView()
中添加这一行 Button buttondeletedb = (Button) view.findViewById(R.id.buttonid);