Android TimePickerDialog
Dieser Android Code zeigt mehrere Darstellungen und Code von
Android TimePicker als Dialog (TimePickerDialog).
Mit Scoll Numbers digital darstellen und verschiedenen
Layouts im Overlay
Die Darstellung der TimePicker wird beim Aufruf als Layout/Theme
mit gegeben
timePickerDialog=new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//*Return values
_editTextTime.setText(i + ":" + i1);
Toast.makeText(MainActivity.this, "i=" + i + " i1=" + i1, Toast.LENGTH_SHORT).show();
}
},hourOfDay,minute,is24HourView);
|
Zusätzlich kann noch der Titel eingegeben werden und der
Hintergrund transparent geschaltet werden
_timePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
_timePickerDialog.setTitle("Select a
Time");
_timePickerDialog.show();
|
Android Activity
Code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dmeo of
TimePickerDialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="time" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select
Time"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="266dp"
android:onClick="onButtonSelectTimeClick"
/>
</LinearLayout>
|
Java Code
in Android app.java.project.MainActity.java
package com.example.demo_timepicker01;
import androidx.appcompat.app.AppCompatActivity;
import android.app.TimePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TimePickerDialog _timePickerDialog;
EditText _editTextTime;
@Override
protected void onCreate(Bundle savedInstanceState)
{
//--------<
onCreate() >--------
//--< init >--
//< activity >
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//</ activity
>
_editTextTime=findViewById(R.id.editTextTime);
_editTextTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
open_TimePickerDialog();
}
});
//--</ init
>--
//--------</ onCreate()
>--------
}
public void onButtonSelectTimeClick(View view) {
open_TimePickerDialog();
}
private void open_TimePickerDialog(){
//--------<
open_TimePickerDialog() >--------
int hourOfDay=2;
int minute=2;
boolean is24HourView=true;
//Theme_Holo_Light_Dialog
//Theme_Holo_Light_DarkActionBar
//*Top Position
_timePickerDialog=new TimePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
//*Return values
_editTextTime.setText(i + ":" + i1);
Toast.makeText(MainActivity.this, "i=" + i + " i1=" + i1, Toast.LENGTH_SHORT).show();
}
},hourOfDay,minute,is24HourView);
_timePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
_timePickerDialog.setTitle("Select a
Time");
_timePickerDialog.show();
//--------</
open_TimePickerDialog() >--------
}
}
|