Primero el 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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TimePicker"
android:onClick="onClickTimePicker"/>
<TextView
android:id="@+id/texto"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer Picker"/>
</LinearLayout>
Luego el MainActivity.java
package com.example.numberpicker;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
TextView texto=(TextView) findViewById(R.id.texto);
texto.setText(String.format("%1$02d:%2$02d",i,i1));
}
public void onClickTimePicker(View view){
TimePickerDialog time = new TimePickerDialog(this,this,12,0,true);
time.show();
}
}
El resultado final seria el siguiente.
No hay comentarios:
Publicar un comentario