您好,登錄后才能下訂單哦!
小編給大家分享一下Android如何實(shí)現(xiàn)簡(jiǎn)單的城市列表功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內(nèi)容如下
步驟:
1. 在app的gradle里面添加依賴(lài):
com.github.andyoom:citypicker:v1.0.4
2.在項(xiàng)目的build.gradle中添加
maven {url "https://jitpack.io"}
開(kāi)始寫(xiě)代碼:
xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.czx.czx.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="更換城市" android:id="@+id/btn"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30sp" android:id="@+id/name"/> </LinearLayout>
MainActivity代碼:
package com.bwei.czx.czx; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.example.city_picker.CityListActivity; public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); tv = (TextView) findViewById(R.id.name); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CityListActivity.startCityActivityForResult(MainActivity.this); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 101&& resultCode ==102){ String city = data.getStringExtra("city"); tv.setText(city); } } }
在模擬器中實(shí)現(xiàn)效果:
點(diǎn)擊Button會(huì)出現(xiàn)
返回會(huì)把城市名稱(chēng)回傳回來(lái),這就實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的城市列表!
看完了這篇文章,相信你對(duì)“Android如何實(shí)現(xiàn)簡(jiǎn)單的城市列表功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。