gpt4 book ai didi

java - 如何在公共(public)界面上获取 EditText (Layout.xml) 的值?

转载 作者:行者123 更新时间:2023-11-30 01:29:06 25 4
gpt4 key购买 nike

我仍在学习 Android 编程,我需要帮助...
我使用 Retrofit 和 Yahoo Weather API 创建(根据在线教程)Weather App。
但是这个应用程序只针对一个特定的城市。
现在我想添加两个 EditText(城市、国家/地区)并将这些内容放入 API 链接。

我制作了activity_settings.xml

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etCity"
android:text="Zagreb"
android:layout_gravity="center_horizontal" />

和 WeatherAPI.java

public interface WeatherAPI{

String BASE_URL = "https://query.yahooapis.com/v1/public/";
//String Zagreb = "Zagreb";
EditText cityText = (EditText) findViewById (R.id.etCity);
String city = cityText.getText().toString();


@GET("yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city + "%2C%20Hr%22)%20and%20u%3D'c'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
Call<Weather> getWeather();

class Factory {

public static WeatherAPI service;

public static WeatherAPI getIstance() {

if (service == null) {

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();


service = retrofit.create(WeatherAPI.class);
return service;
} else {
return service;
}
}
}

如你所见,我添加了

EditText cityText = (EditText) findViewById (R.id.etCity);
String city = cityText.getText().toString();

并将城市放入@GET("......")

我收到错误:

Error:(20, 42) error: cannot find symbol method findViewById(int) Error:(24, 145) error: attribute value must be constant

最佳答案

我这里的 friend 想说的是,只要您在 VIEW 对象之外,就可以轻松调用 findViewById(),但是一旦您在该元素内并且您正在寻找findViewById() 你需要在VIEW对象之前这样写:

Context.findViewById ()  

或者你的情况

WeatherAPI.findViewById ()

关于java - 如何在公共(public)界面上获取 EditText (Layout.xml) 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36010871/

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