<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android簡單實現天氣預報App的具體程式碼,供大家參考,具體內容如下
首頁UI
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/week10_3" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="133dp" android:layout_height="81dp" android:text="確定" android:textSize="30dp" android:fontFamily="sans-serif-black" android:gravity="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/address" android:layout_width="214dp" android:layout_height="98dp" android:layout_marginTop="180dp" android:ems="10" android:inputType="textPersonName" android:text="請選擇城市" android:gravity="center" android:textSize="30dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
效果:
展示頁面UI
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/view" tools:context=".ShowActivity"> <TextView android:id="@+id/textView" android:layout_width="107dp" android:layout_height="59dp" android:layout_marginStart="16dp" android:layout_marginTop="36dp" android:gravity="center" android:text="地址" android:textColor="@color/white" android:textSize="30dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="153dp" android:layout_height="142dp" android:fontFamily="sans-serif-black" android:gravity="center" android:text="氣溫" android:textColor="@color/white" android:textSize="70dp" app:layout_constraintStart_toStartOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView" /> <TextView android:id="@+id/textView4" android:layout_width="98dp" android:layout_height="55dp" android:fontFamily="sans-serif-black" android:gravity="center" android:text="天氣" android:textColor="@color/white" android:textSize="30dp" app:layout_constraintBottom_toBottomOf="@+id/textView3" app:layout_constraintStart_toEndOf="@+id/textView3" /> <TextView android:id="@+id/textView5" android:layout_width="251dp" android:layout_height="60dp" android:fontFamily="sans-serif-black" android:gravity="left" android:text="風力風向 溼度" android:textColor="@color/white" android:textSize="23dp" app:layout_constraintStart_toStartOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/textView3" /> <TextView android:id="@+id/textView6" android:layout_width="97dp" android:layout_height="44dp" android:text="空氣質量" android:textColor="@color/white" android:gravity="center" android:textSize="20dp" android:fontFamily="sans-serif-black" app:layout_constraintBottom_toTopOf="@+id/textView4" app:layout_constraintEnd_toEndOf="parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="185dp" android:layout_height="190dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent"> <TextView android:id="@+id/texttoday" android:layout_width="70dp" android:layout_height="45dp" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:fontFamily="sans-serif-black" android:gravity="center" android:text="今天" android:textColor="@color/white" android:textSize="24dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/weathertoday" android:layout_width="100dp" android:layout_height="45dp" android:fontFamily="sans-serif-black" android:gravity="center" android:text="TextView" android:textColor="@color/white" android:textSize="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@+id/texttoday" /> <TextView android:id="@+id/temtoday" android:layout_width="100dp" android:layout_height="45dp" android:layout_marginTop="30dp" android:fontFamily="sans-serif-black" android:gravity="left" android:text="TextView" android:textColor="@color/white" android:textSize="24dp" app:layout_constraintStart_toStartOf="@+id/texttoday" app:layout_constraintTop_toBottomOf="@+id/texttoday" /> <TextView android:id="@+id/qualtoday" android:layout_width="50dp" android:layout_height="45dp" android:fontFamily="sans-serif-black" android:gravity="left" android:text="良" android:textColor="@color/white" android:textSize="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@+id/temtoday" /> </androidx.constraintlayout.widget.ConstraintLayout> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="185dp" android:layout_height="190dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"> <TextView android:id="@+id/texttomo" android:layout_width="70dp" android:layout_height="45dp" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:text="明天" android:textSize="24dp" android:textColor="@color/white" android:gravity="center" android:fontFamily="sans-serif-black" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/weathertomo" android:layout_width="100dp" android:layout_height="45dp" android:layout_marginEnd="15dp" android:fontFamily="sans-serif-black" android:gravity="center" android:text="TextView" android:textColor="@color/white" android:textSize="24dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="@+id/texttomo" /> <TextView android:id="@+id/temtomo" android:layout_width="100dp" android:layout_height="45dp" android:layout_marginTop="30dp" android:text="TextView" android:textSize="24dp" android:textColor="@color/white" android:gravity="center" android:fontFamily="sans-serif-black" app:layout_constraintStart_toStartOf="@+id/texttomo" app:layout_constraintTop_toBottomOf="@+id/texttomo" /> <TextView android:id="@+id/qualtomo" android:layout_width="50dp" android:layout_height="45dp" android:text="TextView" android:textSize="24dp" android:textColor="@color/white" android:gravity="center" android:fontFamily="sans-serif-black" app:layout_constraintEnd_toEndOf="@+id/weathertomo" app:layout_constraintTop_toTopOf="@+id/temtomo" /> </androidx.constraintlayout.widget.ConstraintLayout> <TextView android:id="@+id/textView16" android:layout_width="251dp" android:layout_height="60dp" android:fontFamily="sans-serif-black" android:gravity="left" android:text="TextView" android:textColor="@color/white" android:textSize="23dp" app:layout_constraintStart_toStartOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/textView5" /> </androidx.constraintlayout.widget.ConstraintLayout>
效果:
這裡我將首頁設定一個輸入框,裡面輸入地點,然後將地點的值傳給展示頁面,讓展示頁面接收到地點後再連線對應的API並呼叫資料。
1.首頁
程式碼如下(範例):
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); ImmersionBar.with(this) //.statusBarColor(R.color.purple_200) //不寫預設透明 .init(); TextView textView=findViewById(R.id.text1); EditText editText=findViewById(R.id.address); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String address=editText.getText().toString(); Intent intent=new Intent(MainActivity.this,ShowActivity.class); intent.putExtra("address",address); startActivity(intent); } }); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(editText.getText().toString()!=null){ editText.setText(""); } } }); }
2.展示頁面
程式碼如下(範例):
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); getSupportActionBar().hide(); ImmersionBar.with(this) //.statusBarColor(R.color.purple_200) //不寫預設透明 .init(); TextView textView=findViewById(R.id.textView); TextView tem=findViewById(R.id.textView3); TextView wind=findViewById(R.id.textView5); TextView aqi=findViewById(R.id.textView6); TextView weather=findViewById(R.id.textView4); TextView ziwaixian=findViewById(R.id.textView16); TextView t2=findViewById(R.id.weathertoday); TextView t3=findViewById(R.id.temtoday); TextView t4=findViewById(R.id.qualtoday); TextView m2=findViewById(R.id.weathertomo); TextView m3=findViewById(R.id.temtomo); TextView m4=findViewById(R.id.qualtomo); Intent intent=getIntent(); String address=getIntent().getStringExtra("address"); textView.setText(address); //c5494be085dd41dca6ec4ffa59c9ed6a String code="APPCODE "+"c5494be085dd41dca6ec4ffa59c9ed6a"; Api api=RetrofitUtils.getRetrofit("https://ali-weather.showapi.com/").create(Api.class); Call<WeatherResult> weatherResultCall=api.getWeather(code,address); weatherResultCall.enqueue(new Callback<WeatherResult>() { @Override public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) { WeatherResult weatherResult = response.body(); t2.setText(weatherResult.showapi_res_body.f1.day_weather); t3.setText(weatherResult.showapi_res_body.f1.day_air_temperature+"/"+weatherResult.showapi_res_body.f1.night_air_temperature+"℃"); t4.setText(weatherResult.showapi_res_body.f1.jiangshui); m2.setText(weatherResult.showapi_res_body.f2.day_weather); m3.setText(weatherResult.showapi_res_body.f2.day_air_temperature+"/"+weatherResult.showapi_res_body.f2.night_air_temperature+"℃"); m4.setText(weatherResult.showapi_res_body.f2.jiangshui); tem.setText(weatherResult.showapi_res_body.now.temperature+"℃"); weather.setText(weatherResult.showapi_res_body.now.weather); wind.setText(weatherResult.showapi_res_body.now.wind_direction+weatherResult.showapi_res_body.now.wind_power+" "+weatherResult.showapi_res_body.now.sd); aqi.setText("aqi:"+weatherResult.showapi_res_body.now.aqi); ziwaixian.setText("紫外線強度:"+weatherResult.showapi_res_body.f1.ziwaixian); } @Override public void onFailure(Call<WeatherResult> call, Throwable t) { } }); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45