時間選擇器的使用:git
//這裏設置的是選擇以後能夠回顯數據,因此先建立一個SP設置基本數值
String string = getSharedPreferences("tag", MODE_PRIVATE).getString("date", "40#0#0");
String[] split = string.split("#");
String s = split[0];
String s1 = split[1];
String s2 = split[2];
//分割以後獲得的並非具體的時間,而是年,月,日的位置。
int i1 = Integer.parseInt(s);
int i2 = Integer.parseInt(s1);
int i3 = Integer.parseInt(s2);
//這裏直接建立 DatePickerDialog,同時把值傳遞給構造方法
DatePickerDialog.Builder builder=new DatePickerDialog.Builder(this,i1,i2,i3);
//經過builder設置時間
DatePickerDialog dialog=builder.setOnDateSelectedListener(new DatePickerDialog.OnDateSelectedListener() {github
@Override public void onDateSelected(int[] dates) { Toast.makeText(getApplicationContext(), dates[0] + "#" + dates[1] + "#" + dates[2], Toast.LENGTH_SHORT).show(); } @Override public void setCurrentItem(int positionYear, int positionMonth, int positionDay) { //設置完成時間以後,保存數據。用於下次回顯數據。 getSharedPreferences("tag",MODE_PRIVATE).edit().putString("date",positionYear+"#" +positionMonth+"#"+positionDay).commit(); }
}).create();
//最後顯示Dialog
dialog.show();ide
設置省市聯動的選擇器:ui
RegionPickerDialog.Builder builder = new RegionPickerDialog.Builder(this);
RegionPickerDialog dialog = builder.setOnRegionSelectedListener(new RegionPickerDialog.OnRegionSelectedListener() {this
@Override public void onRegionSelected(String[] cityAndArea) { Toast.makeText(getApplicationContext(), cityAndArea[0] + "#" + cityAndArea[1], Toast.LENGTH_SHORT).show(); }
}).create();spa
dialog.show();code