Implementing Text Watcher for EditText
set edittext imeOption
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
By using something like this,
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Specify your database function here.
return true;
}
return false;
}
});
Alternatively, you can use the OnEditorActionListener
interface to avoid the anonymous inner class.
TextWatcher for more than one EditText
Suggested solution in @Sebastian Roth's answer is not one instance of TextWatcher
for some EditTexts
. It is one class and n instances of that class for n EditTexts
.
Each EditText has its own Spannable. TextWatcher
's events has this Spannable as s
parameter. I check their hashCode (unique Id of each object). myEditText1.getText() returns that Spannable. So if the myEditText1.getText().hashCode()
equals with s.hashCode()
it means that s
belongs to myEditText1
So if you want to have one instance of TextWatcher
for some EditTexts
you should use this:
private TextWatcher generalTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_onTextChanged(s, start, before, count);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_onTextChanged(s, start, before, count);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_beforeTextChanged(s, start, count, after);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_beforeTextChanged(s, start, count, after);
}
}
@Override
public void afterTextChanged(Editable s) {
if (myEditText1.getText().hashCode() == s.hashCode())
{
myEditText1_afterTextChanged(s);
}
else if (myEditText2.getText().hashCode() == s.hashCode())
{
myEditText2_afterTextChanged(s);
}
}
};
and
myEditText1.addTextChangedListener(generalTextWatcher);
myEditText2.addTextChangedListener(generalTextWatcher);
TextWatcher on two edittexts
As you have set textChangeListener on both edit text and and setting text on both call back events(onTextChangeListener) it will be infinite iteration. It will keep setting text on edittext of one side and call back will be keep calling. Hence this cycle will keep running untill app gets crashed.
Edit
To achieve what you want see below details
You need to keep track currently on what edit text you have focus(This is for setting text in edit text). You need to have 2 boolean variables. Now see below code
public class MainActivity extends AppCompatActivity {
EditText edt1, edt2;
boolean et1Focus, et2Focus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = findViewById(R.id.et1);
edt2 = findViewById(R.id.et2);
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//quando o texto é alterado chamamos o filtro.
if (et1Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor * 100);
edt2.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (et2Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor / 100);
edt1.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et1Focus = b;
}
});
edt2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et2Focus = b;
}
});
}
}
Hope that helps ..If you have any question feel free to ask
Related Topics
Android: Overlay on Android Camera Preview
How to Get the Number of Unread Gmail Mails (On Android)
How to Get Data from Service to Activity
How to Find the Logs on Android Studio
Cmusphinx Pocketsphinx - Recognize All (Or Large Amount) of Words
Rotating Phone Quickly 180 Degrees, Camera Preview Turns Upside Down
How to Get Activity's Content View
Upload a File Through an Http Form, via Multipartentitybuilder, with a Progress Bar
Android Contacts Display Name and Phone Number(S) in Single Database Query
Hiding Title in a Fullscreen Mode
How to Enable Standard Copy Paste for a Textview in Android
Android Slidingdrawer from Top
Android: Open Activity Without Save into the Stack
Error:Cause: Unable to Find Valid Certification Path to Requested Target
How to Send JSON Object to Server Using Volley in Android
Is Ondestroy Not Always Called
How to Add Action Bar from Support Library into Preferenceactivity