* Cara menggunakan “PickerView” dan “ListView”
* Menambahkan event handler pada objek View
1. Time Picker
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#2980b9"
android:textColor="#FFFFFF"
android:text="TOMBOL"/>
</LinearLayout>
java
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TimePicker timePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
Button button = (Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
toast();
}
});
}
public void toast(){
int hour, minute;
int currentApiVersion = Build.VERSION.SDK_INT;
// LOLLIPOP_MR1 adalah versi API level 22
if (currentApiVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
// Jika API Level adalah di atas 22
hour = timePicker.getHour();
minute = timePicker.getMinute();
} else {
// Jika APL Level adalah 22 ke bawah
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
}
Toast.makeText(getApplicationContext(), "Time selected : " + hour + ":" + minute,
Toast.LENGTH_LONG).show();
}
}
2. Date Picker
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2980b9"
android:textColor="#FFFFFF"
android:text="TOMBOL"/>
</LinearLayout>
java
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.Toast;
public class MainActivity extends AppCompatActivity {
DatePicker datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datePicker = (DatePicker) findViewById(R.id.datePicker);
Button button = (Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
toast();
}
});
}
public void toast(){
Toast.makeText(getApplicationContext(), "Tanggal : " + datePicker.getDayOfMonth() + "/" + (datePicker.getMonth()+1) + "/" + datePicker.getYear(),
Toast.LENGTH_LONG).show();
}
}
Menampilkan daftar item panjang menggunakan ListView
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2980b9"
android:textColor="#FFFFFF"
android:text="TOMBOL"/>
</LinearLayout>
java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String presidenIndonesia[] = {"Soekarno", "Soeharto", "B. J. Habibie",
"Abdurrahman Wahid", "Megaputri Sukarnoputri",
"Susilo Bambang Yudhoyono", "Joko Widodo"};
ListView myListView = (ListView) findViewById(R.id.myListView);
myListView.setAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, presidenIndonesia));
}
}
Masih ada view seperti SpinnerView dan event handler lain yang bisa kita coba. Jika ingin mempelajarinya silakan baca sumber pemrograman Android di internet atau menggunakan buku di bawah :
- Professional Android 4 Development (Reto Meier) : Chapter 4 Building User Interfaces, Lihat dari Halaman 132
- Beginning Android Application Development (Wei Meng Lee) : Chapter 5 Designing Your User Interface with Views, Lihat dari Halaman 144
Tidak ada komentar