如何使用 onClickListener 执行两个方法?

huangapple go评论80阅读模式
英文:

How do I execute two methods using onClickListener?

问题

  1. 我是安卓工作室的新手,我正在尝试使用OpenWeatherMap API创建一个简单的天气应用程序。我正在使用OkHttp库执行GET请求。它的功能是通过```EditText```获取输入,并在按钮点击时更新```TextView```
  2. 但问题是,```TextView```在第一次点击```Button```后要经过两次点击才会更新。我希望在第一次点击后立即更新它。那么,我该如何解决这个问题呢?
  3. 以下是我的代码:
  4. ```java
  5. public class MainActivity extends AppCompatActivity {
  6. private EditText cityName;
  7. private TextView weatherData;
  8. private TextView hiddenText;
  9. private Button getBtn;
  10. public String s = "";
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. weatherData = (TextView)findViewById(R.id.weatherText);
  16. getBtn = (Button)findViewById(R.id.getData);
  17. cityName = (EditText)findViewById(R.id.cityName);
  18. getBtn.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View v) {
  21. getWeatherData(cityName.getText().toString());
  22. weatherData.setText(s);
  23. }
  24. });
  25. }
  26. public void getWeatherData(String cityText){
  27. String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
  28. OkHttpClient client = new OkHttpClient();
  29. final Request request = new Request.Builder().url(url).build();
  30. client.newCall(request).enqueue(new Callback() {
  31. @Override
  32. public void onFailure(Call call, IOException e) {
  33. MainActivity.this.runOnUiThread(new Runnable() {
  34. @Override
  35. public void run() {
  36. s = "出现了一些问题!";
  37. }
  38. });
  39. }
  40. @Override
  41. public void onResponse(Call call, final Response response) throws IOException {
  42. if(response.isSuccessful()){
  43. MainActivity.this.runOnUiThread(new Runnable() {
  44. @Override
  45. public void run() {
  46. try{
  47. s = response.body().string();
  48. }
  49. catch (IOException ioe){
  50. s = "获取JSON时出错。";
  51. }
  52. }
  53. });
  54. }
  55. }
  56. });
  57. }
  58. }

我知道我可以在onResponse中更新TextView,但我想知道是否可以通过onClickListener来更新它。如果不可能,我应该使用哪种方法?任何帮助将不胜感激。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m new to android studio and I&#39;m trying to create a simple weather app using OpenWeatherMap API. I am using OkHttp library to perform a GET request. All it does is take an input throught ```EditText``` and update the ```TextView``` on button click using a ```Button```.
  4. But the problem is, the ```TextView``` updates after two clicks on the ```Button```. I want to update it right after the first click. So, how do I go over this?
  5. Here is my code:

public class MainActivity extends AppCompatActivity {

  1. private EditText cityName;
  2. private TextView weatherData;
  3. private TextView hiddenText;
  4. private Button getBtn;
  5. public String s = &quot;&quot;;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. weatherData = (TextView)findViewById(R.id.weatherText);
  11. getBtn = (Button)findViewById(R.id.getData);
  12. cityName = (EditText)findViewById(R.id.cityName);
  13. getBtn.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. getWeatherData(cityName.getText().toString());
  17. weatherData.setText(s);
  18. }
  19. });
  20. }
  21. public void getWeatherData(String cityText){
  22. String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
  23. OkHttpClient client = new OkHttpClient();
  24. final Request request = new Request.Builder().url(url).build();
  25. client.newCall(request).enqueue(new Callback() {
  26. @Override
  27. public void onFailure(Call call, IOException e) {
  28. MainActivity.this.runOnUiThread(new Runnable() {
  29. @Override
  30. public void run() {
  31. s = &quot;Something went wrong!&quot;;
  32. }
  33. });
  34. }
  35. @Override
  36. public void onResponse(Call call, final Response response) throws IOException {
  37. if(response.isSuccessful()){
  38. MainActivity.this.runOnUiThread(new Runnable() {
  39. @Override
  40. public void run() {
  41. try{
  42. s = response.body().string();
  43. }
  44. catch (IOException ioe){
  45. s = &quot;Error while getting JSON.&quot;;
  46. }
  47. }
  48. });
  49. }
  50. }
  51. });
  52. }

}

  1. I know that I can update the ```TextView``` in ```onResponse``` itself but I wanna know if it is possible to update it through ```onClickListener```. If it&#39;s not possible, which method should I use? Any help would be appreciated.
  2. </details>
  3. # 答案1
  4. **得分**: 1
  5. ```java
  6. public class MainActivity extends AppCompatActivity {
  7. private EditText cityName;
  8. private TextView weatherData;
  9. private TextView hiddenText;
  10. private Button getBtn;
  11. public String s = "";
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. weatherData = (TextView)findViewById(R.id.weatherText);
  17. getBtn = (Button)findViewById(R.id.getData);
  18. cityName = (EditText)findViewById(R.id.cityName);
  19. getBtn.setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. getWeatherData(cityName.getText().toString());
  23. }
  24. });
  25. }
  26. public void getWeatherData(String cityText){
  27. String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
  28. OkHttpClient client = new OkHttpClient();
  29. final Request request = new Request.Builder().url(url).build();
  30. client.newCall(request).enqueue(new Callback() {
  31. @Override
  32. public void onFailure(Call call, IOException e) {
  33. MainActivity.this.runOnUiThread(new Runnable() {
  34. @Override
  35. public void run() {
  36. s = "Something went wrong!";
  37. }
  38. });
  39. }
  40. @Override
  41. public void onResponse(Call call, final Response response) throws IOException {
  42. if(response.isSuccessful()){
  43. MainActivity.this.runOnUiThread(new Runnable() {
  44. @Override
  45. public void run() {
  46. try{
  47. s = response.body().string();
  48. weatherData.setText(s);
  49. }
  50. catch (IOException ioe){
  51. s = "Error while getting JSON.";
  52. }
  53. }
  54. });
  55. }
  56. }
  57. });
  58. }
  59. }
英文:

you have to update text value in server response call back

  1. public class MainActivity extends AppCompatActivity {
  2. private EditText cityName;
  3. private TextView weatherData;
  4. private TextView hiddenText;
  5. private Button getBtn;
  6. public String s = &quot;&quot;;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. weatherData = (TextView)findViewById(R.id.weatherText);
  12. getBtn = (Button)findViewById(R.id.getData);
  13. cityName = (EditText)findViewById(R.id.cityName);
  14. getBtn.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. getWeatherData(cityName.getText().toString());
  18. }
  19. });
  20. }
  21. public void getWeatherData(String cityText){
  22. String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
  23. OkHttpClient client = new OkHttpClient();
  24. final Request request = new Request.Builder().url(url).build();
  25. client.newCall(request).enqueue(new Callback() {
  26. @Override
  27. public void onFailure(Call call, IOException e) {
  28. MainActivity.this.runOnUiThread(new Runnable() {
  29. @Override
  30. public void run() {
  31. s = &quot;Something went wrong!&quot;;
  32. }
  33. });
  34. }
  35. @Override
  36. public void onResponse(Call call, final Response response) throws IOException {
  37. if(response.isSuccessful()){
  38. MainActivity.this.runOnUiThread(new Runnable() {
  39. @Override
  40. public void run() {
  41. try{
  42. s = response.body().string();
  43. weatherData.setText(s);
  44. }
  45. catch (IOException ioe){
  46. s = &quot;Error while getting JSON.&quot;;
  47. }
  48. }
  49. });
  50. }
  51. }
  52. });
  53. }
  54. }

答案2

得分: 0

  1. public class MainActivity extends AppCompatActivity {
  2. private EditText cityName;
  3. private TextView weatherData;
  4. private TextView hiddenText;
  5. private Button getBtn;
  6. public String s = "";
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. weatherData = (TextView)findViewById(R.id.weatherText);
  12. getBtn = (Button)findViewById(R.id.getData);
  13. cityName = (EditText)findViewById(R.id.cityName);
  14. getBtn.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. getWeatherData(cityName.getText().toString());
  18. }
  19. });
  20. }
  21. public void getWeatherData(String cityText){
  22. String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
  23. OkHttpClient client = new OkHttpClient();
  24. final Request request = new Request.Builder().url(url).build();
  25. client.newCall(request).enqueue(new Callback() {
  26. @Override
  27. public void onFailure(Call call, IOException e) {
  28. MainActivity.this.runOnUiThread(new Runnable() {
  29. @Override
  30. public void run() {
  31. s = "Something went wrong!";
  32. }
  33. });
  34. }
  35. @Override
  36. public void onResponse(Call call, final Response response) throws IOException {
  37. if(response.isSuccessful()){
  38. MainActivity.this.runOnUiThread(new Runnable() {
  39. @Override
  40. public void run() {
  41. try{
  42. s = response.body().string();
  43. weatherData.setText(s);
  44. }
  45. catch (IOException ioe){
  46. s = "Error while getting JSON.";
  47. }
  48. }
  49. });
  50. }
  51. }
  52. });
  53. }
  54. }
英文:

You are setting data on edit text on click after calling GET Request.
Update the textview with the data , once you get the response.
public class MainActivity extends AppCompatActivity {

  1. private EditText cityName;
  2. private TextView weatherData;
  3. private TextView hiddenText;
  4. private Button getBtn;
  5. public String s = &quot;&quot;;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. weatherData = (TextView)findViewById(R.id.weatherText);
  11. getBtn = (Button)findViewById(R.id.getData);
  12. cityName = (EditText)findViewById(R.id.cityName);
  13. getBtn.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. getWeatherData(cityName.getText().toString());
  17. }
  18. });
  19. }
  20. public void getWeatherData(String cityText){
  21. String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
  22. OkHttpClient client = new OkHttpClient();
  23. final Request request = new Request.Builder().url(url).build();
  24. client.newCall(request).enqueue(new Callback() {
  25. @Override
  26. public void onFailure(Call call, IOException e) {
  27. MainActivity.this.runOnUiThread(new Runnable() {
  28. @Override
  29. public void run() {
  30. s = &quot;Something went wrong!&quot;;
  31. }
  32. });
  33. }
  34. @Override
  35. public void onResponse(Call call, final Response response) throws IOException {
  36. if(response.isSuccessful()){
  37. MainActivity.this.runOnUiThread(new Runnable() {
  38. @Override
  39. public void run() {
  40. try{
  41. s = response.body().string();
  42. weatherData.setText(s);
  43. }
  44. catch (IOException ioe){
  45. s = &quot;Error while getting JSON.&quot;;
  46. }
  47. }
  48. });
  49. }
  50. }
  51. });
  52. }
  53. }

huangapple
  • 本文由 发表于 2020年10月24日 15:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64511126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定