在editText中的每个字符串更改中自动单击saveButton
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在editText中的每个字符串更改中自动单击saveButton相关的知识,希望对你有一定的参考价值。
接收短信,然后将EditText设置为msgBody
public class SmsBroadcastReceiver extends BroadcastReceiver {
//.....
((EditText)MainActivity.mThis.findViewById(R.id.editTextName)).setText(msgBody);}
这个错误是在View中无法应用于android.view.View.Onclicklistiner
//onCreate
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
buttonSave.performClick(this);
}
});
单击buttonSave时,该消息将自动保存到SQLite并同步到mysql
private void saveNameToServer() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Saving Name...");
progressDialog.show();
final String name = editTextName.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_SAVE_NAME,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
try {
JSONObject obj = new JSONObject(response);
if (!obj.getBoolean("error")) {
//if there is a success
//storing the name to sqlite with status synced
saveNameToLocalStorage(name, NAME_SYNCED_WITH_SERVER);
} else {
//if there is some error
//saving the name to sqlite with status unsynced
saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
//on error storing the name to sqlite with status unsynced
saveNameToLocalStorage(name, NAME_NOT_SYNCED_WITH_SERVER);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("name", name);
return params;
}
};
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
@Override
public void onClick(View view) {
saveNameToServer();
}
当EditText值更改时,还有其他方法可以自动单击按钮吗?
答案
而不是调用点击buttonSave.performClick(this);
只需调用saveNameToServer();
方法来保存您的数据。
buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//buttonSave.performClick(this); // remove, not required
saveNameToServer(); // save your data
}
});
以上是关于在editText中的每个字符串更改中自动单击saveButton的主要内容,如果未能解决你的问题,请参考以下文章
如果 EditText 字段中有文本,如何更改 backgroundtint 颜色?