Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

java - How to disable interaction between buttons of the same layout?

I have two sets of layouts, populated with buttons. Is there a way to disable any interaction between buttons of the same layout? I just need nothing to happen when I click one button and then the other from the same layout. I tagged each button with " and ", for buttons that belongs to upper and lower layouts. I have 5 of each kind. I have some interaction beetwen buttons of oposite layouts and that's working fine (actually interaction works fine between any two given buttons and that's bad), but I don't want that interaction between buttons of the same layout.

Here's my game class, not all of it, just important parts. Also, I've removed code for rest of the buttons, only left for 4 buttons, 2 for each layout, to save some space here. I import some text from sqlite database, and randomly set it to my buttons.

final OnClickListener clickListener = new OnClickListener() {

            private Button buttonClicked;

            public void onClick(View v) {


                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

                if (buttonClicked == null) {
                    // first button is clicked
                    buttonClicked = button;
                } else {
                    // second button is clicked
                    if (buttonClicked.getTag().equals(button.getTag())) {
                        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                      buttonClicked.setEnabled(false);
                        button.setEnabled(false);
                    } else {
                        buttonClicked.setEnabled(false);
                        buttonClicked.setTextColor(Color.GRAY);
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
                        button.getBackground().clearColorFilter();
                    }
                    buttonClicked = null;

            }
     };




    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.game);

        a1 = (Button) findViewById(R.id.bA1);
        a2 = (Button) findViewById(R.id.bA2);
        b1 = (Button) findViewById(R.id.bB1);
        b2 = (Button) findViewById(R.id.bB2);

        nextQuestion();
        }

        public void nextQuestion() {


        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();

        try{ 

            mDbHelper.open();

            Cursor c = mDbHelper.getTestData(generateWhereClause());

            mAnsweredQuestions.add(c.getLong(0));

            ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
            ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();

            labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches
            labelsB.add(new MyStruct(c.getString(3), "1"));
            labelsA.add(new MyStruct(c.getString(4), "2"));
            labelsB.add(new MyStruct(c.getString(5), "2"));


            Collections.shuffle(labelsA);
            Collections.shuffle(labelsB);


            pitanje.setText(c.getString(1));

            a1.setText(labelsA.get(0).label);
            a1.setTag(labelsA.get(0).tag);
            a1.setOnClickListener(clickListener);
            a1.getBackground().clearColorFilter();
            a1.setEnabled(true);
            b1.setText(labelsB.get(0).label);
            b1.setTag(labelsB.get(0).tag);
            b1.setOnClickListener(clickListener);
            b1.getBackground().clearColorFilter();
            b1.setEnabled(true);
            a2.setText(labelsA.get(1).label);
            a2.setTag(labelsA.get(1).tag);
            a2.setOnClickListener(clickListener);
            a2.getBackground().clearColorFilter();
            a2.setEnabled(true);
            b2.setText(labelsB.get(1).label);
            b2.setTag(labelsB.get(1).tag);
            b2.setOnClickListener(clickListener);
            b2.getBackground().clearColorFilter();
            b2.setEnabled(true);


        }


        finally{ 
            mDbHelper.close();
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="top|center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvPitanje"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ovde ce biti postavljeno pitanje"
            android:textColor="#ffffff"
            android:textSize="18sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="360dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/kolona1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="top"
            android:orientation="vertical"
            android:paddingLeft="3dp"
            android:paddingRight="2dp" >

            <Button
                android:id="@+id/bA1"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:layout_gravity="clip_horizontal"
                android:layout_marginBottom="1.5dp"
                android:background="@drawable/buttons"
                android:gravity="center_vertical|center_horizontal"
                android:padding="0dp"
                android:tag="l"
                android:text="A1"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA2"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A2"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA3"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A3"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA4"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A4"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA5"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A5"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA6"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A6"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA7"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A7"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bA8"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:text="A8"
                android:background="@drawable/buttons"
                android:textColor="#ffffff"
                android:layout_marginTop="1.5dp"
                android:tag="l"
                android:padding="0dp"
                android:textSize="16sp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/kolona2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="50"
            android:gravity="top"
            android:orientation="vertical"
            android:paddingLeft="2dp"
            android:paddingRight="3dp" >

            <Button
                android:id="@+id/bB1"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:layout_gravity="top|fill_horizontal"
                android:layout_marginBottom="1.5dp"
                android:background="@drawable/buttons"
                android:gravity="top|center_horizontal"
                android:tag="r"
                android:text="B1"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bB2"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:background="@drawable/buttons"
                android:gravity="top|center_horizontal"
                android:tag="r"
                android:text="B2"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <Button
                android:id="@+id/bB3"
                android:layout_width="fill_parent"
                android:layout_height="41dp"
                android:layout_marginBottom="1.5dp"
                android:layout_marginTop="1.5dp"
                android:background="@drawable/buttons"
                android:gravity="top|center_horizontal"
                android:tag="r"
       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the getParent method and compare the parents of both views. If the parent is the same, then the buttons are in the same layout and nothing should happen.

Change your else:

} else {
    // second button is clicked

... to an else if:

// only do stuff if buttons are in different layouts
} else if (button.getParent () != buttonClicked.getParent()) {
     // second button is clicked

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...