当我尝试像我的Logo Quiz app时出错

时间:2013-06-26 07:02:30

标签: android android-intent android-activity

每个人都听说过Logo Quiz!我尝试开发像徽标测验这样的游戏,这是你必须放置徽标名称的活动!意图接收图像和名称。图片标记包含名称。

public class Vie extends Activity implements OnClickListener {

    ImageView img;
    EditText et;
    Button btn;
    TextView txt;
    MediaPlayer win_sound, wrong_sound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vie);
        img = (ImageView) findViewById(R.id.img);
        et = (EditText) findViewById(R.id.et);
        btn = (Button) findViewById(R.id.btnCheck);
        txt = (TextView) findViewById(R.id.txt);

        win_sound = MediaPlayer.create(Vie.this, R.raw.win);
        wrong_sound= MediaPlayer.create(Vie.this, R.raw.wrong);

        setImageAndTagByIntent(img);
        btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        boolean mb = check(et, btn, img);
        if (mb = true) {
            // Sunet toast thread
            txt.setText(title(img));
            win_sound.start();

            Thread t = new Thread() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    super.run();
                    try {
                        sleep(win_sound.getDuration());

                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    } finally {
                        finish();
                    }

                }

            };

            t.start();

        }

        if (mb = false) {
            // sunet toast thread
            wrong_sound.start();
            Toast t = new Toast(Vie.this);
            t.setText("Wrong answer! Please check if you have spelled corectly the name of the team!");
            t.show();
        }
    }

    private CharSequence title(ImageView img2) {
        // TODO Auto-generated method stub
        String s = (String) img2.getTag();
        return s;
    }

    private void setImageAndTagByIntent(ImageView img2) {
        // TODO Auto-generated method stub
        // Trece si tagul!
        Intent i = getIntent();
        Bitmap back = i.getParcelableExtra("back");
        Drawable b = new BitmapDrawable(getResources(), back);
        img2.setImageDrawable(b);
        String tag = i.getStringExtra("tag");
        Object tag2 = (Object) tag;
        img2.setTag(tag2);

    }

    private boolean check(EditText et2, Button btn2, ImageView img2) {
        // TODO Auto-generated method stub
        String s = et2.getText().toString();
        s = WordUtils.capitalize(s);
        String s1 = (String) img2.getTag();
        boolean b = false;

        if (hasBlank(s1) == true && s.contentEquals(firstWord(s1))) {
            b = true;
        }

        if (hasBlank(s1) == true && s.contentEquals(lastWord(s1))) {
            b = true;
        }

        if (s.contentEquals(s1) && hasBlank(s1) == false) {
            b = true;
        }

        return b;

    }

    private String lastWord(String s1) {
        // TODO Auto-generated method stub
        String arr[] = s1.split("//s");
        return arr[1];
    }

    private String firstWord(String s1) {
        // TODO Auto-generated method stub
        String arr[] = s1.split("//s");
        return arr[0];
    }

    private boolean hasBlank(String string) {
        // TODO Auto-generated method stub
        if (string.contains(" ")) {
            return true;
        }
        return false;
    }

}

但是当我在编辑文本中写任何内容时,它看起来是正确的!请帮我!我认为这是布尔方法的一个问题!

1 个答案:

答案 0 :(得分:3)

你的if语句错了。在onClick方法中,您只使用'='而不是'=='。等号将true分配给mb的值,然后评估if。这意味着mb的值始终设置为true,因此每个答案都被评估为正确。

你应该这样检查:

if (mb == true) { ... }

或更好

if (mb) { ... }

检查错误也是如此。你可以使用:

if (mb == false) { ... }

if (!mb) { ... }