gpt4 book ai didi

java - 在 editText 中每次更改字符串时自动单击 saveButton

转载 作者:行者123 更新时间:2023-12-02 11:34:44 24 4
gpt4 key购买 nike

接收短信然后将EditText设置为msgBody

public class SmsBroadcastReceiver extends BroadcastReceiver {
//.....
((EditText)MainActivity.mThis.findViewById(R.id.editTextName)).setText(msgBody);}

错误是View中的this无法应用于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 值更改时自动单击按钮?

最佳答案

无需调用 click buttonSave.performClick(this);,只需调用 saveNameToServer(); 方法即可保存数据。

buttonSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//buttonSave.performClick(this); // remove, not required
saveNameToServer(); // save your data
}
});

关于java - 在 editText 中每次更改字符串时自动单击 saveButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49033959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com