0

Hi Im new to android development and this is my first post on stackoverFlow. I am trying to make an in which there is a picture of say brad pitt a edit text and a button. the object is to guess who is in the picture. i want it so that if the user types in brad pitt and clicks the button it will bring them onto the second page with a different picture and if incorrect it could do a toast "Wrong Answer". My problem is that it wont go to the second page when given the right answer.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button submit = (Button) findViewById(R.id.button1);
        EditText input = (EditText) findViewById(R.id.etAnswer);
        final String userinput = input.toString();
        submit.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                {if (userinput.equals("tom")) {
                    Intent myintent = new Intent (MainActivity.this,MainActivity2.class) ;
                    startActivity (myintent);
                    ;
                    }else{
                        Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT ).show();
                    }
                }
            }});
            }           

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
4

4 回答 4

2

而不是userinput.equals("tom")"tom".equals(input.getText().toString())

userinput你根本不需要

于 2013-05-13T14:12:06.837 回答
0

您只需userinput在 onCreate 期间设置一次。

final String userinput = input.toString();

之后,它再也不会被填满。但input.toString()不返回实际输入的文本。你需要input.getText().toString();那个。

这意味着您userinput始终是空的,因为您的 EditText 在活动开始期间可能是空的。因此,当您按下按钮时,您userinput的仍然是空的,这意味着它不等于“tom”,因此调用了您的else.

如果要在实际按下按钮时检查输入是否等于它,请在按下按钮时实际填写userinput。像这样:

@Override
        public void onClick(View v) {
           userinput = input.getText().toString();
            // TODO Auto-generated method stub
            if (userinput.equals("tom")) {
                Intent myintent = new Intent (MainActivity.this,MainActivity2.class) ;
                startActivity (myintent);    
                }else{
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT ).show();
                }
        }
于 2013-05-13T14:19:37.500 回答
0

嘿,你需要声明你的editText之前onCreate()和你String不应该的final

public class MainActivity extends Activity {
EditText input;
String userinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button submit = (Button) findViewById(R.id.button1);
    input = (EditText) findViewById(R.id.editText1);

    submit.setOnClickListener(new OnClickListener() {

        @Override
    public void onClick(View v) {
         userinput = input.getText().toString();
        Toast.makeText(getApplicationContext(), userinput, Toast.LENGTH_SHORT ).show();
            if (userinput.equals("tom")) {
        Intent myintent = new Intent (MainActivity.this,MainActivity2.class) ;
        startActivity (myintent);
        Toast.makeText(getApplicationContext(), "Correct Answer", Toast.LENGTH_SHORT ).show();
        }else{
            Toast.makeText(getApplicationContext(), "Wrong Answer"+userinput, Toast.LENGTH_SHORT ).show();
        }
    }
});



}
}

嘿,这是我的保管箱文件夹的链接。关联

于 2013-05-13T14:13:43.867 回答
0

获取 EditText 中包含的文本的正确方法是使用EditText.getText(). 尝试替换此行:

final String userinput = input.toString();

有类似的东西:

final String userinput = input.getText().toString();

此外,这应该在单击后完成。将此行放在 onCreate 将始终将 userinput 设置为空字符串或 EditText 的默认值,因为 onCreate 仅在执行开始时运行。

于 2013-05-13T14:14:15.603 回答