Help to Compare ID to String

Asked

Viewed 108 times

1

Good people, I’m developing a simple application that when started is 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 am trying from this code to find a way that when the user click on the Imagebutton "imBt_Post" happens the comparison to see if the text that was drawn and set in Textview "tv_Post" corresponds to 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.

  • Isn’t it easier for you to work with indexes? Why not save the selected item index as instance variable and compare between the two indices?

  • sorry... I’m a beginner.. could help me do that?

  • Okay, I’m putting together an explanation of what I’m talking about.

  • I’m on hold :)

1 answer

2


My suggestion is to save the status of the selected colors and text in Activity, as well as possible values 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
        }
    }
});
  • perfect :) vlw guy... thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.