当前位置:网站首页>DatePickerDialog and trimepickerdialog

DatePickerDialog and trimepickerdialog

2022-07-07 17:37:00 XLMN

DatePickerDialog and trimepickerDialog
Set time dialog box and date dialog box
public class MainActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pickerdialog);
Button datebt = findViewById(R.id.datebt);
Button timebt = findViewById(R.id.timebt);
// Bind listeners for the set date button
datebt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar ca = Calendar.getInstance();
// Create a DatePickerDialog Dialog instance , And show him // Bind listener
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
EditText show = findViewById(R.id.show);
show.setText(“ You chose :” + year + “ year ” + (month + 1) + “ month ” + dayOfMonth + “ Japan ”);
}

            }
                    // Set the initial date 
                    , ca.get(Calendar.YEAR),
                    ca.get(Calendar.MONTH),
                    ca.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
    // Set the listener for the set time button 
    timebt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar ca1 = Calendar.getInstance();
            // Create a TimePickerDialog Instance and display it 
            new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    EditText et = findViewById(R.id.show);
                    et.setText(" You chose :" + hourOfDay + " when " + minute + " branch ");
                }
            }
                    // Set initial time 
                    , ca1.get(Calendar.HOUR_OF_DAY),
                    ca1.get(Calendar.MINUTE), true
                    //true use 24 hourly 

            ).show();
        }
    });
}

}

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:id="@+id/show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/datebt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Date selection dialog " />

<Button
    android:id="@+id/timebt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=" Time selection dialog " />

 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[XLMN]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071527553330.html