0
我跟着this tutorial將日期選擇器集成到我的應用程序中,我已經量身定製了原創以滿足我的需求並設法使其工作。不幸的是,作者並沒有太在意使用DatePicker對話框的概念。Android DatePicker初始化時顯示全屏
因此,當應用程序啓動時會顯示全屏日曆,而我只是在點擊按鈕「選擇日期」時拉動日,月和年,我需要刪除什麼?
代碼在這裏:
public class MainActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
TabHost th;
Button changeDate;
TextView displayDate;
public static final int dDialogId = 1;
// date and time
private int mYear;
private int mMonth;
private int mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Set activity to full screen!!
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
// one tab
specs.setContent(R.id.tab1);
// what appears on actual tab
specs.setIndicator("Tools");
th.addTab(specs);
specs = th.newTabSpec("tag2");
// one tab
specs.setContent(R.id.tab2);
// what appears on actual tab
specs.setIndicator("Calorie Calculator");
th.addTab(specs);
specs = th.newTabSpec("tag3");
// one tab
specs.setContent(R.id.tab3);
// what appears on actual tab
specs.setIndicator("Your Stats!");
th.addTab(specs);
initializeVariables();
}
private void initializeVariables() {
mapARun = (Button) findViewById(R.id.btnMapARun);
weightConverter = (Button) findViewById(R.id.btnWeightConverter);
changeDate = (Button) findViewById(R.id.btnChangeDate);
displayDate = (TextView) findViewById(R.id.tvDisplayDate);
changeDate.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
@Override
@Deprecated
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay() {
displayDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnChangeDate:
DatePickerDialog DPD = new DatePickerDialog(this,
mDateSetListener, mYear, mMonth, mDay);
DPD.show();
break;
}
}
}
}
道歉,如果你打的語法錯誤時codei的磨合,我切出了很多其他不相關的功能,
有另一種方式只是展示datepicker何時調用對話框?
有沒有辦法只是設置按鈕來調用'迷你'日期選擇器,當它被選中時,應用程序的設置方式我真的不想調用一個單獨的活動只是爲了設置一個日期 – dyatesupnorth 2013-05-09 15:50:50