質問編集履歴
3
記述ミスがあったので修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
----------------メイン画面(時間割表の画面)の処理------------------------
|
20
20
|
|
21
|
-
`
|
21
|
+
```Kotlin
|
22
22
|
|
23
23
|
|
24
24
|
|
2
markdown記述を利用してコードを記述しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,6 +18,10 @@
|
|
18
18
|
|
19
19
|
----------------メイン画面(時間割表の画面)の処理------------------------
|
20
20
|
|
21
|
+
`,`Kotlin
|
22
|
+
|
23
|
+
|
24
|
+
|
21
25
|
package com.example.timeschedule
|
22
26
|
|
23
27
|
|
@@ -122,164 +126,172 @@
|
|
122
126
|
|
123
127
|
}
|
124
128
|
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
----------------科目登録の画面側の処理----------------------------------
|
134
|
+
|
135
|
+
```Kotlin
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
package com.example.timeschedule
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
import android.app.Activity
|
144
|
+
|
145
|
+
import android.content.Intent
|
146
|
+
|
147
|
+
import androidx.appcompat.app.AppCompatActivity
|
148
|
+
|
149
|
+
import android.os.Bundle
|
150
|
+
|
151
|
+
import androidx.core.content.edit
|
152
|
+
|
153
|
+
import androidx.preference.PreferenceManager
|
154
|
+
|
155
|
+
import kotlinx.android.synthetic.main.activity_add_subject.*
|
156
|
+
|
157
|
+
import java.io.Serializable
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
class addSubject : AppCompatActivity() {
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
168
|
+
|
169
|
+
super.onCreate(savedInstanceState)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
setContentView(R.layout.activity_add_subject)
|
174
|
+
|
175
|
+
backButton.setOnClickListener {
|
176
|
+
|
177
|
+
finish()
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
addButton.setOnClickListener {
|
184
|
+
|
185
|
+
val intent = Intent()
|
186
|
+
|
187
|
+
intent.putExtra("TEXT_KEY",subjectName.text.toString())//戻す情報をintentにセット
|
188
|
+
|
189
|
+
setResult(Activity.RESULT_OK,intent)//結果のコードとibtentをセット
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
val lesson = LessonData(this)//登録ボタンを押した際、内容をLessonDataに書き込む
|
194
|
+
|
195
|
+
lesson.name = subjectName.text.toString()
|
196
|
+
|
197
|
+
lesson.teacher = responsibleTeacher.text.toString()
|
198
|
+
|
199
|
+
lesson.room = lectureRoom.text.toString()
|
200
|
+
|
201
|
+
finish()
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
override fun onStart() {
|
214
|
+
|
215
|
+
super.onStart()
|
216
|
+
|
217
|
+
val lesson = LessonData(this)//前回保存したデータを読み込む
|
218
|
+
|
219
|
+
val name = lesson.name
|
220
|
+
|
221
|
+
val teacher = lesson.teacher
|
222
|
+
|
223
|
+
val room = lesson.room
|
224
|
+
|
225
|
+
subjectName.setText(name)
|
226
|
+
|
227
|
+
lectureRoom.setText(room)
|
228
|
+
|
229
|
+
responsibleTeacher.setText(teacher)
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
```
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
----------------科目登録の画面用のデータセット--------------------------
|
244
|
+
|
245
|
+
```Kotlin
|
246
|
+
|
247
|
+
package com.example.timeschedule
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
import android.content.Context
|
252
|
+
|
253
|
+
import androidx.preference.PreferenceManager
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
class LessonData(context: Context) {
|
258
|
+
|
259
|
+
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
var name: String?//科目名
|
264
|
+
|
265
|
+
get() = prefs.getString("NAME", null)
|
266
|
+
|
267
|
+
set(value) = prefs.edit().putString("NAME", value).apply()
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
var teacher: String?//教師名
|
272
|
+
|
273
|
+
get() = prefs.getString("TEACHER", null)
|
274
|
+
|
275
|
+
set(value) = prefs.edit().putString("TEACHER", value).apply()
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
var room: String?//部屋番号
|
280
|
+
|
281
|
+
get() = prefs.getString("ROOM", null)
|
282
|
+
|
283
|
+
set(value) = prefs.edit().putString("ROOM", value).apply()
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
```
|
290
|
+
|
291
|
+
|
292
|
+
|
125
293
|
-----------------------------------------------------------------------
|
126
294
|
|
127
|
-
|
128
|
-
|
129
|
-
----------------科目登録の画面側の処理----------------------------------
|
130
|
-
|
131
|
-
package com.example.timeschedule
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
import android.app.Activity
|
136
|
-
|
137
|
-
import android.content.Intent
|
138
|
-
|
139
|
-
import androidx.appcompat.app.AppCompatActivity
|
140
|
-
|
141
|
-
import android.os.Bundle
|
142
|
-
|
143
|
-
import androidx.core.content.edit
|
144
|
-
|
145
|
-
import androidx.preference.PreferenceManager
|
146
|
-
|
147
|
-
import kotlinx.android.synthetic.main.activity_add_subject.*
|
148
|
-
|
149
|
-
import java.io.Serializable
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
class addSubject : AppCompatActivity() {
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
override fun onCreate(savedInstanceState: Bundle?) {
|
160
|
-
|
161
|
-
super.onCreate(savedInstanceState)
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
setContentView(R.layout.activity_add_subject)
|
166
|
-
|
167
|
-
backButton.setOnClickListener {
|
168
|
-
|
169
|
-
finish()
|
170
|
-
|
171
|
-
}
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
addButton.setOnClickListener {
|
176
|
-
|
177
|
-
val intent = Intent()
|
178
|
-
|
179
|
-
intent.putExtra("TEXT_KEY",subjectName.text.toString())//戻す情報をintentにセット
|
180
|
-
|
181
|
-
setResult(Activity.RESULT_OK,intent)//結果のコードとibtentをセット
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
val lesson = LessonData(this)//登録ボタンを押した際、内容をLessonDataに書き込む
|
186
|
-
|
187
|
-
lesson.name = subjectName.text.toString()
|
188
|
-
|
189
|
-
lesson.teacher = responsibleTeacher.text.toString()
|
190
|
-
|
191
|
-
lesson.room = lectureRoom.text.toString()
|
192
|
-
|
193
|
-
finish()
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
override fun onStart() {
|
206
|
-
|
207
|
-
super.onStart()
|
208
|
-
|
209
|
-
val lesson = LessonData(this)//前回保存したデータを読み込む
|
210
|
-
|
211
|
-
val name = lesson.name
|
212
|
-
|
213
|
-
val teacher = lesson.teacher
|
214
|
-
|
215
|
-
val room = lesson.room
|
216
|
-
|
217
|
-
subjectName.setText(name)
|
218
|
-
|
219
|
-
lectureRoom.setText(room)
|
220
|
-
|
221
|
-
responsibleTeacher.setText(teacher)
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
}
|
230
|
-
|
231
|
-
-----------------------------------------------------------------------
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
----------------科目登録の画面用のデータセット--------------------------
|
238
|
-
|
239
|
-
package com.example.timeschedule
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
import android.content.Context
|
244
|
-
|
245
|
-
import androidx.preference.PreferenceManager
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
class LessonData(context: Context) {
|
250
|
-
|
251
|
-
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
var name: String?//科目名
|
256
|
-
|
257
|
-
get() = prefs.getString("NAME", null)
|
258
|
-
|
259
|
-
set(value) = prefs.edit().putString("NAME", value).apply()
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
var teacher: String?//教師名
|
264
|
-
|
265
|
-
get() = prefs.getString("TEACHER", null)
|
266
|
-
|
267
|
-
set(value) = prefs.edit().putString("TEACHER", value).apply()
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
var room: String?//部屋番号
|
272
|
-
|
273
|
-
get() = prefs.getString("ROOM", null)
|
274
|
-
|
275
|
-
set(value) = prefs.edit().putString("ROOM", value).apply()
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
-----------------------------------------------------------------------
|
282
|
-
|
283
295
|
![イメージ説明](a3cd7f80c4f760a5049c57d9619d9d2c.png)
|
284
296
|
|
285
297
|
//時間割表の画面
|
1
コードの部分を直接書き込みました
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,21 +14,271 @@
|
|
14
14
|
|
15
15
|
1つボタンを作って試した際はうまくいったのですが、2つ目のボタン(buttonと書かれたボタン)を配置して試したところ、1つ目のボタンの内容が変更されてしまいました。このまま続けると、20個配置してもすべて1つ目のボタンが変更されてしまうと思い、今回こちらの掲示板に質問することに決めました。
|
16
16
|
|
17
|
-
まずは、現状を知ってもらうために画像を貼っておきます。
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
17
|
+
まずは、現状を知ってもらうために各コードとレイアウトの画像を貼っておきます。
|
18
|
+
|
19
|
+
----------------メイン画面(時間割表の画面)の処理------------------------
|
20
|
+
|
21
|
+
package com.example.timeschedule
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
import android.app.Activity
|
26
|
+
|
27
|
+
import android.content.Intent
|
28
|
+
|
29
|
+
import androidx.appcompat.app.AppCompatActivity
|
30
|
+
|
31
|
+
import android.os.Bundle
|
32
|
+
|
33
|
+
import android.view.View
|
34
|
+
|
35
|
+
import kotlinx.android.synthetic.main.activity_main.*
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
class MainActivity : AppCompatActivity() {
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
companion object{
|
44
|
+
|
45
|
+
const val REQUEST_CODE = 1000//REQUEST_CODEの値はいくつでもよい。
|
46
|
+
|
47
|
+
// onActivityResult()に戻ってきたときに、どのActivityから戻ってきたのかを判別する。
|
48
|
+
|
49
|
+
// 今回のように遷移先が1つしかない場合、値は何でもよい。
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
super.onCreate(savedInstanceState)
|
62
|
+
|
63
|
+
setContentView(R.layout.activity_main)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
mon_12button.setOnClickListener {
|
68
|
+
|
69
|
+
val intent = Intent(this, addSubject::class.java)
|
70
|
+
|
71
|
+
startActivityForResult(intent, REQUEST_CODE)
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
mon_12button2.setOnClickListener {
|
78
|
+
|
79
|
+
intent = Intent(this,addSubject::class.java)
|
80
|
+
|
81
|
+
startActivityForResult(intent, REQUEST_CODE)
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
override fun onStart() {
|
92
|
+
|
93
|
+
super.onStart()
|
94
|
+
|
95
|
+
val lesson = LessonData(this)//mainで読み込めばアプリを閉じて、再び開いても保存されたまま?
|
96
|
+
|
97
|
+
val name = lesson.name
|
98
|
+
|
99
|
+
mon_12button.text = name
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
//結果を受け取るメソッド
|
106
|
+
|
107
|
+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
108
|
+
|
109
|
+
super.onActivityResult(requestCode, resultCode, data)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if(requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
|
114
|
+
|
115
|
+
//RESURT_OKで帰ってきたとき、ボタンのテキストを更新
|
116
|
+
|
117
|
+
mon_12button.text = data?.getStringExtra("TEXT_KEY")
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
-----------------------------------------------------------------------
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
----------------科目登録の画面側の処理----------------------------------
|
130
|
+
|
131
|
+
package com.example.timeschedule
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
import android.app.Activity
|
136
|
+
|
137
|
+
import android.content.Intent
|
138
|
+
|
139
|
+
import androidx.appcompat.app.AppCompatActivity
|
140
|
+
|
141
|
+
import android.os.Bundle
|
142
|
+
|
143
|
+
import androidx.core.content.edit
|
144
|
+
|
145
|
+
import androidx.preference.PreferenceManager
|
146
|
+
|
147
|
+
import kotlinx.android.synthetic.main.activity_add_subject.*
|
148
|
+
|
149
|
+
import java.io.Serializable
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
class addSubject : AppCompatActivity() {
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
160
|
+
|
161
|
+
super.onCreate(savedInstanceState)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
setContentView(R.layout.activity_add_subject)
|
166
|
+
|
167
|
+
backButton.setOnClickListener {
|
168
|
+
|
169
|
+
finish()
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
addButton.setOnClickListener {
|
176
|
+
|
177
|
+
val intent = Intent()
|
178
|
+
|
179
|
+
intent.putExtra("TEXT_KEY",subjectName.text.toString())//戻す情報をintentにセット
|
180
|
+
|
181
|
+
setResult(Activity.RESULT_OK,intent)//結果のコードとibtentをセット
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
val lesson = LessonData(this)//登録ボタンを押した際、内容をLessonDataに書き込む
|
186
|
+
|
187
|
+
lesson.name = subjectName.text.toString()
|
188
|
+
|
189
|
+
lesson.teacher = responsibleTeacher.text.toString()
|
190
|
+
|
191
|
+
lesson.room = lectureRoom.text.toString()
|
192
|
+
|
193
|
+
finish()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
override fun onStart() {
|
206
|
+
|
207
|
+
super.onStart()
|
208
|
+
|
209
|
+
val lesson = LessonData(this)//前回保存したデータを読み込む
|
210
|
+
|
211
|
+
val name = lesson.name
|
212
|
+
|
213
|
+
val teacher = lesson.teacher
|
214
|
+
|
215
|
+
val room = lesson.room
|
216
|
+
|
217
|
+
subjectName.setText(name)
|
218
|
+
|
219
|
+
lectureRoom.setText(room)
|
220
|
+
|
221
|
+
responsibleTeacher.setText(teacher)
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
-----------------------------------------------------------------------
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
----------------科目登録の画面用のデータセット--------------------------
|
238
|
+
|
239
|
+
package com.example.timeschedule
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
import android.content.Context
|
244
|
+
|
245
|
+
import androidx.preference.PreferenceManager
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
class LessonData(context: Context) {
|
250
|
+
|
251
|
+
private val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
var name: String?//科目名
|
256
|
+
|
257
|
+
get() = prefs.getString("NAME", null)
|
258
|
+
|
259
|
+
set(value) = prefs.edit().putString("NAME", value).apply()
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
var teacher: String?//教師名
|
264
|
+
|
265
|
+
get() = prefs.getString("TEACHER", null)
|
266
|
+
|
267
|
+
set(value) = prefs.edit().putString("TEACHER", value).apply()
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
var room: String?//部屋番号
|
272
|
+
|
273
|
+
get() = prefs.getString("ROOM", null)
|
274
|
+
|
275
|
+
set(value) = prefs.edit().putString("ROOM", value).apply()
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
-----------------------------------------------------------------------
|
32
282
|
|
33
283
|
![イメージ説明](a3cd7f80c4f760a5049c57d9619d9d2c.png)
|
34
284
|
|