How do I set a string for View in onFocusChange?

Asked 2 years ago, Updated 2 years ago, 48 views

In OnFocusChange, check the entered string for error check
If there is an error, I would like to return it to the original string.

String from View of OnFocusChange Arguments I was able to obtain it using toString().
How do I set a string in View?

public void onFocusChange(View v, boolean hasFocus){

    String str_save=";
    if(hasFocus){
        str_save=v.toString();
    } else {
        // Check for errors
        if(isError(v.toString())){
            // If it is an error, I would like to set the string saved in str_save to v.<- I don't know where it is.
        }
    }
            
}

java android

2022-09-30 20:22

1 Answers

[Self-answer]
I'm terribly sorry.It was a mistake to say that toString has a value.
It was resolved using findViewById as follows.

String str_save=";
public void on FocusChange (View v, boolean hasFocus) {

    String str_value;
    EditText mText= (EditText) v.findViewById;
    str_value=mText.getText().toString();
  
    if(hasFocus){
        str_save = str_value;
    } else {
        // Check for errors
        if(isError(str_value)) {
            mText.setText(str_save);
        }
    }
            
}


2022-09-30 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.