1.3 Inisialisasi TextView Anda dan variabel hitungan skor
1.4 Implementasikan fungsionalitas onClick untuk tombol Anda.
package com.example.safiraaini.scorekeeper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int mScore1;
private int mScore2;
private TextView mScoreText1;
private TextView mScoreText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScoreText1 = (TextView)findViewById(R.id.tvscore1);
mScoreText2 = (TextView)findViewById(R.id.tvscore2);
}
public void decreaseScore(View view) {
//Get the ID of the button that was clicked
int viewID = view.getId();
switch (viewID){
//If it was on Team 1
case R.id.ib1min:
//Decrement the score and update the TextView
mScore1--;
mScoreText1.setText(String.valueOf(mScore1));
break;
//If it was Team 2
case R.id.ib2min:
//Decrement the score and update the TextView
mScore2--;
mScoreText2.setText(String.valueOf(mScore2));
}
}
public void increaseScore(View view) {
//Get the ID of the button that was clicked
int viewID = view.getId();
switch (viewID) {
//If it was on Team 1
case R.id.ib1plus:
//Increment the score and update the TextView
mScore1++;
mScoreText1.setText(String.valueOf(mScore1));
break;
//If it was Team 2
case R.id.ib2plus:
//Increment the score and update the TextView
mScore2++;
mScoreText2.setText(String.valueOf(mScore2));
}
}
Tugas 2: Membuat sumber daya Drawable
2.1 Buat Drawable Bentuk
2.2 Terapkan drawable bentuk sebagai latar belakang
Hasil :
Tugas 3: Tata gaya tampilan Anda
3.1 Buat gaya tombol
3.2 Buat gaya TextView
3.3 Memperbarui gaya
Hasil :
Tugas 4: Tema dan Sentuhan Akhir
4.1 Jelajahi tema
Hasil :
4.2 Tambahkan tombol tema ke menu
5.2: Desain Material Daftar, Kartu, dan Warna
Tugas 1: Mengunduh Kode Starter
1.1 Buka dan Jalankan Proyek Material Me
Tugas 2: Menambahkan CardView dan Gambar
2.1 Tambahkan CardView
2.2 Unduh gambar
2.3 Modifikasi objek Sport
2.4 Perbaiki metode initializeData()
2.5 Tambahkan ImageView ke item daftar
2.6 Muat gambar menggunakan Glide
Tugas 3: Membuat CardView Anda bisa digesek, dipindahkan, dan diklik
3.1 Implementasikan gesek untuk menutup
3.2 Implementasikan seret dan lepas
3.3 Implementasikan tampilan detail
Tugas 4: Tambahkan FAB dan pilih Palet Warna Material
4.1 Tambahkan FAB
4.2 Pilih Palet Material
Hasil :
5.3 Mendukung Lanskap, Beberapa Ukuran Layar, dan Pelokalan
4.1. Menggunakan Keyboard, Kontrol Masukan, Peringatan, dan Picker
Tugas 1: Bereksperimen dengan atribut keyboard entri teks
1.1 Membuat layout utama dan metode showText
Hasil :
1.2 Setel keyboard untuk menjadikan huruf besar di awal kalimat
Hasil :
1.3 Setel keyboard untuk menyembunyikan sandi saat memasukkannya
Hasil :
Tugas 2. Mengubah tipe keyboard
2.1 Menggunakan keyboard email
Hasil :
2.2 Menggunakan keypad ponsel
Hasil :
Tugas 3: Menambahkan kontrol masukan spinner untuk memilih label telepon
3.1 Salin proyek KeyboardSamples dan ubah layout
3.2 Tambahkan kode untuk mengaktifkan spinner dan listener-nya
3.3 Tambahkan kode untuk merespons pilihan pengguna
Hasil :
Tugas 4: Menggunakan dialog untuk peringatan yang memerlukan keputusan
4.1 Buat proyek baru dengan layout untuk menampilkan dialog peringatan
Hasil :
Tugas 5: Menggunakan picker untuk masukan pengguna
5.1 Membuat layout aktivitas utama
5.2 Buat fragmen baru untuk picker tanggal
5.3 Buat fragmen baru untuk picker waktu
Date Picker :package com.example.android.DateTimePickers;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
/**
* Creates the date picker dialog with the current date from Calendar.
* @param savedInstanceState Saved instance
* @return DatePickerDialog The date picker dialog
*/
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker.
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it.
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Convert the date elements to strings.
// Set the activity to the Main Activity.
MainActivity activity = (MainActivity) getActivity();
// Invoke Main Activity's processDatePickerResult() method.
activity.processDatePickerResult(year, month, day);
}
}
Main Activitypackage com.example.android.DateTimePickers;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
/**
* This app shows the time and date pickers when you click the appropriate button.
*/
public class MainActivity extends AppCompatActivity {
/**
* Creates the view based on the layout for the main activity.
* @param savedInstanceState Saved instance
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), getString(R.string.date_picker));
}
public void showTimePickerDialog(View view) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), getString(R.string.time_picker));
}
public void processDatePickerResult(int year, int month, int day) {
// The month integer returned by the date picker starts counting at 0
// for January, so you need to add 1 to show months starting at 1.
String month_string = Integer.toString(month + 1);
String day_string = Integer.toString(day);
String year_string = Integer.toString(year);
// Assign the concatenated strings to dateMessage.
String dateMessage = (month_string + "/" + day_string + "/" + year_string);
Toast.makeText(this, getString(R.string.date) + dateMessage, Toast.LENGTH_SHORT).show();
}
public void processTimePickerResult(int hourOfDay, int minute) {
// Convert time elements into strings.
String hour_string = Integer.toString(hourOfDay);
String minute_string = Integer.toString(minute);
// Assign the concatenated strings to timeMessage.
String timeMessage = (hour_string + ":" + minute_string);
Toast.makeText(this, getString(R.string.time) + timeMessage, Toast.LENGTH_SHORT).show();
}
} Time Picker :package com.example.android.DateTimePickers;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;
import java.util.Calendar;
/**
* A simple {@link Fragment} subclass for a time picker.
* Sets the current time for the picker using Calendar.
*/
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
/**
* Creates the time picker dialog with the current time from Calendar.
* @param savedInstanceState Saved instance
* @return TimePickerDialog The time picker dialog
*/
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker.
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it.
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
/**
* Grabs the time and converts it to a string to pass
* to the Main Activity in order to show it with processTimePickerResult().
* @param view The time picker view
* @param hourOfDay The hour chosen
* @param minute The minute chosen
*/
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Set the activity to the Main Activity.
MainActivity activity = (MainActivity) getActivity();
// Invoke Main Activity's processTimePickerResult() method.
activity.processTimePickerResult(hourOfDay, minute);
}
}
Hasil :
Tugas 6: Menggunakan tampilan gambar sebagai tombol
6.1 Mulai proyek baru
6.2 Tambahkan gambar
6.3 Tambahkan metode onClick untuk tampilan gambar
Tugas 7: Menggunakan tombol radio
7.1 Tambahkan aktivitas lain
7.2 Tambahkan layout untuk tombol radio
7.3 Tambahkan handler klik tombol radio
Hasil :
4.2: Menggunakan Menu Opsi
Tugas 1: Menambahkan item ke menu opsi opsi
1.1 Periksa kode bilah aplikasi
Hasil :
Hasil:
H
Hasil :
4.3: Menggunakan Bilah Aplikasi dan Tab untuk Navigasi
Tugas 1. Menambahkan tombol Naik untuk navigasi ancestral
Hasil :
Tugas 2. Menambahkan navigasi tab dengan tampilan gesek
/**
* JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed
*/ @RunWith(JUnit4.class)
@SmallTest public class CalculatorTest {
private Calculator mCalculator;
/**
* Set up the environment for testing
*/ @Before public void setUp() { mCalculator = new Calculator();
}
/**
* Test for simple addition
*/ @Test public void addTwoNumbers() { double resultAdd = mCalculator.add(1d, 1d); assertThat(resultAdd, is(equalTo(2d)));
}
}
1.2 Menjalankan pengujian dalam Android Studio
Tugas 2. Menambahkan lebih banyak pengujian unit ke CalculatorTest
2.1 Menambahkan lebih banyak pengujian untuk metode add()
2.2 Menambahkan pengujian unit untuk metode penghitungan lain
/*
* Copyright 2016, Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.SimpleCalc;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import android.test.suitebuilder.annotation.SmallTest;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.closeTo;
import static org.junit.Assert.assertThat;
/**
* JUnit4 unit tests for the calculator logic. These are local unit tests; no device needed
*/
@RunWith(JUnit4.class)
@SmallTest
public class CalculatorTest {
private Calculator mCalculator;
/**
* Set up the environment for testing
*/
@Before
public void setUp() {
mCalculator = new Calculator();
}
/**
* Test for simple addition
*/
@Test
public void addTwoNumbers() {
double resultAdd = mCalculator.add(1d, 1d);
assertThat(resultAdd, is(equalTo(2d)));
}
@Test
public void addTwoNumbersNegative() {
double resultAdd = mCalculator.add(-1d, 2d);
assertThat(resultAdd, is(equalTo(1d)));
}
@Test
public void addTwoNumbersFloats() {
double resultAdd = mCalculator.add(1.111f, 1.111d);
assertThat(resultAdd, is(closeTo(2.222, 0.01)));
}
@Test
public void subTwoNumbers() {
double resultSub = mCalculator.sub(1d, 1d);
assertThat(resultSub, is(equalTo(0d)));
}
@Test
public void subWorksWithNegativeResult() {
double resultSub = mCalculator.sub(1d, 17d);
assertThat(resultSub, is(equalTo(-16d)));
}
@Test
public void mulTwoNumbers() {
double resultMul = mCalculator.mul(32d, 2d);
assertThat(resultMul, is(equalTo(64d)));
}
@Test
public void divTwoNumbers() {
double resultDiv = mCalculator.div(32d,2d);
assertThat(resultDiv, is(equalTo(16d)));
}
@Test
public void divTwoNumbersZero() {
double resultDiv = mCalculator.div(32d,0);
assertThat(resultDiv, is(equalTo(Double.POSITIVE_INFINITY)));
}
}
3.3: Menggunakan Pustaka Dukungan Android
Tugas 1. Menyiapkan proyek
1.1 Memverifikasi bahwa Pustaka Dukungan Android tersedia
Recent Comments