如何从用户输入中调用图像?

时间:2019-06-15 19:08:06

标签: java android

我希望用户选择四张图像。我希望用户输入一个1到4之间的数字,然后将导致显示相应的图像。未选择的图像将淡出(alpha 0),选择的图像将淡入(alpha 1)。我不知道如何使用包含用户输入的字符串来指定要设置动画的图像。

Google /堆栈溢出搜索没有帮助。

public void buttonClic (View view) {
  ImageView pic1 = findViewById(R.id.imageView1);
  ImageView pic2 = findViewById(R.id.imageView2);
  ImageView pic3 = findViewById(R.id.imageView3);
  ImageView pic4 = findViewById(R.id.imageView4);
  Log.i("info", "OK, pictures have been assigned.");
  EditText number = findViewById(R.id.enterNumber);
  String str = number.getText().toString().trim();
  double val = 0;
  boolean enteredSmth = true;
  try {val = Integer.parseInt(str);}
  catch(NumberFormatException e) {enteredSmth = false;}
  if(enteredSmth) {
      enteredNumber = Integer.parseInt(number.getText().toString());
      Log.i("info", "OK, number entered has been parsed);
      if (enteredNumber<=4 && enteredNumber>=1){
          String desiredPic = "pic" + str;
          Log.i("info", "The user desires to view " + desiredPic + ".");
          pic1.animate().alpha(0).setDuration(2000);
          pic2.animate().alpha(0).setDuration(2000);
          pic3.animate().alpha(0).setDuration(2000);
          pic4.animate().alpha(0).setDuration(2000);
          // Here is where I'd like the selected image, defined by 'desiredPic', to animate in.
      }
      else {Toast.makeText(this, "I'm no mathematician, but "+enteredNumber+" isn't from 1 to 4...", Toast.LENGTH_LONG).show();}
  }
  else {Toast.makeText(this, "What, are you trying to crash the app?! Enter a NUMBER.", Toast.LENGTH_LONG).show();}
}

1 个答案:

答案 0 :(得分:0)

使用数组存储它们并按索引引用它们,而不是为每个ImageView使用4个变量:

public void buttonClic (View view) {
    ImageView[] images = new ImageView[] {
            findViewById(R.id.imageView1),
            findViewById(R.id.imageView2),
            findViewById(R.id.imageView3),
            findViewById(R.id.imageView4)
    }
    Log.i("info", "OK, pictures have been assigned.");
    EditText number = findViewById(R.id.enterNumber);
    String str = number.getText().toString().trim();
    double val = 0;
    boolean enteredSmth = true;
    try {val = Integer.parseInt(str);}
    catch(NumberFormatException e) {enteredSmth = false;}
    if(enteredSmth) {
        enteredNumber = Integer.parseInt(number.getText().toString());
        Log.i("info", "OK, number entered has been parsed");
        if (enteredNumber<=4 && enteredNumber>=1){
            String desiredPic = "pic" + str;
            Log.i("info", "The user desires to view " + desiredPic + ".");
            for (int i = 0; i < images.length; i++) {
                if (i != enteredNumber - 1)
                    images[i].animate().alpha(0).setDuration(2000);
                else
                    images[i].animate().alpha(1).setDuration(2000);
            }
        }
        else {
            Toast.makeText(this, "I'm no mathematician, but "+enteredNumber+" isn't from 1 to 4...", Toast.LENGTH_LONG).show();}
    }
    else {Toast.makeText(this, "What, are you trying to crash the app?! Enter a NUMBER.", Toast.LENGTH_LONG).show();}
}

请注意,所选图片的索引为 -1 用户选择的编号。