我有一组单选按钮,每个单选按钮都有一个编辑文本,我想在焦点退出时保存编辑文本值或单选按钮值,在焦点关闭时执行自动保存。请帮我。
提前致谢。
您可以在和之后使用TextWatcher
来做某事。
这是一个例子:onTextChanged
beforeTextChanged
afterTextChanged
final EditText ed = findViewById(R.id.edittext);
final TextView tv = findViewById(R.id.textview);
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
//Save Your Text With SharedPreference
SharedPreferences sp = getSharedPreferences("TEST", 0);
SharedPreferences.Editor ed_sp = sp.edit();
ed_sp.putString("myText", ed.getText().toString());
ed_sp.commit();
ShowSavedText();
}
void ShowSavedText(){
SharedPreferences sp = getSharedPreferences("TEST", 0);
String savedText = sp.getString("myText", "");
tv.setText(savedText);
}
});
这是您要求的答案。
edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), " auto save Here", Toast.LENGTH_LONG).show();
}
} });
希望这会有所帮助。