我的应用程序中的布尔方法出错

时间:2013-06-29 14:33:59

标签: java android android-activity boolean

我正在开发像徽标测验这样的应用程序...这是活动,您必须在其中写下徽标的名称:

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, img);
        Intent i = getIntent();
        if (mb) {
            // Sunet toast thread
            txt.setText(title(img));
            win_sound.start();
            i.putExtra("score", "1");
            setResult(RESULT_OK, i);

            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) {
            // sunet toast thread
            wrong_sound.start();
            Toast t = Toast.makeText(getApplicationContext(),"Wrong answer! Please check if you have spelled corectly the name of the team!",Toast.LENGTH_LONG);
            t.show();
        }
    }

    private String 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, ImageView img2) {
        // TODO Auto-generated method stub
        String s = et2.getText().toString();
        s = WordUtils.capitalize(s);
        String s1 = (String) img2.getTag();
        boolean b;

        if ((s1.contains("") && s.contentEquals(firstWord(s1)))||(s.contentEquals(s1) && !s1.contains(""))) {
            b = true;
        }else {
            b=false;
        }



        return b;

    }


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



}

它有效...但是当徽标的名称有两个单词时,当我只放置第一个单词时,它说它是错的!它仅适用于全名。请帮助我!

2 个答案:

答案 0 :(得分:1)

更改以下条件:

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

同时更改您的if条件:

((s1.contains(" ") && s.contentEquals(firstWord(s1)))||(s.contentEquals(s1) && !s1.contains(" ")))

答案 1 :(得分:0)

我认为你的firstWord功能存在问题。尝试将其更改为:

private String firstWord(String s1) {
    String arr[] = s1.split(" ");
    return arr[0];
}

另外,改变这个:

if ((s1.contains("") && s.contentEquals(firstWord(s1)))||(s.contentEquals(s1) && !s1.contains("")))

要:

if ((s1.contains(" ") && s.contentEquals(firstWord(s1)))||(s.contentEquals(s1) && !s1.contains(" ")))