画面を閉じる(アクティビティの終了)を学習します。
画面を閉じるは、最短1行の入力で可能です。
動画リスト
この動画はシリーズ物です。以下の順にご覧ください。
- 【 アクティビティ① 】 ~ アクティビティとは ~
- 【 アクティビティ② 】 ~ 画面の作成 ~
- 【 アクティビティ③ 】 ~ 画面の編集 ~
- 【 アクティビティ④ 】 ~ 画面の切替 ~
- 【 アクティビティ⑤ 】 ~ 画面を閉じる ~
ボタンの追加
「閉じるボタン」を追加します。
編集するXMLのファイル名にご注意ください。
activity_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88ffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.activity.Gallery">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ギャラリー"
android:textSize="30sp" />
<Button
android:onClick="onClose"
android:layout_centerInParent="true"
android:textSize="30sp"
android:text="閉じる"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
プログラムの追加
プログラム(動的な命令)を追加します。
編集するJavaのファイル名にご注意ください。
Gallery.java
package com.test.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Gallery extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
}
// クリック処理
public void onClose( View v){
finish(); // 画面を閉じる(アクティビティの終了)
}
}



