Help to Compare ID with String

1

Well, I'm developing a simple application that when started starts displaying random images and text on the screen just for learning and what I have so far is this:

package br.com.appteste;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class RunActivity extends ActionBarActivity {

    Timer timer = new Timer();

    int delay = 1000;
    int interval = 1000;

    ImageButton imBt_Post;
    TextView tv_Post, tv_setScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_running);

        imBt_Post = (ImageButton) findViewById(R.id.ImageSelectGame);

        tv_Post = (TextView) findViewById(R.id.TextSelectGame);
        tv_setScore = (TextView) findViewById(R.id.TextScoreGame);

        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                int[] varButtonImages = {R.drawable.circle_blue,
                                         R.drawable.circle_green,
                                         R.drawable.circle_pink,
                                         R.drawable.circle_red,
                                         R.drawable.circle_yellow};

                final int buttonSelect = varButtonImages[new Random().nextInt(varButtonImages.length)];

                imBt_Post.setTag(buttonSelect);

                imBt_Post.post(new Runnable() {                 
                    @Override
                    public void run() {
                        imBt_Post.setBackgroundResource(buttonSelect);
                    }
                });

                String[] varTextColor = {"Azul","Verde","Rosa","Vermelho","Amarelo"};

                final String textSelect = varTextColor[new Random().nextInt(varTextColor.length)];

                tv_Post.post(new Runnable() {                   
                    @Override
                    public void run() {
                        tv_Post.setText(textSelect);
                    }
                });
            }
        }, delay, interval);

        imBt_Post.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

I'm trying from this code to find a way that when the user clicks on the ImageButton "imBt_Post" the comparison happens to see if the text that was drawn and set in the TextView "tv_Post" matches the color of the circle (Image) set in ImageButton, someone could help me in how to do this because at the moment I have no idea how to do this.

    
asked by anonymous 24.04.2015 / 00:55

1 answer

2

My suggestion is to save the state of the colors and selected text in Activity , as well as the possible value vectors as constants:

public class RunActivity extends ActionBarActivity {

    // Valores possiveis de Drawables para o ImageButton 
    private static final int[] cores =
    {
        R.drawable.circle_blue,
        R.drawable.circle_green,
        R.drawable.circle_pink,
        R.drawable.circle_red,
        R.drawable.circle_yellow
    };

    // Constante com os valores possiveis de cores em formato textual
    private static final String[] coresTexto =
    {
        "Azul",
        "Verde",
        "Rosa",
        "Vermelho",
        "Amarelo"
    };

    // Valores atuais dos vetores
    int mIndiceTexto, mIndiceCor;
    Random mRandom = new Random();

    // Restante das suas variaveis de instancia

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // Restante do seu codigo...

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mIndiceTexto = mRandom.nextInt(coresTexto.length);
                mIndiceCor = mRandom.nextInt(cores.length);

                runOnUiThread(new Runnable() {                   
                    @Override
                    public void run() {
                        imBt_Post.setBackgroundResource(cores[mIndiceCor]);
                        tv_Post.setText(coresTexto[mIndiceTexto]);
                    }
                });
            }
        }, delay, interval);
    }

    // Restante do seu codigo
}

To make the comparison:

imBt_Post.setOnClickListener(new View.OnClickListener() {           
    @Override
    public void onClick(View v) {
        if(mIndiceCor == mIndiceTexto) {
            // A cor e o texto sao iguais
        }
    }
});
    
24.04.2015 / 01:36