2011-06-03 63 views
1

我的res/drawable文件夹中有10个动物图像,我想根据编辑文本选项逐个显示图像。我的意思是在编辑文本中,当我们输入体重时,根据体重的范围(如果体重在60-70之间,图像视图显示老虎)对于每个体重图像视图显示具有不同声音的不同动物(根据范围)。由于我是Android新手,无法解决这个问题,我们如何实现这一点?任何想法?Android:根据edittext选项显示图像的Imageview

帮助总是赞赏....!

回答

2

根据你EDITTEXT值可以传递值以下功能

private void displayImg(int value){ 
    ImageView im = (ImageView)findViewById(R.id.img1); 
    if (value >= 0 && value < 10){ 
     im.setBackgroundResource(R.drawable.animal1); 
    }else if (value >=10 && value <20){ 
     im.setBackgroundResource(R.drawable.animal2); 
    } 
      ..... so on... 
} 
+0

NKS了很多,但如何才能发挥动物的声音每个动物出现时? – Jamesdroid 2011-06-03 05:30:17

+0

你应该接受答案,并upvote它,如果你觉得它有帮助.... :) - 我会检查,如果我已经做了后台播放声音,然后给你。 – Nikhil 2011-06-03 05:32:48

+0

接受亲爱的朋友:) – Jamesdroid 2011-06-03 05:34:52

2
ImageView imgAnimal = (ImageView)findViewById(R.id.image_animal); 
EditText editWeight = (EditText)findViewById(R.id.edit_weight); 
try { 
    // get the weight, will throw an exception if 
    // the EditText doesn't contain a valid integer 
    int weight = Integer.parseInt(editWeight.getText().toString()); 
    // if you reach this, the conversion was successful, but you might want to 
    // check for negative values, or values too small/too big and warn the user 
    // if you want to change the image based on a range, 
    // the only way is to use chained if's 
    if (weight >= 60 && weight <= 70) 
     imgAnimal.setImageResource(R.drawable.tiger); 
    else if (weight > 70 && weight <= 80) 
     imgAnimal.setImageResource(R.drawable.some_animal); 
    // and so on... 
} catch (NumberFormatException nfe) { 
    // the EditText does not contain a number, warn the user or something 
} 
+0

Neguţ非常感谢,但是当每只动物出现时,我们如何播放动物的声音? – Jamesdroid 2011-06-03 05:31:23