1. 程式人生 > >城市的三級列表

城市的三級列表

//studio匯入依賴

compile 'liji.library.dev:citypickerview:0.3.0'

//佈局檔案

   <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請選擇省市區"
android:id="@+id/go"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="請選擇結果" android:layout_marginTop="30dp"/>

//MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button go;
    private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView(); } private void initView() { go = (Button) findViewById(R.id.go); text = (TextView) findViewById(R.id.text); go.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.go
: CityPicker cityPicker=new CityPicker.Builder(MainActivity.this).textSize(20) .textColor(Color.parseColor("#000000")) .provinceCyclic(true) .cityCyclic(false) .visibleItemsCount(7) .itemPadding(10) .build(); cityPicker.show(); cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() { @Override public void onSelected(String... citySelected) { text.setText("選擇結果:\n省"+citySelected[0]+"\n市:"+citySelected[1]+"\n區:" +citySelected[2]+"\n郵編:"+citySelected[3]); } }); break; } } }

//網址

https://github.com/crazyandcoder/citypicker