Created
May 7, 2020 20:29
-
-
Save susantokun/600ba573b536e5a4c4f83c8abbfe1b9a to your computer and use it in GitHub Desktop.
DatePicker di Android Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.susantokun.project_1; | |
import android.app.DatePickerDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.DatePicker; | |
import android.widget.EditText; | |
import android.widget.RadioButton; | |
import android.widget.RadioGroup; | |
import android.widget.Spinner; | |
import android.widget.TextView; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Locale; | |
public class FormMahasiswa extends AppCompatActivity { | |
private DatePickerDialog datePickerDialog; | |
private SimpleDateFormat dateFormatter; | |
private TextView tvDateResult; | |
private EditText btDatePicker; | |
Button bsimpan; | |
EditText enpm, enama, etempatlahir; | |
TextView thasil; | |
RadioGroup rgjk; | |
Spinner skelas, sagama; | |
private void showDateDialog(){ | |
Calendar newCalendar = Calendar.getInstance(); | |
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { | |
@Override | |
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | |
Calendar newDate = Calendar.getInstance(); | |
newDate.set(year, monthOfYear, dayOfMonth); | |
tvDateResult.setText(dateFormatter.format(newDate.getTime())); | |
} | |
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); | |
datePickerDialog.show(); | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.formmhs); | |
// inisialisasi | |
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US); | |
tvDateResult = (TextView) findViewById(R.id.tv_dateresult); | |
btDatePicker = (EditText) findViewById(R.id.tv_dateresult); | |
bsimpan = (Button) findViewById(R.id.simpan); | |
enpm = (EditText) findViewById(R.id.isinpm); | |
enama = (EditText) findViewById(R.id.isinama); | |
thasil = (TextView) findViewById(R.id.hasil); | |
rgjk = (RadioGroup) findViewById(R.id.jk); | |
skelas = (Spinner) findViewById(R.id.kelas); | |
sagama = (Spinner) findViewById(R.id.agama); | |
etempatlahir = (EditText) findViewById(R.id.tempatlahir); | |
bsimpan.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String inputnpm = String.valueOf(enpm.getText().toString()); | |
String inputnama = String.valueOf(enama.getText().toString()); | |
String inputtempatlahir = String.valueOf(etempatlahir.getText().toString()); | |
String inputotomatis = String.valueOf(tvDateResult.getText().toString()); | |
int gender = rgjk.getCheckedRadioButtonId(); | |
RadioButton jk = (RadioButton) findViewById(gender); | |
String inputjk = String.valueOf(jk.getText().toString()); | |
thasil.setText("\n" + "NPM\t\t\t\t\t\t\t\t\t\t\t: " + inputnpm + "\n" + | |
"Nama\t\t\t\t\t\t\t\t\t\t: " + inputnama + "\n" + | |
"Jenis Kelamin\t\t: " + inputjk + "\n" + | |
"Kelas\t\t\t\t\t\t\t\t\t\t: " + skelas.getSelectedItem().toString() + "\n" + | |
"Agama\t\t\t\t\t\t\t\t\t: " + sagama.getSelectedItem().toString() + "\n" + | |
"Jenis Kelamin\t\t: " + inputtempatlahir + "\n" + | |
"Tanggal Lahir\t\t\t: " + inputotomatis + "\n"); | |
} | |
}); | |
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US); | |
tvDateResult = (TextView) findViewById(R.id.tv_dateresult); | |
btDatePicker = (EditText) findViewById(R.id.tv_dateresult); | |
btDatePicker.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
showDateDialog(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment