専門学校305日目/1月21日(水)
1時限目~3時限目
webプログラミング実習
もうすぐ提出と発表になりますので、各自それなりに動くものにするように指示がありました。
先週は、AIによるリファクタリング前の提出用コードをいじって、プロフィール画像を正常に差し替えられるようにしたり、データベースの再現を行えるようにエクスポートしたりとやりました。またLaTex辞書機能も追加しました。
本日は、プロフィール画像がコメント欄にも表示されるので、画像が使われるページすべてに反映されるように修正しました。
といっても、プロフィール用の画像は専用のフォルダに入れることにしたので、パスを変えるだけでした。
そして、コメント欄に表示される日時の色が見えにくくなっていたので、色を変えました。使用しているBootstrapの_utilities.scssの中で色が決まっていたようで、単純にCSSファイルで指定しても色は変わりませんでしたが、自前CSSの読み込みをBootstrapよりも後にして、!importantで優先度を上げたらうまく変更できました。Bootstrap内でも同じ方法で優先度を高く設定されていたので、それを上書きした感じですね。
.text-muted {
color: beige !important;
}
.text-muted {
--bs-text-opacity: 1;
color: var(--bs-secondary-color) !important;
}
この↑のコードは開発者ツールで見れるものですが、下がBootstrapで上が自前のCSSです。実際にはBootstrap側のには取り消し線が表示されています。
もう一つ細かな修正ですが、数学事典機能のボタンとLaTex辞書機能のボタンの配置がくっついていたのを間をあけました。LaTex辞書機能のボタンにクラスでms-2を追加したら左側にマージンが作れてそれで解決しました。これはBootstrapの機能みたいで、msでマージンスタート(左側)、meでマージンエンド(右側)だそうです。クラスを追加するだけでいろんなことができるのが魅力的ですね。
<button type="button" class="btn btn-outline-success btn-sm" data-bs-toggle="modal" data-bs-target="#encyclopediaModal">
<i class="bi bi-book"></i> 数学事典
</button>
<button type="button" class="btn btn-outline-info btn-sm ms-2" data-bs-toggle="modal" data-bs-target="#latexDictModal">
<i class="bi bi-alphabet"></i> LaTeX辞書
</button>最後に取り組んだのが検索機能の改善です。現在は記事のタイトルや日付で検索できるのですが、指定した日付以前以降や、2つの日付の間の記事を検索したり、記事にタグを設定してタグでも検索出来たりといった機能をつけようとしました。MAMPでも開発なので、MySQLですが、テーブルを2つ作成しました。
-- 1. タグ自体のマスターテーブル(タグの名前を管理)
CREATE TABLE tags (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE
);
-- 2. メモとタグを結びつける「中間テーブル」
-- ここで「どのメモ」に「どのタグ」がついているかを管理します
CREATE TABLE memo_tags (
memo_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (memo_id, tag_id),
-- メモが削除されたら、紐付けも自動で消える設定
FOREIGN KEY (memo_id) REFERENCES memos(id) ON DELETE CASCADE,
-- タグが削除されたら、紐付けも自動で消える設定
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;それから、記事の編集画面でタグを書き込めるようにし、記事の保存時にタグも保存するようにしました。
タグ関係のコードをAIに聞いているときに、登録したテンプレを削除する機能が実装されていないと言われて、それも追加しました。
本日できたのはここまでです。
4時限目~6時限目
Androidプログラミング実習
先週はListViewを使ったアプリの作成や、地図アプリの作成をしました。
本日は「くじ」アプリを作成しました。Intentで画面遷移をして遷移先に情報を渡して、それに応じて表示するアプリです。2画面なので、コード量はその分増えています。とはいえ、AndroidStudioで自動で用意されるソースコードがほとんどです。
[MainActivity.java]
package com.example.kujiapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btClick_a = findViewById(R.id.btA);
Button btClick_b = findViewById(R.id.btB);
Button btClick_c = findViewById(R.id.btC);
KujiListener listener = new KujiListener();
btClick_a.setOnClickListener(listener);
btClick_b.setOnClickListener(listener);
btClick_c.setOnClickListener(listener);
}
private class KujiListener implements View.OnClickListener {
@Override
public void onClick(View view) {
int bt_id = view.getId();
String result;
Intent intent = new Intent(MainActivity.this, Result.class);
if (bt_id == R.id.btA) {
Toast.makeText(MainActivity.this, "Aが押されました", Toast.LENGTH_LONG).show();
result = "0";
intent.putExtra("btInput", result);
startActivity(intent);
} else if (bt_id == R.id.btB) {
Toast.makeText(MainActivity.this, "Bが押されました", Toast.LENGTH_LONG).show();
result = "1";
intent.putExtra("btInput", result);
startActivity(intent);
} else if (bt_id == R.id.btC) {
Toast.makeText(MainActivity.this, "Cが押されました", Toast.LENGTH_LONG).show();
result = "2";
intent.putExtra("btInput", result);
startActivity(intent);
}
}
}
}[activity_main.xml]
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="くじアプリ"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="A"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="B"
app:layout_constraintEnd_toStartOf="@+id/btC"
app:layout_constraintStart_toEndOf="@+id/btA"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>[Result.java]
package com.example.kujiapplication;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class Result extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent = getIntent();
String receive = intent.getStringExtra("btInput");
TextView tvMessage = findViewById(R.id.tvResult);
if ("0".equals(receive)) {
tvMessage.setText("はずれ");
} else if ("1".equals(receive)) {
tvMessage.setText("あたり");
} else if ("2".equals(receive)) {
tvMessage.setText("はずれ");
}
}
}[activity_result.xml]
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Result">
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>以上になります。先週の授業の記事にも書いたと思いますが、build.gradleの中身も少しいじって、この更新したファイルと同期しないと動かない場合があります。自分の環境だと、
compileSdk 36に、minSdk 23に、targetSdk 36にしてうまくいきました。
plugins {
alias(libs.plugins.android.application)
}
android {
namespace 'com.example.kujiapplication'
compileSdk 36
defaultConfig {
applicationId "com.example.kujiapplication"
minSdk 23
targetSdk 36
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}
dependencies {
implementation libs.appcompat
implementation libs.material
implementation libs.activity
implementation libs.constraintlayout
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
}