Android中获取strings.xml文件中的数据

Android中获取strings.xml文件的数据有以下方式:

(1)在Activity文件中:

方法一:this.getString(R.string.resource_name)

方法二:getResources().getString(R.string.resource_name)

(2)在AndroidManifest.xml或layout等xml文件中:

@string/resource_name

(3)其他文件中:

context.getString(R.string.resource_name)

application.getString(R.string.resource_name)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio显示聚合数据的天气预报,您需要执行以下步骤: 1.获取聚合数据的API密钥 2.创建一个新的Android Studio项目 3.在AndroidManifest.xml文件添加以下权限: ``` <uses-permission android:name="android.permission.INTERNET" /> ``` 4.在app/build.gradle文件添加以下依赖项: ``` implementation 'com.squareup.okhttp3:okhttp:3.11.0' ``` 5.创建一个名为Weather的新Java类 6.在Weather类添加以下代码: ``` import java.io.IOException; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class Weather { private static final String API_KEY = "YOUR_API_KEY"; private static final String BASE_URL = "http://apis.juhe.cn/simpleWeather/query"; public static String getWeather(String city) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(BASE_URL + "?city=" + city + "&key=" + API_KEY) .build(); Response response = client.newCall(request).execute(); return response.body().string(); } } ``` 7.在MainActivity.java文件添加以下代码: ``` import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.IOException; public class MainActivity extends AppCompatActivity { private EditText editTextCity; private Button buttonSearch; private TextView textViewWeather; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextCity = findViewById(R.id.editTextCity); buttonSearch = findViewById(R.id.buttonSearch); textViewWeather = findViewById(R.id.textViewWeather); buttonSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new WeatherTask().execute(editTextCity.getText().toString()); } }); } private class WeatherTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... strings) { try { return Weather.getWeather(strings[0]); } catch (IOException e) { e.printStackTrace(); return null; } } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s != null) { textViewWeather.setText(s); } else { textViewWeather.setText("Error"); } } } } ``` 8.在activity_main.xml文件添加以下布局代码: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editTextCity" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="City" /> <Button android:id="@+id/buttonSearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Search" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textViewWeather" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout> ``` 9.替换上面代码的YOUR_API_KEY为您自己的API密钥并运行应用程序。 10.输入城市名称并单击搜索按钮,将在TextView显示天气预报信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值