【问题标题】:Android: Imageview showing an image according to edittext optionAndroid:Imageview根据edittext选项显示图像
【发布时间】:2011-06-03 13:33:12
【问题描述】:

我的 res/drawable 文件夹中有 10 张动物图像,我想根据编辑文本选项一张一张地显示这些图像。我的意思是在编辑文本中,当我们输入我们的体重时,根据体重的范围(如果体重在 60-70 之间图像视图显示老虎)为每个重量图像视图显示不同的动物(根据范围)具有不同的声音。由于我是 android 新手,我无法解决这个问题,我们该如何实现呢?有什么想法吗?

我们总是感谢您的帮助....!

【问题讨论】:

    标签: android imageview android-edittext


    【解决方案1】:

    根据您的 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...
    }
    

    【讨论】:

    • nks 很多,但是当每个动物出现时我们如何播放动物声音呢?
    • 你应该接受答案并投票,如果你觉得它有帮助的话......:) - 我会检查我是否在后台播放声音然后给你。
    • MediaPlayer 播放器; private void displayImg(int value){ ImageView im = (ImageView)findViewById(R.id.img1); if (player.isPlaying() == true) { player.stop(); } if (value >= 0 && value =10 && value
    【解决方案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
    }
    

    【讨论】:

    • Neguţ 非常感谢,但是我们如何在每只动物出现时播放动物声音呢?
    猜你喜欢
    • 2013-12-20
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多