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

时间:2017-05-06 19:17:08

标签: java android xml android-fragments android-button

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

  

Class' FragmentShapesDemo'必须被宣布为抽象的或实现抽象方法' onClick(View)'在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();    
    }
}

1 个答案:

答案 0 :(得分:0)

从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;
    }
}