2011-06-03 64 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