设置图像资源和if-else问题

时间:2019-03-14 11:18:58

标签: if-statement android-imageview

我正在尝试根据TextView中的String更改ImageView中显示的图像(对嵌套的if-else语句感到抱歉)。在我运行的测试中,即使st1的值为“ HKD”,从嵌套if-else传递出来的flagName仍然为“ @ drawable / foreign_exchange”。

我似乎找不到问题。任何帮助都感激不尽。谢谢。

    EditText etCurrency = findViewById(R.id.editTextCurrency);
    String st1 = etCurrency.getText().toString();

    if (st1=="HKD"){
        flagName = "@drawable/flag_hong_kong";
    }else if(st1=="IDR"){
        flagName = "@drawable/flag_indonesia";
    }else if(st1=="JPY"){
        flagName = "@drawable/flag_japan";
    }else if(st1=="KRW"){
        flagName = "@drawable/flag_south_korea";
    }else if(st1=="MYR"){
        flagName = "@drawable/flag_malaysia";
    }else if(st1=="NZD"){
        flagName = "@drawable/flag_new_zealand";
    }else if(st1=="PHP"){
        flagName = "@drawable/flag_philippines";
    }else if(st1=="THB"){
        flagName = "@drawable/flag_thailand";
    }else if(st1=="TWD"){
        flagName = "@drawable/flag_taiwan";
    }else flagName = "@drawable/foreign_exchange";

    int imageResource = getResources().getIdentifier(flagName,null,this.getPackageName());
    imageView.setImageResource(imageResource);

1 个答案:

答案 0 :(得分:0)

使用.equals代替==

if (st1.equals("HKD"))