1.适用场合
当我们需要使用时间选择器,又碍于TimePicker的使用会破坏界面的美观性时,可以考虑使用TimePickerDialog.
2.关键代码实现
public class DemoActivity extends Activity {private TextView showTextView = null;private TimePickerDialog mTimePickerDialog = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showTextView = (TextView) findViewById(R.id.show_textview);Button settingButton = (Button) findViewById(R.id.setting_button);settingButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (mTimePickerDialog == null) {showTimePickerDialog();}mTimePickerDialog.show();}});}private void showTimePickerDialog() {TimePickerDialog.OnTimeSetListener pickerOnTimeSetListener = new TimePickerDialog.OnTimeSetListener() {public void onTimeSet(TimePicker view, int hourOfDay, int minute) {showTextView.setText("您设置了时间:" + hourOfDay + "时" + minute + "分");mTimePickerDialog.dismiss();}};Calendar calendar = Calendar.getInstance(TimeZone.getDefault());int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);int minute = calendar.get(Calendar.MINUTE);mTimePickerDialog = new TimePickerDialog(this, pickerOnTimeSetListener, hourOfDay, minute,true);}}
3.笔者最新开发的应用连接
/detail/index/soft_id/2419729?recrefer=SE_D_%E6%A3%B1%E9%95%9C
/apps/com.mastershield.personalstat
如果觉得《Android时间选择器对话框的使用》对你有帮助,请点赞、收藏,并留下你的观点哦!