片段中多个按钮的单击事件不起作用

Click event for multiple buttons in fragment not working

在尝试让我的所有 3 个按钮在片段中使用相同的点击事件后,public class FragmentShapesDemo extends android.support.v4.app.Fragment implements View.OnClickListener 变成红色下划线并且 returns 出现错误:

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();    
    }
}

从 XML 中删除 onClick:

<Button
    android:id="@+id/btn_1"

    android:text="@string/circles"/>

<Button
    android:id="@+id/btn_2"
    android:text="@string/squares"/>

<Button
    android:id="@+id/btn_3"
    android:text="@string/triangles"/>

使用您的 rootView 参考来查找元素视图。您也可以使用以下代码实现按钮 onClick

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(new OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(getActivity, "Do Your Work", Toast.LENGTH_SHORT).show();
                }
            });

        button1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                }
            });

        button2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                }
            });

        return v;
    }
}