角色计数游戏机器人

时间:2016-03-04 19:59:46

标签: android button android-edittext textview

我正在构建一个简单的字母计数应用程序,它需要来自editText的数字输入,并检查textView(random name)中的字母数是否正确,单击按钮时显示是否正确输入正确的数字或​​不正确,每次都显示不正确。

public class MainActivity extends AppCompatActivity {

    EditText EditTextInput;
    String strInput;
    Button btn;
    TextView txtLenght;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
         EditTextInput= (EditText)findViewById(R.id.editText);

        EditTextInput.getText().toString();

        txtLenght = (TextView)findViewById(R.id.textView2);
        txtLenght.getText().toString();

       btn = (Button)findViewById(R.id.button);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {






                if (EditTextInput==txtLenght )
                {
                    Toast.makeText(getApplicationContext(), "Your Guess is correct", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "Your Guess is wrong!!!", Toast.LENGTH_LONG).show();
            }
        };


});
    }

1 个答案:

答案 0 :(得分:0)

您的代码将是这样的:

public class MainActivity extends AppCompatActivity {

    EditText EditTextInput;
    String strInput;
    Button btn;
    TextView txtLenght;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        EditTextInput= (EditText)findViewById(R.id.editText);
        int length = Integer.parseInt(EditTextInput.getText().toString());
        txtLenght = (TextView)findViewById(R.id.textView2);
        btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(length == txtLenght.getText().toString().length()){
                    Toast.makeText(getApplicationContext(), "Your Guess is correct", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "Your Guess is wrong!!!", Toast.LENGTH_LONG).show();
            }
        };
    });
}