简体   繁体   中英

How to change imageView depending on text in textView in android/java/eclipse

Is it possible to change an image in the imageView depending on the outcome of the text inside a textView.

For example if I have:

    public void pourfive(View v){
    TextView statusupdate = (TextView) findViewById(R.id.txt_statusupdate); 
    TextView fivecup = (TextView) findViewById(R.id.txt_fivecup);
    TextView threecup = (TextView) findViewById(R.id.txt_threecup);
    if (fivecup.getText().toString().equals("0")){
        threecup.setText("0");
        statusupdate.setText("You have no water in the 5 litre cup to pour!");
    }
    else if (threecup.getText().toString().equals("0") && (fivecup.getText().toString().equals("5"))){
        fivecup.setText("2");
        threecup.setText("3");
        statusupdate.setText("You poured 3 litres into the 3 litre cup!");
    }

In this instance I have a cup that can contain 1,2,3,4,5 different levels of coffee. If i wanted to have an image change to represent each one of these different levels. I think it might start something like this:

    public void cupLevel(View v){
    TextView threecup = (TextView) findViewById(R.id.txt_threecup);
        threecup.getText();
}

I then think I would need some if and if else statements depending on the threecup number and some way to call the relevant image but I'm not sure how to call the required image.

Any help would be greatly appreciated thanks.

There are many ways you could approach this.
One of the simplest to understand if you're just starting out learning to program, would be to have a bunch of images for different levels of the cup. For example:

// Sets the image for the three cup, based on the fill level supplied
public void setThreeCupImage(int fillLevel) {

    // make an int array containing the relevant images.
    int[] threeCupLevels = {R.drawable.threecup_level_1,
                            R.drawable.threecup_level_2,
                            R.drawable.threecup_level_3};

    // get reference to an ImageView you've put in you layout xml.
    ImageView threeCupImage = (ImageView)findViewById(R.id.image_threecup);

    // set the ImageView to display the required image from the array.
    threeCupImage.setImageResource(threeCupLevels[fillLevel]);
}

Some tips regarding how you've written your other code...

Hard coding mathematical results is not a good idea as you have done here:

 else if (threecup.getText().toString().equals("0") && (fivecup.getText().toString().equals("5"))){ fivecup.setText("2"); threecup.setText("3"); 

This is inflexible and quickly becomes bulky and complicated in larger problems. Try and make you code more general. Computers are good at maths! Let them do the maths for you!

For example, think about what the main variables are in this problem. They are:

  1. The sizes of the cups
  2. The levels they are filled to

A more general way to code your problem may be to first define a Cup class:

public class Cup {

    // public fields of the class
    public final int cupSize;    // this is final because a cup does not change size.
    public int cupLevel;    // this is NOT final because level can vary

    // class constructor
    public Cup(int size, int level) {
        cupSize = size;
        cupLevel = level;
    }
}

Then you can create cups and change their contents as needed:

// create a new cup with a size of 5 and fill level of 0 (empty)
Cup fiveCup = new Cup(5, 0)

// create a new cup with a size of 3 and fill level of 3 (full)
Cup threeCup = new Cup(3, 3)

// change the level of fiveCup
fiveCup.cupLevel = 2;

You could then write a method for pouring from one cup to another which can handle cups of any size or fill level:

public void pourCup(Cup source, Cup target) {

    int spaceInTargetCup = target.cupSize - target.cupLevel;

    if (source.cupLevel <= spaceInTargetCup) {
        // there's room to pour all the contents of the source cup into the target cup
        target.cupLevel += source.cupLevel; // add the contents of source to the target
        source.cupLevel = 0; // the source is now empty
    } else {
        // there's not enough room to pour all the contents of the source in
        source.cupLevel -= spaceInTargetCup; // empty as much as you can from the source
        target.cupLevel = target.cupSize; // the target is now full
    }
}

To pour the contents of threeCup into fiveCup you would simply call:

pourCup(threeCup, fiveCup);

...and to pour the contents of fiveCup into threeCup would obviously be:

pourCup(fiveCup, threeCup);

You can easily modify the method to let the user know what is going on with statements like:

statusupdate.setText("There is room to pour " + spaceInTargetCup + " litres into the " +
        target.cupSize + " litre target cup");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM