今回はボタンを押すとカウントアップするという簡単なアプリを作成します。
はじめにプロジェクトの作成を行いますが、前回の記事を参考にして下さい。
レイアウトについて
まずは res/layoutディレクトリにあるactivity_main.xml を開いてください。
activity_main.xml にレイアウトを記述していきます。
「コード」で直接書く方法と「デザイン」で画面上に配置する方法があります。
右上の「コード」「デザイン」で切り替えができます。
TextView と Button を好きな位置に配置して下さい。

activity_main.xml
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:layout_editor_absoluteX="4dp" tools:layout_editor_absoluteY="-136dp"> <TextView android:id="@+id/count_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="56dp" android:text="Button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/count_text" /> </androidx.constraintlayout.widget.ConstraintLayout> |
TextView の idを変更しています。
idを指定することでどの TextViewか判別することができるようになります。
|
1 |
android:id="@+id/count_text" |
さらに初期値としてテキストに0を入れています。
|
1 |
android:text="0" |
カウントアップ処理について
MainActivity.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.example.apptest01; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView count_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // ボタンを設定 Button button = findViewById(R.id.button); // TextView の設定 count_view = findViewById(R.id.count_text); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String countView = new String((count_view.getText()).toString()); int mCount = Integer.parseInt(countView); mCount++; count_view.setText(String.valueOf(mCount)); } }); } } |
まずは findViewById() メソッドによって ButtonとTextView を取得しています。
ここで指定するidはactivity_main.xmlで指定したidです。
|
1 |
Button button = findViewById(R.id.button); |
|
1 |
count_view = findViewById(R.id.count_text); |
次に setOnClickListener() を使用することでボタンが押された事を受け取るようにしています。
|
1 2 3 4 5 6 |
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); |
count_view.getText()で画面上のテキストの値を取得しています。
|
1 |
String countView = new String((count_view.getText()).toString()); |
mCountの値を+1ずつするようにしています。
|
1 2 |
int mCount = Integer.parseInt(countView); mCount++; |
TextViewに値を反映しています。
|
1 |
count_view.setText(String.valueOf(mCount)); |
ここでビルドしてください。
ボタンを押すごとにカウントアップしていけば成功です。


