Change Android On Focus

I want to update EditText when the user changes focus from edittext to another element. I want to check the contents of edittext, for example, this number is greater than 10 if it changes it to 10.

How can I do it.

+4
source share
3 answers

set setOnFocusChangeListener to your editor ...

 editText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus){ //this if condition is true when edittext lost focus... //check here for number is larger than 10 or not editText.setText("10"); } } }); 
+7
source
  EditText ET =(EditText)findViewById(R.id.yourtextField); ET.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View arg0, boolean arg1) { String myText = ET.getText(); //Do whatever } 
+3
source

If someone wants to do this in kotlin and with data binding, please refer below code

  //first get reference to your edit text var editText:EditText = viewDataBinding.editText // add listner on edit text editText.setOnFocusChangeListener { v, hasFocus -> if(!hasFocus) { if((editText.text.toString().toIntOrNull()>10) {// add any thing in this block editText.text = "10" } } } 
0
source

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


All Articles