Check whether the dynamically added TextEdit contains text in Android
I rewrite onfocuschanged to determine the TextEdit that has just lost focus. The key is to delete this TextEdit (if there is no text in it). The problem is that if ((EditText... Is invalid. I can't parse the method 'gettext()'. I tried casting to tell it is an EditText view
Listitemsview is the ID of LinearLayout, and listitems is the ArrayList of EditText
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
int removeCurrent = listItems.indexOf(this)+1;
// code to execute when EditText loses focus
if((EditText)view.getText().equals("")) {
}
listItemsView.removeViewAt(removeCurrent);
listItems.remove(removeCurrent);
}
}
I also tried:
if((EditText)listItemsView.getChildAt(removeCurrent).getText().equals(""))
The same results were obtained
resolvent:
If you want to cast a value to a type:
(T)V
Where t is the type to convert to and V is the value
Good so far?
In your code
(EditText)view.getText().equals("")
What is V and what is t? The answer may surprise you! T is obviously EditText, but v here is actually view. Gettext(). Equals ("")!
In other words, you didn't cast the view to EditText. That's why the compiler couldn't find a method named gettext. To tell the compiler to specifically convert the view, add parentheses:
((EditText)view).getText().equals("")