Android - Detect specific word in an Edittext

Asked

Viewed 76 times

1

How do I make it so that when a button is pressed, it activates a function only if a specific word has been typed at 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();
}

}
});

1 answer

4


Use the method contains() string class:

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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.