Android實(shí)現(xiàn)簡單的城市列表功能
本文實(shí)例為大家分享了Android城市列表簡單用法,供大家參考,具體內(nèi)容如下
步驟:
1. 在app的gradle里面添加依賴:
com.github.andyoom:citypicker:v1.0.4
2.在項(xiàng)目的build.gradle中添加
maven {url "https://jitpack.io"}
開始寫代碼:
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會出現(xiàn)
返回會把城市名稱回傳回來,這就實(shí)現(xiàn)了一個簡單的城市列表!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動下拉框 下拉列表spinner的實(shí)例代碼
- android二級listview列表實(shí)現(xiàn)代碼
- android 支持的語言列表(匯總)
- Android uses-permission權(quán)限列表中文注釋版
- Android通過LIstView顯示文件列表的兩種方法介紹
- Android ExpandableListView展開列表控件使用實(shí)例
- Android實(shí)現(xiàn)獲取應(yīng)用程序相關(guān)信息列表的方法
- android開發(fā)教程之使用listview顯示qq聯(lián)系人列表
- Android用ListView顯示SDCard文件列表的小例子
- Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能
相關(guān)文章
Android學(xué)習(xí)筆記-保存文件(Saving Files)
這篇文章主要介紹了Android中保存文件(Saving Files)的方法,需要的朋友可以參考下2014-10-10Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
這篇文章主要介紹了Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法,本文講解了最簡單的方法、最通用的方法,需要的朋友可以參考下2015-04-04

Android Studio開發(fā)環(huán)境搭建教程詳解

Android Activity與Fragment之間的跳轉(zhuǎn)實(shí)例詳解

怎樣刪除android的gallery中的圖片實(shí)例說明