微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android中的if else语句

我是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.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.airplane1).getConstantState())                                                                                                                                                                                                                                                                                                                                                                                                                                              

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐