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會(huì)出現(xiàn)
返回會(huì)把城市名稱回傳回來,這就實(shí)現(xiàn)了一個(gè)簡單的城市列表!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表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)文章
kotlin anko頁面跳轉(zhuǎn)實(shí)現(xiàn)方式,攜帶參數(shù)或flag
這篇文章主要介紹了kotlin anko頁面跳轉(zhuǎn)實(shí)現(xiàn)方式,攜帶參數(shù)或flag,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android雷達(dá)掃描動(dòng)態(tài)界面制作
這篇文章主要為大家詳細(xì)介紹了Android雷達(dá)掃描動(dòng)態(tài)界面制作資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
android開發(fā)中獲取手機(jī)分辨率大小的方法
不管是在我們的布局還是在實(shí)現(xiàn)代碼中進(jìn)行操控,我們的靈活性都不是局限于一個(gè)固定的數(shù)值,而是面對不同的手機(jī)對象都有一個(gè)適應(yīng)的數(shù)值。2013-04-04
Android編程設(shè)置TextView顏色setTextColor用法實(shí)例
這篇文章主要介紹了Android編程設(shè)置TextView顏色setTextColor用法,結(jié)合實(shí)例形式分析了Android設(shè)置TextView顏色setTextColor、ColorStateList等方法的使用技巧與布局文件的設(shè)置方法,需要的朋友可以參考下2016-01-01
詳解Flutter中StatefulBuilder組件的使用
StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時(shí)僅重建某些小區(qū)域而無需付出太多努力。本文將來詳細(xì)講講它的使用,需要的可以參考一下2022-05-05
Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android獲取點(diǎn)擊屏幕的位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

