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

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

How do I execute two methods using onClickListener?

问题

我是安卓工作室的新手,我正在尝试使用OpenWeatherMap API创建一个简单的天气应用程序。我正在使用OkHttp库执行GET请求。它的功能是通过```EditText```获取输入,并在按钮点击时更新```TextView```。

但问题是,```TextView```在第一次点击```Button```后要经过两次点击才会更新。我希望在第一次点击后立即更新它。那么,我该如何解决这个问题呢?

以下是我的代码:

```java
public class MainActivity extends AppCompatActivity {

    private EditText cityName;
    private TextView weatherData;
    private TextView hiddenText;
    private Button getBtn;
    public String s = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weatherData = (TextView)findViewById(R.id.weatherText);
        getBtn = (Button)findViewById(R.id.getData);
        cityName = (EditText)findViewById(R.id.cityName);

        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWeatherData(cityName.getText().toString());
                weatherData.setText(s);
            }
        });

    }


    public void getWeatherData(String cityText){

        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
        OkHttpClient client = new OkHttpClient();

        final Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        s = "出现了一些问题!";
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if(response.isSuccessful()){
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                s = response.body().string();
                            }
                            catch (IOException ioe){
                                s = "获取JSON时出错。";
                            }
                        }
                    });
                }
            }
        });
    }
}

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


<details>
<summary>英文:</summary>

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```.

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?

Here is my code:

public class MainActivity extends AppCompatActivity {

private EditText cityName;
private TextView weatherData;
private TextView hiddenText;
private Button getBtn;
public String s = &quot;&quot;;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    weatherData = (TextView)findViewById(R.id.weatherText);
    getBtn = (Button)findViewById(R.id.getData);
    cityName = (EditText)findViewById(R.id.cityName);

    getBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getWeatherData(cityName.getText().toString());
            weatherData.setText(s);
        }
    });

}


public void getWeatherData(String cityText){

    String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
    OkHttpClient client = new OkHttpClient();

    final Request request = new Request.Builder().url(url).build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    s = &quot;Something went wrong!&quot;;
                }
            });
        }

        @Override
        public void onResponse(Call call, final Response response) throws IOException {
            if(response.isSuccessful()){
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try{
                            s = response.body().string();
                        }
                        catch (IOException ioe){
                            s = &quot;Error while getting JSON.&quot;;
                        }
                    }
                });
            }
        }
    });
}

}


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.

</details>


# 答案1
**得分**: 1

```java
public class MainActivity extends AppCompatActivity {

    private EditText cityName;
    private TextView weatherData;
    private TextView hiddenText;
    private Button getBtn;
    public String s = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weatherData = (TextView)findViewById(R.id.weatherText);
        getBtn = (Button)findViewById(R.id.getData);
        cityName = (EditText)findViewById(R.id.cityName);

        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWeatherData(cityName.getText().toString());
            }
        });

    }


    public void getWeatherData(String cityText){

        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
        OkHttpClient client = new OkHttpClient();

        final Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        s = "Something went wrong!";
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if(response.isSuccessful()){
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                s = response.body().string();
                                weatherData.setText(s);
                            }
                            catch (IOException ioe){
                                s = "Error while getting JSON.";
                            }
                        }
                    });
                }
            }
        });
    }
}
英文:

you have to update text value in server response call back

public class MainActivity extends AppCompatActivity {

    private EditText cityName;
    private TextView weatherData;
    private TextView hiddenText;
    private Button getBtn;
    public String s = &quot;&quot;;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weatherData = (TextView)findViewById(R.id.weatherText);
        getBtn = (Button)findViewById(R.id.getData);
        cityName = (EditText)findViewById(R.id.cityName);

        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWeatherData(cityName.getText().toString());
                
            }
        });

    }


    public void getWeatherData(String cityText){

        String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
        OkHttpClient client = new OkHttpClient();

        final Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        s = &quot;Something went wrong!&quot;;
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if(response.isSuccessful()){
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                s = response.body().string();

                                weatherData.setText(s);
                            }
                            catch (IOException ioe){
                                s = &quot;Error while getting JSON.&quot;;
                            }
                        }
                    });
                }
            }
        });
    }
}

答案2

得分: 0

public class MainActivity extends AppCompatActivity {

    private EditText cityName;
    private TextView weatherData;
    private TextView hiddenText;
    private Button getBtn;
    public String s = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weatherData = (TextView)findViewById(R.id.weatherText);
        getBtn = (Button)findViewById(R.id.getData);
        cityName = (EditText)findViewById(R.id.cityName);

        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWeatherData(cityName.getText().toString());

            }
        });

    }


    public void getWeatherData(String cityText){

        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + cityText + "&appid=ba45ceb57328448f7wd666hdc6d57aaf";
        OkHttpClient client = new OkHttpClient();

        final Request request = new Request.Builder().url(url).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        s = "Something went wrong!";
                    }
                });
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if(response.isSuccessful()){
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                s = response.body().string();
                                weatherData.setText(s);
                            }
                            catch (IOException ioe){
                                s = "Error while getting JSON.";
                            }
                        }
                    });
                }
            }
        });
    }
}
英文:

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 {

    private EditText cityName;
private TextView weatherData;
private TextView hiddenText;
private Button getBtn;
public String s = &quot;&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherData = (TextView)findViewById(R.id.weatherText);
getBtn = (Button)findViewById(R.id.getData);
cityName = (EditText)findViewById(R.id.cityName);
getBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getWeatherData(cityName.getText().toString());
}
});
}
public void getWeatherData(String cityText){
String url = &quot;https://api.openweathermap.org/data/2.5/weather?q=&quot; + cityText + &quot;&amp;appid=ba45ceb57328448f7wd666hdc6d57aaf&quot;;
OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
s = &quot;Something went wrong!&quot;;
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if(response.isSuccessful()){
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try{
s = response.body().string();
weatherData.setText(s);
}
catch (IOException ioe){
s = &quot;Error while getting JSON.&quot;;
}
}
});
}
}
});
}
}

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:

确定