我是不是遗漏了一些代码?
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_blank, container, false); //btnButtonTwo = view.findViewById(R.id.btnOne2); //btnButtonTwo.setOnClickListener(this); btnButtonTwo = view.findViewById(R.id.btnOne2); //btnButtonTwo.setEnabled(true); btnButtonTwo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "Y0U clicked da Button, INSIDE THE FRAGMENT!", Toast.LENGTH_LONG).show(); } }); return view; }
[Fragment_blank.xml]
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BlankFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/btnOne2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:focusable="auto" android:text="One" /> </LinearLayout> </FrameLayout>
我将 OnClickListener 代码提取到一个单独的应用程序中,以试图隔离问题。按钮应该响应点击但它不是。我不确定现在发生了什么! 工作项目文件位于此处... https://github.com/potpori-code/clicky/MyOnClicky.zip