Android - Detect specific word in an EditText

1

How do I when a button is pressed, activate a function only if a specific word has been typed in any position in an EditText? Thank you.

main.xml:

<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Digite aqui..."
android:id="@+id/searchfield"/>

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Botão"
android:id="@+id/btn"/>

MainActivity.java:

Button btn = (Button)findViewById(R.id.btn);
final EditText search = (EditText)findViewById(R.id.searchfield);


go.setOnClickListener(new OnClickListener(){
public void onClick(View p1){
//O que devo colocar na condição if?
if(search.getText().toString()){
function();
}

}
});
    
asked by anonymous 16.05.2018 / 18:35

1 answer

4

Use the contains () method of the class String:

if(search.getText().toString().contains("palavra")){
    //entra aqui se a *palavra* estiver no texto
}
else{
    //entra aqui se a *palavra* NÃO estiver no texto
}
    
16.05.2018 / 18:40