按钮单击“网格布局”内按钮的侦听器

时间:2016-08-11 15:54:49

标签: android android-layout android-gridlayout

我有一个网格布局,里面有多达9个按钮。我想为每个按钮设置按钮单击侦听器。

这是我到目前为止所做的事情,显然是行不通的:

片段文件

public class Fragment2 extends Fragment {
int choice = -1;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2_layout,container,false);

    btn1 = (Button) view.findViewById(R.id.b1);
    btn2 = (Button) view.findViewById(R.id.b2);
    btn3 = (Button) view.findViewById(R.id.b3);
    btn4 = (Button) view.findViewById(R.id.b4);
    btn5 = (Button) view.findViewById(R.id.b5);
    btn6 = (Button) view.findViewById(R.id.b6);
    btn7 = (Button) view.findViewById(R.id.b7);
    btn8 = (Button) view.findViewById(R.id.b8);
    btn9 = (Button) view.findViewById(R.id.b9);
    if(choice == 0)
        ButtonClicked1();
    else
    if(choice == 1)
        ButtonClicked2();
    choice = getArguments().getInt("ch");
   Toast.makeText(getActivity(), String.valueOf(choice), Toast.LENGTH_SHORT).show();
    return view;
}

public void ButtonClicked1(){
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "2", Toast.LENGTH_SHORT).show();
        }
    });

    btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "3", Toast.LENGTH_SHORT).show();
        }
    });

    btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "4", Toast.LENGTH_SHORT).show();
        }
    });

    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "5", Toast.LENGTH_SHORT).show();
        }
    });

    btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "6", Toast.LENGTH_SHORT).show();
        }
    });

    btn7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "7", Toast.LENGTH_SHORT).show();
        }
    });

    btn8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "8", Toast.LENGTH_SHORT).show();
        }
    });

    btn9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getActivity(), "9", Toast.LENGTH_SHORT).show();
        }
    });
}

public void ButtonClicked2() {
}

}

其XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/RL">

<GridLayout
    android:layout_marginTop="120dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:columnWidth="20dp"
    android:id="@+id/gridLayout">

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b1"
        android:layout_row="0"
        android:layout_column="0"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b4"
        android:layout_row="1"
        android:layout_column="0"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b2"
        android:layout_row="0"
        android:layout_column="1"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b3"
        android:layout_row="0"
        android:layout_column="2"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b5"
        android:layout_row="1"
        android:layout_column="1"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b7"
        android:layout_below="@+id/gridLayout"
        android:layout_alignParentStart="true"
        android:layout_row="2"
        android:layout_column="0"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b8"
        android:layout_row="2"
        android:layout_column="1"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b6"
        android:layout_row="1"
        android:layout_column="2"
        android:background="#ffffff" />

    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="New Button"
        android:id="@+id/b9"
        android:layout_row="2"
        android:layout_column="2"
        android:background="#ffffff" />
</GridLayout>

3 个答案:

答案 0 :(得分:0)

获得View类或其子类的对象后,可以为其注册OnClickListener

btn1 = (Button) view.findViewById(R.id.b1);
...
btn9 = (Button) view.findViewById(R.id.b9);

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getActivity(), "1", Toast.LENGTH_SHORT).show();
    }
});

等等。如果你想通过传递额外的参数来控制它,你必须首先阅读它们:

choice = getArguments().getInt("ch");
if(choice == 0){
    ButtonClicked1();
} else if(choice == 1){
    ButtonClicked2();
}

答案 1 :(得分:0)

嗯,choice-1,因此ButtonClicked1()ButtonClicked2()都不会被调用。

之后 choice = getArguments().getInt("ch");,您检查choice以查看它是0还是1。

答案 2 :(得分:0)

而不是这个:

if(choice == 0) // at this point choice=-1
        ButtonClicked1();
    else
    if(choice == 1) //at this point choice=-1
        ButtonClicked2();
    choice = getArguments().getInt("ch"); // now you are getting value

这样做:

choice = getArguments().getInt("ch"); // now you are getting value
if(choice == 0) // at this point choice="ch value"
            ButtonClicked1();
        else
        if(choice == 1) //at this point choice="ch value"
            ButtonClicked2();