【问题标题】:if else statement in androidandroid中的if else语句
【发布时间】:2013-02-12 08:30:43
【问题描述】:

我是 Android 新手,在互联网上找到了一些很好的教程,所以我尝试了一个简单的活动,其中包含 if-else 语句。我在尝试“正确与错误”提示/Toast:

Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });

我不确定我的if-else 语句有什么问题,但根本不会提示。我尝试删除 (iv1.equals(R.drawable.airplane3)) 和 (iv1.equals(R.drawable.airplane2)) 然后它只显示错误的 Toast。我似乎无法正确提示我。

这是我的课程的完整代码:

public class MainActivity extends Activity {
private static final Random imagerandom = new Random();

private static final Integer[] Imagesnumber = 
    { R.drawable.airplane1, R.drawable.airplane2, R.drawable.airplane3, };
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Integer a = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
    final ImageView iv = (ImageView) findViewById(R.id.imageView1);

    View nextButton = findViewById(R.id.button1);
    nextButton.setOnClickListener(new Button.OnClickListener() {
         public void onClick(View V) {
              int resource = Imagesnumber[imagerandom.nextInt(Imagesnumber.length)];
              iv.setImageResource(resource);
         }
    });
    Button page1 = (Button) findViewById(R.id.button2);
       page1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
                if (iv1.equals(R.drawable.airplane1)) {
                    Toast.makeText(getApplicationContext(), "Correct",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane2)) {
                    Toast.makeText(getApplicationContext(), "Please put an answer",
                            Toast.LENGTH_SHORT).show();
                } else if (iv1.equals(R.drawable.airplane3)){
                    Toast.makeText(getApplicationContext(), "Wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

       });




}

}

【问题讨论】:

  • 让我看看我对你的理解是否正确,你是想看看你iv1中显示的图像是什么?
  • 是 im trying to see if the imaged displayed in iv1 is correct or wrong using the if else im 不太确定我的 if else 语句有什么问题
  • 您缺少 else 条件。我的意思是不满足您指定的 3 个 if 的条件
  • 问题是您正在尝试查看图像视图是否等于可绘制对象的 Id,这些东西永远不会相等,因为它们是不同的对象。您可能想寻找一种方法从图像视图中获取当前的可绘制对象,然后查看它等于什么。
  • 您正在将整数与 ImageViews 进行比较。那是行不通的。 R.drawable.airplane1 本身不是图像,它是作为资源标识符服务的 int。

标签: android


【解决方案1】:
iv1.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())                                                                                                                                                                                                                                                                                                                                                                                                                                              

【讨论】:

  • +1 让我们知道......它总是这样工作吗?..我已经尝试过并且正在工作
  • 一个问题再次出现在我的脑海中,这是否会加载用于加载图像的内存?因为我担心我可能会在 logcat 中得到错误 outofmemory 错误
【解决方案2】:

R.drawable.airplane* 是一个 int,这意味着您将 ImageView 对象与一个 int 进行比较,这绝不是真的。使用iv.getDrawable().equals(getResources().getDrawable(R.drawable.airplane1)); 与Drawable 对象进行比较。

这可能不是性能最佳的选项,您可能希望将显示图像的索引作为类变量来跟踪,并基于它(或类似的东西)做一个条件。

【讨论】:

  • 你好 invertigo ive tried what you suggested with this "if (iv1.getDrawable()==getResources().getDrawable(R.drawable.airplane1)) { Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show(); }" im 还是弄错了吐司
【解决方案3】:

ImageView 的类型不能与 int 的类型进行比较

iv1.equals(R.drawable.airplane1)

这是错误的

试试这个

iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1)

改为

【讨论】:

  • 你好 Pragnani 我已经用这个 "if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane1)) { Toast.makeText(getApplicationContext(), "正确", Toast.LENGTH_SHORT).show(); } else if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane2)) { Toast.makeText(getApplicationContext(), "请给出答案", Toast.LENGTH_SHORT).show(); } else if (iv.getDrawable()==getResources().getDrawable(R.drawable.airplane3)) { Toast.makeText(getApplicationContext(), "Wrong", Toast. LENGTH_SHORT).show(); }"
  • 现在它根本不提示任何内容,我想我的代码有误
  • @NewbieontheSide 对图像视图使用 setTag 和 getTag 比较,或 setContentDescription 和 getContentDescription ..
  • 我看到了这个链接stackoverflow.com/questions/5291726/…,但我不知道t know how to implement it in my code.. sorry im 是android/java 编程的新手
  • 在您的 xml 添加属性 android:contentDescription="airplane" 任何您可以识别的名称..给出可绘制名称进行比较,然后像这样比较 if(iv.getcontnentDescription.equals("airplace") ){//做一些事情}
【解决方案4】:

您正在比较ImageView 实例和ints,检查点击哪个视图的正确方法是:

if (iv1.getId() == R.id......) { ...

这样您就可以将int 与另一个int 进行比较

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2015-10-28
    • 2017-02-05
    • 1970-01-01
    • 2015-07-06
    • 2012-08-05
    相关资源
    最近更新 更多