試圖讓我的按鈕全部3片段使用相同的單擊事件後Click事件,public class FragmentShapesDemo extends android.support.v4.app.Fragment implements View.OnClickListener
變成紅色下劃線&返回錯誤:在片段多個按鍵不靈
Class 'FragmentShapesDemo' must either be declared abstract or implement abstract method 'onClick(View)' in OnClickListener
我不不懂爲什麼這個錯誤出現時,點擊事件已實施: -/
fragment_shapes_demo.xml
<Button
android:id="@+id/btn_1"
android:onClick="btn_Click"
android:text="@string/circles"/>
<Button
android:id="@+id/btn_2"
android:onClick="btn_Click"
android:text="@string/squares"/>
<Button
android:id="@+id/btn_3"
android:onClick="btn_Click"
android:text="@string/triangles"/>
FragmentShapesDemo.java
public class FragmentShapesDemo extends android.support.v4.app.Fragment {
Button button0;
Button button1;
Button button2;
public FragmentShapesDemo() {
// Required empty constructor
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_shapes_demo, container, false);
button0 = (Button) v.findViewById(R.id.btn_1);
button1 = (Button) v.findViewById(R.id.btn_2);
button2 = (Button) v.findViewById(R.id.btn_3);
button0.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
return v;
}
public void btn_Click(View v) {
Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
}
findViewById(),而你是在你的情況 – Gautam