![](/img/trans.png)
[英]How to display AlertDialog with ImageView and EditText without layout.xml?
[英]How to get the Value of an EditText (Layout.xml) on public interface?
我還在學習 Android 編程,我需要幫助...
我使用 Retrofit 和 Yahoo Weather API 創建(從在線教程)天氣應用程序。
但是這個應用程序只是為一個特定的城市而制作的。
現在我想添加兩個 EditText (City, Country) 並將這些內容放入 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("......")
我收到錯誤:
錯誤:(20, 42) 錯誤:找不到符號方法 findViewById(int) 錯誤:(24, 145) 錯誤:屬性值必須是常量
我這里的朋友想說的是,只要您在 VIEW 對象之外,您就可以輕松調用findViewById()
,但是一旦您在該元素內並且正在尋找findViewById()
您需要在 VIEW 之前編寫對象之前像這樣:
Context.findViewById ()
或者在你的情況下
WeatherAPI.findViewById ()
僅當當前 Activity 布局由setContentView設置時,在 Activity 對象上調用findViewById()
才會起作用。 如果通過其他方式添加布局,則需要布局的 View 對象並在其上調用findViewById()
。
像這樣..
View v = inflater.inflate(id_layout);
View innerView = v.findViewById(R.id.etCity);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.