Change value in EditText

et = (EditText) findViewById(R.id.editText1); iv = (ImageView) findViewById(R.id.imageView1); Case R.id.ibGo: String str = et.getText().toString(); if (str.contentEquals("password")){ Intent levelTwo = new Intent ( "com.xxx.xxx.LEVELTWO"); startActivity(levelTwo); } else if (str.contentEquals("music")){ Intent levelFour = new Intent ( "com.xxx.xxx.LEVELFOUR"); startActivity(levelFour); } else { vib.vibrate(300); iv.setVisibility(View.VISIBLE); } break; 

When I write the “password” in the editor in the emulator, the emulator works as expected. But when I (for example) write “Passwords” (Capital P), the application stops unexpectedly.

What can I do to prevent it from falling through in capital letters?

+4
source share
2 answers

Strings are case sensitive.

Instead

 if (str.contentEquals("password")){ //do something } 

DOING SO!!

 if (str.equalsIgnoreCase("password")){ //do something } 
+1
source

I think your question and your problem are two different things, therefore:

What can I do to prevent him from responding to capital letters, to make it not random?

You can use equalsIgnoreCase ( str.equalsIgnoreCase("password") ) to make the comparison case insensitive, but I think your problem is here:

But when I, for example, write "Passwords" (Capital P), the application unexpectedly stops.

This is probably due to a problem in the else block, it looks like either vib or iv are null.

+2
source

Source: https://habr.com/ru/post/1439023/


All Articles