Android之Dialog时间(分秒)对话框
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.demo_dialog.MainActivity" android:orientation="horizontal" > <Button android:id="@+id/main_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="....点我啊."/> </LinearLayout>
MainActivity.java
1 import java.util.Calendar; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.app.DatePickerDialog; 6 import android.app.DatePickerDialog.OnDateSetListener; 7 import android.app.TimePickerDialog; 8 import android.app.TimePickerDialog.OnTimeSetListener; 9 import android.content.DialogInterface; 10 import android.content.DialogInterface.OnMultiChoiceClickListener; 11 import android.os.Bundle; 12 import android.util.Log; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.DatePicker; 16 import android.widget.TimePicker; 17 18 19 public class MainActivity extends Activity { 20 21 private Button btTime; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 btTime = (Button)findViewById(R.id.main_time); 28 btTime.setOnClickListener(clickListener); 29 } 30 31 private View.OnClickListener clickListener = new View.OnClickListener() { 32 33 @Override 34 public void onClick(View v) { 35 switch (v.getId()) { 36 case R.id.main_time: 37 showTimeDialog(); 38 break; 39 } 40 } 41 }; 42 43 private void showTimeDialog(){ 44 45 // 参数1:上下文 参数2:点击事件 参数3:时 参数4:分 参数5:是否是24小时制(true为24小时制,false为12小时制) 46 TimePickerDialog dialog = new TimePickerDialog(this, timeLis, 10, 43, true); 47 dialog.show(); 48 } 49 50 private OnTimeSetListener timeLis = new OnTimeSetListener() { 51 52 @Override 53 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 54 Log.i("aaaa",hourOfDay+"=================="+minute); 55 } 56 }; 57 }