回答編集履歴

9

スコープとキャスト修正

2020/03/24 09:38

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  //ドラムロールの表示と同じ順にボタンの id を並べた配列を作る
74
74
 
75
- final int[] buttons={
75
+ private static final int[] buttons={
76
76
 
77
77
  R.id.btn月1,R.id.btn月2,R.id.btn月3,R.id.btn月4,R.id.btn月5,R.id.btn月6,
78
78
 
@@ -124,7 +124,7 @@
124
124
 
125
125
  int numPicker=data.getIntExtra("numPicker", -1); //0-35
126
126
 
127
- Button button=(Button)findViewById(buttons[numPicker]);
127
+ Button button=findViewById(buttons[numPicker]);
128
128
 
129
129
  button.setText(txtName);
130
130
 

8

コードやり直し

2020/03/24 09:38

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -44,440 +44,440 @@
44
44
 
45
45
 
46
46
 
47
- #追加
47
+ #コードやり直し
48
+
49
+
50
+
48
-
51
+ MainActivity.java
49
-
50
52
 
51
53
  ```java
52
54
 
55
+ package to.msn.wings.ourapp;
56
+
57
+
58
+
59
+ import androidx.appcompat.app.AppCompatActivity;
60
+
53
- import android.os.*;
61
+ import android.os.Bundle;
62
+
54
-
63
+ import android.content.Intent;
64
+
55
-
65
+ import android.view.View;
66
+
56
-
67
+ import android.widget.Button;
68
+
69
+
70
+
57
- class Lecture implements Parcelable {
71
+ public class MainActivity extends AppCompatActivity {
58
-
72
+
59
- static final String INTENT_NAME = "Lecture";
73
+ //ドラムロールの表示と同じ順にボタンの id を並べた配列を作る
60
-
61
- final String name;
74
+
62
-
63
- final String room;
75
+ final int[] buttons={
76
+
64
-
77
+ R.id.btn月1,R.id.btn月2,R.id.btn月3,R.id.btn月4,R.id.btn月5,R.id.btn月6,
78
+
79
+ R.id.btn火1,R.id.btn火2,R.id.btn火3,R.id.btn火4,R.id.btn火5,R.id.btn火6,
80
+
81
+ R.id.btn水1,R.id.btn水2,R.id.btn水3,R.id.btn水4,R.id.btn水5,R.id.btn水6,
82
+
83
+ R.id.btn木1,R.id.btn木2,R.id.btn木3,R.id.btn木4,R.id.btn木5,R.id.btn木6,
84
+
85
+ R.id.btn金1,R.id.btn金2,R.id.btn金3,R.id.btn金4,R.id.btn金5,R.id.btn金6,
86
+
87
+ R.id.btn土1,R.id.btn土2,R.id.btn土3,R.id.btn土4,R.id.btn土5,R.id.btn土6
88
+
89
+ };
90
+
91
+ @Override
92
+
65
- final int dayOfWeek; //0-5
93
+ protected void onCreate(Bundle savedInstanceState) {
66
-
67
- final int time; //0-5
94
+
68
-
69
- Lecture(int dayOfWeek, int time) {
95
+ super.onCreate(savedInstanceState);
70
-
96
+
71
- this("","", dayOfWeek, time);
97
+ setContentView(R.layout.activity_main);
72
98
 
73
99
  }
74
100
 
101
+ public void btn2_onClick(View view){
102
+
75
- Lecture(String name, String room, int dayOfWeek, int time) {
103
+ Intent intent=new Intent(this, SubActivity2.class);
76
-
104
+
77
- this.name = name;
105
+ //アクティビティを起動(サブ画面起動)
78
-
79
- this.room = room;
106
+
80
-
81
- this.dayOfWeek = dayOfWeek;
107
+ startActivityForResult(intent,0);
82
-
83
- this.time = time;
84
108
 
85
109
  }
86
110
 
87
111
  @Override
88
112
 
113
+ protected void onActivityResult(int requestCode,int resultCode,Intent data){
114
+
115
+ super.onActivityResult(requestCode, resultCode, data);
116
+
117
+ //リクエストコードと結果コードをチェック
118
+
119
+ if(requestCode==0 && resultCode==RESULT_OK){
120
+
121
+ //結果のコードを取得&ボタンのテクストに記入
122
+
123
+ String txtName=data.getStringExtra("txtName");
124
+
125
+ int numPicker=data.getIntExtra("numPicker", -1); //0-35
126
+
127
+ Button button=(Button)findViewById(buttons[numPicker]);
128
+
89
- public int describeContents() {
129
+ button.setText(txtName);
90
-
130
+
91
- return 0;
131
+ }
92
132
 
93
133
  }
94
134
 
135
+ }
136
+
137
+ ```
138
+
139
+ activity_main.xml
140
+
141
+ ```xml
142
+
143
+ <?xml version="1.0" encoding="utf-8"?>
144
+
145
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
146
+
147
+ xmlns:app="http://schemas.android.com/apk/res-auto"
148
+
149
+ android:layout_width="match_parent"
150
+
151
+ android:layout_height="match_parent"
152
+
153
+ android:orientation="vertical">
154
+
155
+
156
+
157
+ <androidx.constraintlayout.widget.ConstraintLayout
158
+
159
+ android:layout_width="match_parent"
160
+
161
+ android:layout_height="wrap_content">
162
+
163
+
164
+
165
+ <Button
166
+
167
+ android:id="@+id/btn2"
168
+
169
+ android:layout_width="wrap_content"
170
+
171
+ android:layout_height="wrap_content"
172
+
173
+ android:layout_gravity="end"
174
+
175
+ android:layout_marginTop="8dp"
176
+
177
+ android:layout_marginEnd="8dp"
178
+
179
+ android:text="時間割選択"
180
+
181
+ app:layout_constraintEnd_toEndOf="parent"
182
+
183
+ app:layout_constraintTop_toTopOf="parent"
184
+
185
+ android:onClick="btn2_onClick"/>
186
+
187
+
188
+
189
+ <Button
190
+
191
+ android:id="@+id/btn1"
192
+
193
+ android:layout_width="wrap_content"
194
+
195
+ android:layout_height="wrap_content"
196
+
197
+ android:layout_gravity="top"
198
+
199
+ android:layout_marginStart="8dp"
200
+
201
+ android:layout_marginTop="8dp"
202
+
203
+ android:text="写真選択"
204
+
205
+ app:layout_constraintStart_toStartOf="parent"
206
+
207
+ app:layout_constraintTop_toTopOf="parent" />
208
+
209
+ </androidx.constraintlayout.widget.ConstraintLayout>
210
+
211
+
212
+
213
+ <LinearLayout
214
+
215
+ android:layout_width="match_parent"
216
+
217
+ android:layout_height="50dp"
218
+
219
+ android:orientation="vertical">
220
+
221
+ </LinearLayout>
222
+
223
+
224
+
225
+ <TableLayout
226
+
227
+ android:layout_width="match_parent"
228
+
229
+ android:layout_height="match_parent"
230
+
231
+ android:orientation="vertical"
232
+
233
+ android:shrinkColumns="0,1,2,3,4,5,6">
234
+
235
+
236
+
237
+ <TableRow
238
+
239
+ android:layout_width="match_parent"
240
+
241
+ android:layout_height="match_parent">
242
+
243
+ <Button
244
+
245
+ android:layout_width="wrap_content"
246
+
247
+ android:layout_height="wrap_content"/>
248
+
249
+ <Button
250
+
251
+ android:layout_width="wrap_content"
252
+
253
+ android:layout_height="wrap_content"
254
+
255
+ android:text="月"/>
256
+
257
+ <Button
258
+
259
+ android:layout_width="wrap_content"
260
+
261
+ android:layout_height="wrap_content"
262
+
263
+ android:text="火"/>
264
+
265
+ <Button
266
+
267
+ android:layout_width="wrap_content"
268
+
269
+ android:layout_height="wrap_content"
270
+
271
+ android:text="水"/>
272
+
273
+ <Button
274
+
275
+ android:layout_width="wrap_content"
276
+
277
+ android:layout_height="wrap_content"
278
+
279
+ android:text="木"/>
280
+
281
+ <Button
282
+
283
+ android:layout_width="wrap_content"
284
+
285
+ android:layout_height="wrap_content"
286
+
287
+ android:text="金"/>
288
+
289
+ <Button
290
+
291
+ android:layout_width="wrap_content"
292
+
293
+ android:layout_height="wrap_content"
294
+
295
+ android:text="土"/>
296
+
297
+ </TableRow>
298
+
299
+
300
+
301
+ <TableRow
302
+
303
+ android:layout_width="match_parent"
304
+
305
+ android:layout_height="match_parent">
306
+
307
+ <Button
308
+
309
+ android:layout_width="wrap_content"
310
+
311
+ android:layout_height="wrap_content"
312
+
313
+ android:text="1" />
314
+
315
+ <Button
316
+
317
+ android:id="@+id/btn月1"
318
+
319
+ android:layout_width="wrap_content"
320
+
321
+ android:layout_height="wrap_content" />
322
+
323
+ <Button
324
+
325
+ android:id="@+id/btn火1"
326
+
327
+ android:layout_width="wrap_content"
328
+
329
+ android:layout_height="wrap_content" />
330
+
331
+ <Button
332
+
333
+ android:id="@+id/btn水1"
334
+
335
+ android:layout_width="wrap_content"
336
+
337
+ android:layout_height="wrap_content" />
338
+
339
+ <Button
340
+
341
+ android:id="@+id/btn木1"
342
+
343
+ android:layout_width="wrap_content"
344
+
345
+ android:layout_height="wrap_content" />
346
+
347
+ <Button
348
+
349
+ android:id="@+id/btn金1"
350
+
351
+ android:layout_width="wrap_content"
352
+
353
+ android:layout_height="wrap_content" />
354
+
355
+ <Button
356
+
357
+ android:id="@+id/btn土1"
358
+
359
+ android:layout_width="wrap_content"
360
+
361
+ android:layout_height="wrap_content" />
362
+
363
+ </TableRow>
364
+
365
+
366
+
367
+ (以下5行, 時間ボタンの text と 曜日ボタンの id を変える他は同様)
368
+
369
+
370
+
371
+ </TableLayout>
372
+
373
+ </LinearLayout>
374
+
375
+ ```
376
+
377
+ SubActivity2.java
378
+
379
+ ```java
380
+
381
+ package to.msn.wings.ourapp;
382
+
383
+
384
+
385
+ import android.content.Intent;
386
+
387
+ import android.os.Bundle;
388
+
389
+ import androidx.appcompat.app.AppCompatActivity;
390
+
391
+ import android.view.View;
392
+
393
+ import android.widget.EditText;
394
+
395
+ import android.widget.NumberPicker;
396
+
397
+
398
+
399
+ public class SubActivity2 extends AppCompatActivity{
400
+
401
+ //ドラムロールに表示したい値を含んだ配列を作る
402
+
403
+ private static final String[] pref={
404
+
405
+ "月1","月2","月3","月4","月5","月6",
406
+
407
+ "火1","火2","火3","火4","火5","火6",
408
+
409
+ "水1","水2","水3","水4","水5","水6",
410
+
411
+ "木1","木2","木3","木4","木5","木6",
412
+
413
+ "金1","金2","金3","金4","金5","金6",
414
+
415
+ "土1","土2","土3","土4","土5","土6"
416
+
417
+ };
418
+
95
419
  @Override
96
420
 
97
- public void writeToParcel(Parcel dest, int flags) {
421
+ protected void onCreate(Bundle savedInstanceState){
422
+
98
-
423
+ super.onCreate(savedInstanceState);
424
+
425
+ setContentView(R.layout.activity_sub2);
426
+
427
+
428
+
429
+ //numberPickerをインスタンス化する
430
+
431
+ final NumberPicker picker=findViewById(R.id.numPicker0);
432
+
433
+ //配列のインデックスの最小、最大を指定する
434
+
99
- dest.writeString(name);
435
+ picker.setMinValue(0);
100
-
436
+
101
- dest.writeString(room);
437
+ picker.setMaxValue(pref.length-1);
438
+
102
-
439
+ //numberpickerに配列をセットする
440
+
103
- dest.writeInt(dayOfWeek);
441
+ picker.setDisplayedValues(pref);
104
-
105
- dest.writeInt(time);
106
442
 
107
443
  }
108
444
 
109
- public static final Creator<Lecture> CREATOR = new Creator<Lecture>() {
110
-
111
- public Lecture createFromParcel(Parcel source) {
112
-
113
- return new Lecture(source);
114
-
115
- }
116
-
117
- public Lecture[] newArray(int size) {
118
-
119
- return new Lecture[size];
120
-
121
- }
122
-
123
- };
124
-
125
- //Creator<Lecture> 用
445
+ //ボタンクリック時に呼び出されるメソッド
126
-
446
+
127
- private Lecture(Parcel source) {
447
+ public void btn88_onClick(View v){
128
-
129
- name = source.readString();
448
+
130
-
131
- room = source.readString();
132
-
133
- dayOfWeek = source.readInt();
449
+ //インテントを生成&データをセット
134
-
450
+
135
- time = source.readInt();
451
+ Intent i=new Intent();
452
+
453
+ //講義名
454
+
455
+ EditText txtName=findViewById(R.id.txtName);
456
+
457
+ i.putExtra("txtName",txtName.getText().toString());
458
+
459
+ //教室
460
+
461
+ EditText txtName2=findViewById(R.id.txtName2);
462
+
463
+ i.putExtra("txtName2",txtName2.getText().toString());
464
+
465
+ //numberPicker
466
+
467
+ NumberPicker numPicker=findViewById(R.id.numPicker0);
468
+
469
+ i.putExtra("numPicker",numPicker.getValue()); //0-35
470
+
471
+ //結果情報をセット
472
+
473
+ setResult(RESULT_OK,i);
474
+
475
+ //現在のアクティビティを終了
476
+
477
+ finish();
136
478
 
137
479
  }
138
480
 
139
481
  }
140
482
 
141
483
  ```
142
-
143
- ```java
144
-
145
- import androidx.appcompat.app.AppCompatActivity;
146
-
147
- import android.app.Activity;
148
-
149
- import android.content.Intent;
150
-
151
- import android.os.Bundle;
152
-
153
- import android.view.View;
154
-
155
- import android.widget.*;
156
-
157
-
158
-
159
- public class MainActivity extends AppCompatActivity {
160
-
161
- private static final String[] WEEKDAY_HEADER = {"月","火","水","木","金","土"};
162
-
163
- private static final int TIME_COUNT = 6; //一日の最大講義数
164
-
165
- private Button[][] lectureSchedule;
166
-
167
-
168
-
169
- @Override
170
-
171
- protected void onCreate(Bundle savedInstanceState) {
172
-
173
- super.onCreate(savedInstanceState);
174
-
175
- setContentView(R.layout.activity_main);
176
-
177
-
178
-
179
- Button scheduleSelectButton = findViewById(R.id.scheduleSelectButton);
180
-
181
- scheduleSelectButton.setOnClickListener(new View.OnClickListener() {
182
-
183
- @Override
184
-
185
- public void onClick(View v) {
186
-
187
- Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKDAY_HEADER, TIME_COUNT);
188
-
189
- startActivityForResult(intent, 0);
190
-
191
- }
192
-
193
- });
194
-
195
-
196
-
197
- lectureSchedule = initScheduleGrid(WEEKDAY_HEADER, TIME_COUNT);
198
-
199
- }
200
-
201
- private Button[][] initScheduleGrid(String[] columnLabels, int rowCount) {
202
-
203
- lectureSchedule = new Button[rowCount][columnLabels.length];
204
-
205
-
206
-
207
- GridLayout grid = findViewById(R.id.grid);
208
-
209
- grid.setColumnCount(1+columnLabels.length);
210
-
211
- grid.setRowCount(1+rowCount);
212
-
213
-
214
-
215
- //一行目(曜日)
216
-
217
- Space space = new Space(this); //左上角
218
-
219
- grid.addView(space, createLayoutParamsOfTimeLabel());
220
-
221
- for(int j=0; j<columnLabels.length; j++) {
222
-
223
- TextView textView = new TextView(this);
224
-
225
- textView.setText(columnLabels[j]);
226
-
227
- textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
228
-
229
- grid.addView(textView, createLayoutParamsOfWeekday());
230
-
231
- }
232
-
233
- //二行目以降(時間割)
234
-
235
- for(int i=0; i<rowCount; i++) {
236
-
237
- TextView timeLabel = new TextView(this);
238
-
239
- timeLabel.setText(""+(i+1));
240
-
241
- grid.addView(timeLabel, createLayoutParamsOfTimeLabel());
242
-
243
- for(int j=0; j<columnLabels.length; j++) {
244
-
245
- Button button = new Button(this);
246
-
247
- button.setText("");
248
-
249
- button.setTag(new Lecture(j, i));
250
-
251
- //button.setOnClickListener(clickListener);
252
-
253
- grid.addView(button, createLayoutParamsOfWeekday());
254
-
255
- lectureSchedule[i][j] = button;
256
-
257
- }
258
-
259
- }
260
-
261
- return lectureSchedule;
262
-
263
- }
264
-
265
- private GridLayout.LayoutParams createLayoutParamsOfTimeLabel() {
266
-
267
- return createLayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, 0, GridLayout.CENTER);
268
-
269
- }
270
-
271
- private GridLayout.LayoutParams createLayoutParamsOfWeekday() {
272
-
273
- return createLayoutParams( 0, 1, GridLayout.FILL);
274
-
275
- }
276
-
277
- private GridLayout.LayoutParams createLayoutParams(int width, int columnSpecWeight, GridLayout.Alignment rowSpecAlignment) {
278
-
279
- GridLayout.LayoutParams params = new GridLayout.LayoutParams();
280
-
281
- params.width = width;
282
-
283
- params.height = GridLayout.LayoutParams.WRAP_CONTENT;
284
-
285
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, columnSpecWeight);
286
-
287
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, rowSpecAlignment, 1);
288
-
289
- return params;
290
-
291
- }
292
-
293
- @Override
294
-
295
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
296
-
297
- super.onActivityResult(requestCode, resultCode, data);
298
-
299
- //リクエストコードと結果コードをチェック
300
-
301
- if(requestCode == 0 && resultCode == Activity.RESULT_OK){
302
-
303
- Lecture lecture = data.getParcelableExtra(Lecture.INTENT_NAME);
304
-
305
- Button button = lectureSchedule[lecture.time][lecture.dayOfWeek];
306
-
307
- button.setText(lecture.name);
308
-
309
- button.setTag(lecture);
310
-
311
- }
312
-
313
- }
314
-
315
- }
316
-
317
- ```
318
-
319
- ```java
320
-
321
- import androidx.appcompat.app.AppCompatActivity;
322
-
323
- import android.app.Activity;
324
-
325
- import android.content.*;
326
-
327
- import android.os.Bundle;
328
-
329
- import android.text.*;
330
-
331
- import android.view.View;
332
-
333
- import android.widget.*;
334
-
335
-
336
-
337
- public class SubActivity2 extends AppCompatActivity {
338
-
339
- public static final String INTENT_EXTRA_WEEKDAY = "Weekday";
340
-
341
- public static final String INTENT_EXTRA_TIMECOUNT = "TimeCount";
342
-
343
- private String[] weekday;
344
-
345
-
346
-
347
- @Override
348
-
349
- protected void onCreate(Bundle savedInstanceState){
350
-
351
- super.onCreate(savedInstanceState);
352
-
353
- setContentView(R.layout.activity_sub2);
354
-
355
-
356
-
357
- final EditText lectureName = findViewById(R.id.lectureName);
358
-
359
- final EditText lectureRoom = findViewById(R.id.lectureRoom);
360
-
361
- final NumberPicker timePicker = findViewById(R.id.timePicker);
362
-
363
- final Button okButton = findViewById(R.id.okButton);
364
-
365
-
366
-
367
- // name と room の入力状態によって ok の有効/無効を設定
368
-
369
- TextWatcher textWatcher = new TextWatcher() {
370
-
371
- @Override
372
-
373
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
374
-
375
- @Override
376
-
377
- public void onTextChanged(CharSequence s, int start, int before, int count) {
378
-
379
- okButton.setEnabled(lectureName.length() > 0 && lectureRoom.length() > 0);
380
-
381
- }
382
-
383
- @Override
384
-
385
- public void afterTextChanged(Editable s) {}
386
-
387
- };
388
-
389
-
390
-
391
- lectureName.addTextChangedListener(textWatcher);
392
-
393
- lectureRoom.addTextChangedListener(textWatcher);
394
-
395
-
396
-
397
- okButton.setEnabled(false);
398
-
399
- okButton.setOnClickListener(new View.OnClickListener() {
400
-
401
- @Override
402
-
403
- public void onClick(View v) {
404
-
405
- String name = lectureName.getText().toString();
406
-
407
- String room = lectureRoom.getText().toString();
408
-
409
- int dayOfWeek = timePicker.getValue() / weekday.length;
410
-
411
- int time = timePicker.getValue() % weekday.length;
412
-
413
-
414
-
415
- Intent data = new Intent();
416
-
417
- data.putExtra(Lecture.INTENT_NAME, new Lecture(name, room, dayOfWeek, time));
418
-
419
- setResult(Activity.RESULT_OK, data);
420
-
421
- finish();
422
-
423
- }
424
-
425
- });
426
-
427
-
428
-
429
- Intent data = getIntent(); //必須
430
-
431
- if(data == null) throw new IllegalArgumentException("必要な intent データがありません");
432
-
433
-
434
-
435
- weekday = data.getStringArrayExtra(INTENT_EXTRA_WEEKDAY);
436
-
437
- int timeCount = data.getIntExtra(INTENT_EXTRA_TIMECOUNT, 0);
438
-
439
-
440
-
441
- timePicker.setMinValue(0);
442
-
443
- timePicker.setMaxValue(weekday.length*timeCount-1);
444
-
445
- timePicker.setDisplayedValues(createDisplayedValues(weekday, timeCount));
446
-
447
- timePicker.setValue(0);
448
-
449
- }
450
-
451
- //ドラムロールに表示したい値を含んだ配列を作る
452
-
453
- private String[] createDisplayedValues(String[] weekday, int timeCount) {
454
-
455
- String[] displayedValues = new String[weekday.length * timeCount];
456
-
457
- for(int i=0, k=0; i<weekday.length; i++) {
458
-
459
- for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = weekday[i] + j;
460
-
461
- }
462
-
463
- return displayedValues;
464
-
465
- }
466
-
467
-
468
-
469
- public static Intent createStartActivityIntent(Context packageContext, String[] weekday, int timeCount) {
470
-
471
- Intent intent = new Intent(packageContext, SubActivity2.class);
472
-
473
- intent.putExtra(INTENT_EXTRA_WEEKDAY, weekday);
474
-
475
- intent.putExtra(INTENT_EXTRA_TIMECOUNT, timeCount);
476
-
477
- return intent;
478
-
479
- }
480
-
481
- }
482
-
483
- ```

7

コード修正

2020/03/24 09:26

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -148,7 +148,7 @@
148
148
 
149
149
  import android.content.Intent;
150
150
 
151
- import android.os.*;
151
+ import android.os.Bundle;
152
152
 
153
153
  import android.view.View;
154
154
 
@@ -158,7 +158,7 @@
158
158
 
159
159
  public class MainActivity extends AppCompatActivity {
160
160
 
161
- private static final String[] WEEKRY_HEADER = {"月","火","水","木","金","土"};
161
+ private static final String[] WEEKDAY_HEADER = {"月","火","水","木","金","土"};
162
162
 
163
163
  private static final int TIME_COUNT = 6; //一日の最大講義数
164
164
 
@@ -184,7 +184,7 @@
184
184
 
185
185
  public void onClick(View v) {
186
186
 
187
- Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKRY_HEADER, TIME_COUNT);
187
+ Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKDAY_HEADER, TIME_COUNT);
188
188
 
189
189
  startActivityForResult(intent, 0);
190
190
 
@@ -194,57 +194,53 @@
194
194
 
195
195
 
196
196
 
197
+ lectureSchedule = initScheduleGrid(WEEKDAY_HEADER, TIME_COUNT);
198
+
199
+ }
200
+
201
+ private Button[][] initScheduleGrid(String[] columnLabels, int rowCount) {
202
+
203
+ lectureSchedule = new Button[rowCount][columnLabels.length];
204
+
205
+
206
+
197
207
  GridLayout grid = findViewById(R.id.grid);
198
208
 
199
- lectureSchedule = initScheduleGrid(grid, WEEKRY_HEADER.length, TIME_COUNT);
200
-
201
- }
202
-
203
- private Button[][] initScheduleGrid(GridLayout grid, int width, int height) {
204
-
205
- grid.setColumnCount(1+width);
209
+ grid.setColumnCount(1+columnLabels.length);
206
-
210
+
207
- grid.setRowCount(1+height);
211
+ grid.setRowCount(1+rowCount);
208
-
209
-
210
-
211
- lectureSchedule = new Button[height][width];
212
212
 
213
213
 
214
214
 
215
215
  //一行目(曜日)
216
216
 
217
- Space space = new Space(this);
217
+ Space space = new Space(this); //左上角
218
-
218
+
219
- grid.addView(space, createTimeLabelLayoutParams()); //左上角
219
+ grid.addView(space, createLayoutParamsOfTimeLabel());
220
-
220
+
221
- for(int j=0; j<WEEKRY_HEADER.length; j++) {
221
+ for(int j=0; j<columnLabels.length; j++) {
222
222
 
223
223
  TextView textView = new TextView(this);
224
224
 
225
- textView.setText(WEEKRY_HEADER[j]);
225
+ textView.setText(columnLabels[j]);
226
226
 
227
227
  textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
228
228
 
229
- textView.setLayoutParams(createLayoutParams());
229
+ grid.addView(textView, createLayoutParamsOfWeekday());
230
-
231
- grid.addView(textView);
232
230
 
233
231
  }
234
232
 
235
233
  //二行目以降(時間割)
236
234
 
237
- for(int i=0; i<TIME_COUNT; i++) {
235
+ for(int i=0; i<rowCount; i++) {
238
236
 
239
237
  TextView timeLabel = new TextView(this);
240
238
 
241
239
  timeLabel.setText(""+(i+1));
242
240
 
243
- timeLabel.setLayoutParams(createTimeLabelLayoutParams());
241
+ grid.addView(timeLabel, createLayoutParamsOfTimeLabel());
244
-
245
- grid.addView(timeLabel);
242
+
246
-
247
- for(int j=0; j<WEEKRY_HEADER.length; j++) {
243
+ for(int j=0; j<columnLabels.length; j++) {
248
244
 
249
245
  Button button = new Button(this);
250
246
 
@@ -252,11 +248,9 @@
252
248
 
253
249
  button.setTag(new Lecture(j, i));
254
250
 
255
- button.setLayoutParams(createLayoutParams());
256
-
257
251
  //button.setOnClickListener(clickListener);
258
252
 
259
- grid.addView(button);
253
+ grid.addView(button, createLayoutParamsOfWeekday());
260
254
 
261
255
  lectureSchedule[i][j] = button;
262
256
 
@@ -268,38 +262,34 @@
268
262
 
269
263
  }
270
264
 
265
+ private GridLayout.LayoutParams createLayoutParamsOfTimeLabel() {
266
+
267
+ return createLayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, 0, GridLayout.CENTER);
268
+
269
+ }
270
+
271
- private GridLayout.LayoutParams createTimeLabelLayoutParams() {
271
+ private GridLayout.LayoutParams createLayoutParamsOfWeekday() {
272
+
273
+ return createLayoutParams( 0, 1, GridLayout.FILL);
274
+
275
+ }
276
+
277
+ private GridLayout.LayoutParams createLayoutParams(int width, int columnSpecWeight, GridLayout.Alignment rowSpecAlignment) {
272
278
 
273
279
  GridLayout.LayoutParams params = new GridLayout.LayoutParams();
274
280
 
275
- params.width = GridLayout.LayoutParams.WRAP_CONTENT;
281
+ params.width = width;
276
282
 
277
283
  params.height = GridLayout.LayoutParams.WRAP_CONTENT;
278
284
 
279
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 0);
285
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, columnSpecWeight);
280
-
286
+
281
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.CENTER, 1);
287
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, rowSpecAlignment, 1);
282
288
 
283
289
  return params;
284
290
 
285
291
  }
286
292
 
287
- private GridLayout.LayoutParams createLayoutParams() {
288
-
289
- GridLayout.LayoutParams params = new GridLayout.LayoutParams();
290
-
291
- params.width = 0;
292
-
293
- params.height = GridLayout.LayoutParams.WRAP_CONTENT;
294
-
295
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
296
-
297
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
298
-
299
- return params;
300
-
301
- }
302
-
303
293
  @Override
304
294
 
305
295
  protected void onActivityResult(int requestCode, int resultCode, Intent data){
@@ -346,11 +336,11 @@
346
336
 
347
337
  public class SubActivity2 extends AppCompatActivity {
348
338
 
349
- public static final String INTENT_DAY_OF_WEEK_LABELS = "dayOfWeekLabels";
339
+ public static final String INTENT_EXTRA_WEEKDAY = "Weekday";
350
-
340
+
351
- public static final String INTENT_TIME_COUNT = "TimeCount";
341
+ public static final String INTENT_EXTRA_TIMECOUNT = "TimeCount";
352
-
342
+
353
- private String[] dayOfWeekLabels;
343
+ private String[] weekday;
354
344
 
355
345
 
356
346
 
@@ -374,7 +364,7 @@
374
364
 
375
365
 
376
366
 
377
- // name と room の入力状態によって regist の有効/無効を設定
367
+ // name と room の入力状態によって ok の有効/無効を設定
378
368
 
379
369
  TextWatcher textWatcher = new TextWatcher() {
380
370
 
@@ -416,9 +406,9 @@
416
406
 
417
407
  String room = lectureRoom.getText().toString();
418
408
 
419
- int dayOfWeek = timePicker.getValue() / dayOfWeekLabels.length;
409
+ int dayOfWeek = timePicker.getValue() / weekday.length;
420
-
410
+
421
- int time = timePicker.getValue() % dayOfWeekLabels.length;
411
+ int time = timePicker.getValue() % weekday.length;
422
412
 
423
413
 
424
414
 
@@ -442,17 +432,17 @@
442
432
 
443
433
 
444
434
 
445
- dayOfWeekLabels = data.getStringArrayExtra(INTENT_DAY_OF_WEEK_LABELS);
435
+ weekday = data.getStringArrayExtra(INTENT_EXTRA_WEEKDAY);
446
-
436
+
447
- int timeCount = data.getIntExtra(INTENT_TIME_COUNT, 0);
437
+ int timeCount = data.getIntExtra(INTENT_EXTRA_TIMECOUNT, 0);
448
438
 
449
439
 
450
440
 
451
441
  timePicker.setMinValue(0);
452
442
 
453
- timePicker.setMaxValue(dayOfWeekLabels.length*timeCount-1);
443
+ timePicker.setMaxValue(weekday.length*timeCount-1);
454
-
444
+
455
- timePicker.setDisplayedValues(createDisplayedValues(dayOfWeekLabels, timeCount));
445
+ timePicker.setDisplayedValues(createDisplayedValues(weekday, timeCount));
456
446
 
457
447
  timePicker.setValue(0);
458
448
 
@@ -460,13 +450,13 @@
460
450
 
461
451
  //ドラムロールに表示したい値を含んだ配列を作る
462
452
 
463
- private String[] createDisplayedValues(String[] dayOfWeekLabels, int timeCount) {
453
+ private String[] createDisplayedValues(String[] weekday, int timeCount) {
464
-
454
+
465
- String[] displayedValues = new String[dayOfWeekLabels.length * timeCount];
455
+ String[] displayedValues = new String[weekday.length * timeCount];
466
-
456
+
467
- for(int i=0, k=0; i<dayOfWeekLabels.length; i++) {
457
+ for(int i=0, k=0; i<weekday.length; i++) {
468
-
458
+
469
- for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = dayOfWeekLabels[i] + j;
459
+ for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = weekday[i] + j;
470
460
 
471
461
  }
472
462
 
@@ -476,13 +466,13 @@
476
466
 
477
467
 
478
468
 
479
- public static Intent createStartActivityIntent(Context packageContext, String[] dayOfWeekLabels, int timeCount) {
469
+ public static Intent createStartActivityIntent(Context packageContext, String[] weekday, int timeCount) {
480
470
 
481
471
  Intent intent = new Intent(packageContext, SubActivity2.class);
482
472
 
483
- intent.putExtra(INTENT_DAY_OF_WEEK_LABELS, dayOfWeekLabels);
473
+ intent.putExtra(INTENT_EXTRA_WEEKDAY, weekday);
484
-
474
+
485
- intent.putExtra(INTENT_TIME_COUNT, timeCount);
475
+ intent.putExtra(INTENT_EXTRA_TIMECOUNT, timeCount);
486
476
 
487
477
  return intent;
488
478
 

6

コード 追加

2020/03/22 10:54

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -48,212 +48,446 @@
48
48
 
49
49
 
50
50
 
51
- activity_main.xml
52
-
53
- ```xml
54
-
55
- <?xml version="1.0" encoding="utf-8"?>
56
-
57
- <androidx.constraintlayout.widget.ConstraintLayout
58
-
59
- xmlns:android="http://schemas.android.com/apk/res/android"
60
-
61
- xmlns:app="http://schemas.android.com/apk/res-auto"
62
-
63
- xmlns:tools="http://schemas.android.com/tools"
64
-
65
- android:layout_width="match_parent"
66
-
67
- android:layout_height="match_parent"
68
-
69
- tools:context=".MainActivity">
70
-
71
- <androidx.constraintlayout.widget.ConstraintLayout
72
-
73
- android:id="@+id/header"
74
-
75
- android:layout_width="match_parent"
76
-
77
- android:layout_height="wrap_content"
78
-
79
- android:layout_marginBottom="50dp"
80
-
81
- app:layout_constraintTop_toTopOf="parent"
82
-
83
- app:layout_constraintLeft_toLeftOf="parent"
84
-
85
- app:layout_constraintRight_toRightOf="parent">
86
-
87
- <Button
88
-
89
- android:id="@+id/photoSelectButton"
90
-
91
- android:layout_width="wrap_content"
92
-
93
- android:layout_height="wrap_content"
94
-
95
- android:layout_margin="8dp"
96
-
97
- android:text="写真選択"
98
-
99
- app:layout_constraintTop_toTopOf="parent"
100
-
101
- app:layout_constraintLeft_toLeftOf="parent" />
102
-
103
- <Button
104
-
105
- android:id="@+id/scheduleSelectButton"
106
-
107
- android:layout_width="wrap_content"
108
-
109
- android:layout_height="wrap_content"
110
-
111
- android:layout_margin="8dp"
112
-
113
- android:text="時間割選択"
114
-
115
- app:layout_constraintTop_toTopOf="parent"
116
-
117
- app:layout_constraintRight_toRightOf="parent" />
118
-
119
- </androidx.constraintlayout.widget.ConstraintLayout>
120
-
121
- <GridLayout
122
-
123
- android:id="@+id/grid"
124
-
125
- android:layout_width="match_parent"
126
-
127
- android:layout_height="wrap_content"
128
-
129
- app:layout_constraintTop_toBottomOf="@id/header"
130
-
131
- app:layout_constraintLeft_toLeftOf="parent"
132
-
133
- app:layout_constraintRight_toRightOf="parent"
134
-
135
- app:layout_constraintBottom_toBottomOf="parent" />
136
-
137
- </androidx.constraintlayout.widget.ConstraintLayout>
138
-
139
- ```
140
-
141
- activity_sub2.xml
142
-
143
- ```xml
144
-
145
- <?xml version="1.0" encoding="utf-8"?>
146
-
147
- <androidx.constraintlayout.widget.ConstraintLayout
148
-
149
- xmlns:android="http://schemas.android.com/apk/res/android"
150
-
151
- xmlns:app="http://schemas.android.com/apk/res-auto"
152
-
153
- android:layout_width="match_parent"
154
-
155
- android:layout_height="match_parent">
156
-
157
-
158
-
159
- <EditText
160
-
161
- android:id="@+id/lectureName"
162
-
163
- android:layout_width="match_parent"
164
-
165
- android:layout_height="wrap_content"
166
-
167
- android:inputType="text"
168
-
169
- android:hint="講義名を入力してください。"
170
-
171
- app:layout_constraintTop_toTopOf="parent"
172
-
173
- app:layout_constraintLeft_toLeftOf="parent"
174
-
175
- app:layout_constraintRight_toRightOf="parent" />
176
-
177
- <EditText
178
-
179
- android:id="@+id/lectureRoom"
180
-
181
- android:layout_width="match_parent"
182
-
183
- android:layout_height="wrap_content"
184
-
185
- android:inputType="text"
186
-
187
- android:hint="教室を入力してください。"
188
-
189
- app:layout_constraintTop_toBottomOf="@id/lectureName"
190
-
191
- app:layout_constraintLeft_toLeftOf="parent"
192
-
193
- app:layout_constraintRight_toRightOf="parent" />
194
-
195
- <TextView
196
-
197
- android:id="@+id/scheduleLabel"
198
-
199
- android:layout_width="wrap_content"
200
-
201
- android:layout_height="wrap_content"
202
-
203
- android:layout_gravity="center"
204
-
205
- android:layout_margin="30dp"
206
-
207
- android:textStyle="bold"
208
-
209
- android:text="@string/時間割"
210
-
211
- android:textColor="#00f"
212
-
213
- android:textSize="40sp"
214
-
215
- app:layout_constraintTop_toBottomOf="@id/lectureRoom"
216
-
217
- app:layout_constraintLeft_toLeftOf="parent"
218
-
219
- app:layout_constraintRight_toRightOf="parent" />
220
-
221
- <NumberPicker
222
-
223
- android:id="@+id/timePicker"
224
-
225
- android:layout_width="wrap_content"
226
-
227
- android:layout_height="wrap_content"
228
-
229
- android:background="#aaf"
230
-
231
- android:layout_margin="10dp"
232
-
233
- app:layout_constraintTop_toBottomOf="@id/scheduleLabel"
234
-
235
- app:layout_constraintLeft_toLeftOf="parent"
236
-
237
- app:layout_constraintRight_toRightOf="parent" />
238
-
239
- <Button
240
-
241
- android:id="@+id/okButton"
242
-
243
- android:layout_width="match_parent"
244
-
245
- android:layout_height="wrap_content"
246
-
247
- android:layout_margin="40dp"
248
-
249
- android:text="@string/ok"
250
-
251
- app:layout_constraintTop_toBottomOf="@id/timePicker"
252
-
253
- app:layout_constraintLeft_toLeftOf="parent"
254
-
255
- app:layout_constraintRight_toRightOf="parent" />
256
-
257
- </androidx.constraintlayout.widget.ConstraintLayout>
258
-
259
- ```
51
+ ```java
52
+
53
+ import android.os.*;
54
+
55
+
56
+
57
+ class Lecture implements Parcelable {
58
+
59
+ static final String INTENT_NAME = "Lecture";
60
+
61
+ final String name;
62
+
63
+ final String room;
64
+
65
+ final int dayOfWeek; //0-5
66
+
67
+ final int time; //0-5
68
+
69
+ Lecture(int dayOfWeek, int time) {
70
+
71
+ this("","", dayOfWeek, time);
72
+
73
+ }
74
+
75
+ Lecture(String name, String room, int dayOfWeek, int time) {
76
+
77
+ this.name = name;
78
+
79
+ this.room = room;
80
+
81
+ this.dayOfWeek = dayOfWeek;
82
+
83
+ this.time = time;
84
+
85
+ }
86
+
87
+ @Override
88
+
89
+ public int describeContents() {
90
+
91
+ return 0;
92
+
93
+ }
94
+
95
+ @Override
96
+
97
+ public void writeToParcel(Parcel dest, int flags) {
98
+
99
+ dest.writeString(name);
100
+
101
+ dest.writeString(room);
102
+
103
+ dest.writeInt(dayOfWeek);
104
+
105
+ dest.writeInt(time);
106
+
107
+ }
108
+
109
+ public static final Creator<Lecture> CREATOR = new Creator<Lecture>() {
110
+
111
+ public Lecture createFromParcel(Parcel source) {
112
+
113
+ return new Lecture(source);
114
+
115
+ }
116
+
117
+ public Lecture[] newArray(int size) {
118
+
119
+ return new Lecture[size];
120
+
121
+ }
122
+
123
+ };
124
+
125
+ //Creator<Lecture> 用
126
+
127
+ private Lecture(Parcel source) {
128
+
129
+ name = source.readString();
130
+
131
+ room = source.readString();
132
+
133
+ dayOfWeek = source.readInt();
134
+
135
+ time = source.readInt();
136
+
137
+ }
138
+
139
+ }
140
+
141
+ ```
142
+
143
+ ```java
144
+
145
+ import androidx.appcompat.app.AppCompatActivity;
146
+
147
+ import android.app.Activity;
148
+
149
+ import android.content.Intent;
150
+
151
+ import android.os.*;
152
+
153
+ import android.view.View;
154
+
155
+ import android.widget.*;
156
+
157
+
158
+
159
+ public class MainActivity extends AppCompatActivity {
160
+
161
+ private static final String[] WEEKRY_HEADER = {"月","火","水","木","金","土"};
162
+
163
+ private static final int TIME_COUNT = 6; //一日の最大講義数
164
+
165
+ private Button[][] lectureSchedule;
166
+
167
+
168
+
169
+ @Override
170
+
171
+ protected void onCreate(Bundle savedInstanceState) {
172
+
173
+ super.onCreate(savedInstanceState);
174
+
175
+ setContentView(R.layout.activity_main);
176
+
177
+
178
+
179
+ Button scheduleSelectButton = findViewById(R.id.scheduleSelectButton);
180
+
181
+ scheduleSelectButton.setOnClickListener(new View.OnClickListener() {
182
+
183
+ @Override
184
+
185
+ public void onClick(View v) {
186
+
187
+ Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKRY_HEADER, TIME_COUNT);
188
+
189
+ startActivityForResult(intent, 0);
190
+
191
+ }
192
+
193
+ });
194
+
195
+
196
+
197
+ GridLayout grid = findViewById(R.id.grid);
198
+
199
+ lectureSchedule = initScheduleGrid(grid, WEEKRY_HEADER.length, TIME_COUNT);
200
+
201
+ }
202
+
203
+ private Button[][] initScheduleGrid(GridLayout grid, int width, int height) {
204
+
205
+ grid.setColumnCount(1+width);
206
+
207
+ grid.setRowCount(1+height);
208
+
209
+
210
+
211
+ lectureSchedule = new Button[height][width];
212
+
213
+
214
+
215
+ //一行目(曜日)
216
+
217
+ Space space = new Space(this);
218
+
219
+ grid.addView(space, createTimeLabelLayoutParams()); //左上角
220
+
221
+ for(int j=0; j<WEEKRY_HEADER.length; j++) {
222
+
223
+ TextView textView = new TextView(this);
224
+
225
+ textView.setText(WEEKRY_HEADER[j]);
226
+
227
+ textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
228
+
229
+ textView.setLayoutParams(createLayoutParams());
230
+
231
+ grid.addView(textView);
232
+
233
+ }
234
+
235
+ //二行目以降(時間割)
236
+
237
+ for(int i=0; i<TIME_COUNT; i++) {
238
+
239
+ TextView timeLabel = new TextView(this);
240
+
241
+ timeLabel.setText(""+(i+1));
242
+
243
+ timeLabel.setLayoutParams(createTimeLabelLayoutParams());
244
+
245
+ grid.addView(timeLabel);
246
+
247
+ for(int j=0; j<WEEKRY_HEADER.length; j++) {
248
+
249
+ Button button = new Button(this);
250
+
251
+ button.setText("");
252
+
253
+ button.setTag(new Lecture(j, i));
254
+
255
+ button.setLayoutParams(createLayoutParams());
256
+
257
+ //button.setOnClickListener(clickListener);
258
+
259
+ grid.addView(button);
260
+
261
+ lectureSchedule[i][j] = button;
262
+
263
+ }
264
+
265
+ }
266
+
267
+ return lectureSchedule;
268
+
269
+ }
270
+
271
+ private GridLayout.LayoutParams createTimeLabelLayoutParams() {
272
+
273
+ GridLayout.LayoutParams params = new GridLayout.LayoutParams();
274
+
275
+ params.width = GridLayout.LayoutParams.WRAP_CONTENT;
276
+
277
+ params.height = GridLayout.LayoutParams.WRAP_CONTENT;
278
+
279
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 0);
280
+
281
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.CENTER, 1);
282
+
283
+ return params;
284
+
285
+ }
286
+
287
+ private GridLayout.LayoutParams createLayoutParams() {
288
+
289
+ GridLayout.LayoutParams params = new GridLayout.LayoutParams();
290
+
291
+ params.width = 0;
292
+
293
+ params.height = GridLayout.LayoutParams.WRAP_CONTENT;
294
+
295
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
296
+
297
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
298
+
299
+ return params;
300
+
301
+ }
302
+
303
+ @Override
304
+
305
+ protected void onActivityResult(int requestCode, int resultCode, Intent data){
306
+
307
+ super.onActivityResult(requestCode, resultCode, data);
308
+
309
+ //リクエストコードと結果コードをチェック
310
+
311
+ if(requestCode == 0 && resultCode == Activity.RESULT_OK){
312
+
313
+ Lecture lecture = data.getParcelableExtra(Lecture.INTENT_NAME);
314
+
315
+ Button button = lectureSchedule[lecture.time][lecture.dayOfWeek];
316
+
317
+ button.setText(lecture.name);
318
+
319
+ button.setTag(lecture);
320
+
321
+ }
322
+
323
+ }
324
+
325
+ }
326
+
327
+ ```
328
+
329
+ ```java
330
+
331
+ import androidx.appcompat.app.AppCompatActivity;
332
+
333
+ import android.app.Activity;
334
+
335
+ import android.content.*;
336
+
337
+ import android.os.Bundle;
338
+
339
+ import android.text.*;
340
+
341
+ import android.view.View;
342
+
343
+ import android.widget.*;
344
+
345
+
346
+
347
+ public class SubActivity2 extends AppCompatActivity {
348
+
349
+ public static final String INTENT_DAY_OF_WEEK_LABELS = "dayOfWeekLabels";
350
+
351
+ public static final String INTENT_TIME_COUNT = "TimeCount";
352
+
353
+ private String[] dayOfWeekLabels;
354
+
355
+
356
+
357
+ @Override
358
+
359
+ protected void onCreate(Bundle savedInstanceState){
360
+
361
+ super.onCreate(savedInstanceState);
362
+
363
+ setContentView(R.layout.activity_sub2);
364
+
365
+
366
+
367
+ final EditText lectureName = findViewById(R.id.lectureName);
368
+
369
+ final EditText lectureRoom = findViewById(R.id.lectureRoom);
370
+
371
+ final NumberPicker timePicker = findViewById(R.id.timePicker);
372
+
373
+ final Button okButton = findViewById(R.id.okButton);
374
+
375
+
376
+
377
+ // name と room の入力状態によって regist の有効/無効を設定
378
+
379
+ TextWatcher textWatcher = new TextWatcher() {
380
+
381
+ @Override
382
+
383
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
384
+
385
+ @Override
386
+
387
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
388
+
389
+ okButton.setEnabled(lectureName.length() > 0 && lectureRoom.length() > 0);
390
+
391
+ }
392
+
393
+ @Override
394
+
395
+ public void afterTextChanged(Editable s) {}
396
+
397
+ };
398
+
399
+
400
+
401
+ lectureName.addTextChangedListener(textWatcher);
402
+
403
+ lectureRoom.addTextChangedListener(textWatcher);
404
+
405
+
406
+
407
+ okButton.setEnabled(false);
408
+
409
+ okButton.setOnClickListener(new View.OnClickListener() {
410
+
411
+ @Override
412
+
413
+ public void onClick(View v) {
414
+
415
+ String name = lectureName.getText().toString();
416
+
417
+ String room = lectureRoom.getText().toString();
418
+
419
+ int dayOfWeek = timePicker.getValue() / dayOfWeekLabels.length;
420
+
421
+ int time = timePicker.getValue() % dayOfWeekLabels.length;
422
+
423
+
424
+
425
+ Intent data = new Intent();
426
+
427
+ data.putExtra(Lecture.INTENT_NAME, new Lecture(name, room, dayOfWeek, time));
428
+
429
+ setResult(Activity.RESULT_OK, data);
430
+
431
+ finish();
432
+
433
+ }
434
+
435
+ });
436
+
437
+
438
+
439
+ Intent data = getIntent(); //必須
440
+
441
+ if(data == null) throw new IllegalArgumentException("必要な intent データがありません");
442
+
443
+
444
+
445
+ dayOfWeekLabels = data.getStringArrayExtra(INTENT_DAY_OF_WEEK_LABELS);
446
+
447
+ int timeCount = data.getIntExtra(INTENT_TIME_COUNT, 0);
448
+
449
+
450
+
451
+ timePicker.setMinValue(0);
452
+
453
+ timePicker.setMaxValue(dayOfWeekLabels.length*timeCount-1);
454
+
455
+ timePicker.setDisplayedValues(createDisplayedValues(dayOfWeekLabels, timeCount));
456
+
457
+ timePicker.setValue(0);
458
+
459
+ }
460
+
461
+ //ドラムロールに表示したい値を含んだ配列を作る
462
+
463
+ private String[] createDisplayedValues(String[] dayOfWeekLabels, int timeCount) {
464
+
465
+ String[] displayedValues = new String[dayOfWeekLabels.length * timeCount];
466
+
467
+ for(int i=0, k=0; i<dayOfWeekLabels.length; i++) {
468
+
469
+ for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = dayOfWeekLabels[i] + j;
470
+
471
+ }
472
+
473
+ return displayedValues;
474
+
475
+ }
476
+
477
+
478
+
479
+ public static Intent createStartActivityIntent(Context packageContext, String[] dayOfWeekLabels, int timeCount) {
480
+
481
+ Intent intent = new Intent(packageContext, SubActivity2.class);
482
+
483
+ intent.putExtra(INTENT_DAY_OF_WEEK_LABELS, dayOfWeekLabels);
484
+
485
+ intent.putExtra(INTENT_TIME_COUNT, timeCount);
486
+
487
+ return intent;
488
+
489
+ }
490
+
491
+ }
492
+
493
+ ```

5

xml 追加

2020/03/22 09:08

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -41,3 +41,219 @@
41
41
  NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.
42
42
 
43
43
  選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.
44
+
45
+
46
+
47
+ #追加
48
+
49
+
50
+
51
+ activity_main.xml
52
+
53
+ ```xml
54
+
55
+ <?xml version="1.0" encoding="utf-8"?>
56
+
57
+ <androidx.constraintlayout.widget.ConstraintLayout
58
+
59
+ xmlns:android="http://schemas.android.com/apk/res/android"
60
+
61
+ xmlns:app="http://schemas.android.com/apk/res-auto"
62
+
63
+ xmlns:tools="http://schemas.android.com/tools"
64
+
65
+ android:layout_width="match_parent"
66
+
67
+ android:layout_height="match_parent"
68
+
69
+ tools:context=".MainActivity">
70
+
71
+ <androidx.constraintlayout.widget.ConstraintLayout
72
+
73
+ android:id="@+id/header"
74
+
75
+ android:layout_width="match_parent"
76
+
77
+ android:layout_height="wrap_content"
78
+
79
+ android:layout_marginBottom="50dp"
80
+
81
+ app:layout_constraintTop_toTopOf="parent"
82
+
83
+ app:layout_constraintLeft_toLeftOf="parent"
84
+
85
+ app:layout_constraintRight_toRightOf="parent">
86
+
87
+ <Button
88
+
89
+ android:id="@+id/photoSelectButton"
90
+
91
+ android:layout_width="wrap_content"
92
+
93
+ android:layout_height="wrap_content"
94
+
95
+ android:layout_margin="8dp"
96
+
97
+ android:text="写真選択"
98
+
99
+ app:layout_constraintTop_toTopOf="parent"
100
+
101
+ app:layout_constraintLeft_toLeftOf="parent" />
102
+
103
+ <Button
104
+
105
+ android:id="@+id/scheduleSelectButton"
106
+
107
+ android:layout_width="wrap_content"
108
+
109
+ android:layout_height="wrap_content"
110
+
111
+ android:layout_margin="8dp"
112
+
113
+ android:text="時間割選択"
114
+
115
+ app:layout_constraintTop_toTopOf="parent"
116
+
117
+ app:layout_constraintRight_toRightOf="parent" />
118
+
119
+ </androidx.constraintlayout.widget.ConstraintLayout>
120
+
121
+ <GridLayout
122
+
123
+ android:id="@+id/grid"
124
+
125
+ android:layout_width="match_parent"
126
+
127
+ android:layout_height="wrap_content"
128
+
129
+ app:layout_constraintTop_toBottomOf="@id/header"
130
+
131
+ app:layout_constraintLeft_toLeftOf="parent"
132
+
133
+ app:layout_constraintRight_toRightOf="parent"
134
+
135
+ app:layout_constraintBottom_toBottomOf="parent" />
136
+
137
+ </androidx.constraintlayout.widget.ConstraintLayout>
138
+
139
+ ```
140
+
141
+ activity_sub2.xml
142
+
143
+ ```xml
144
+
145
+ <?xml version="1.0" encoding="utf-8"?>
146
+
147
+ <androidx.constraintlayout.widget.ConstraintLayout
148
+
149
+ xmlns:android="http://schemas.android.com/apk/res/android"
150
+
151
+ xmlns:app="http://schemas.android.com/apk/res-auto"
152
+
153
+ android:layout_width="match_parent"
154
+
155
+ android:layout_height="match_parent">
156
+
157
+
158
+
159
+ <EditText
160
+
161
+ android:id="@+id/lectureName"
162
+
163
+ android:layout_width="match_parent"
164
+
165
+ android:layout_height="wrap_content"
166
+
167
+ android:inputType="text"
168
+
169
+ android:hint="講義名を入力してください。"
170
+
171
+ app:layout_constraintTop_toTopOf="parent"
172
+
173
+ app:layout_constraintLeft_toLeftOf="parent"
174
+
175
+ app:layout_constraintRight_toRightOf="parent" />
176
+
177
+ <EditText
178
+
179
+ android:id="@+id/lectureRoom"
180
+
181
+ android:layout_width="match_parent"
182
+
183
+ android:layout_height="wrap_content"
184
+
185
+ android:inputType="text"
186
+
187
+ android:hint="教室を入力してください。"
188
+
189
+ app:layout_constraintTop_toBottomOf="@id/lectureName"
190
+
191
+ app:layout_constraintLeft_toLeftOf="parent"
192
+
193
+ app:layout_constraintRight_toRightOf="parent" />
194
+
195
+ <TextView
196
+
197
+ android:id="@+id/scheduleLabel"
198
+
199
+ android:layout_width="wrap_content"
200
+
201
+ android:layout_height="wrap_content"
202
+
203
+ android:layout_gravity="center"
204
+
205
+ android:layout_margin="30dp"
206
+
207
+ android:textStyle="bold"
208
+
209
+ android:text="@string/時間割"
210
+
211
+ android:textColor="#00f"
212
+
213
+ android:textSize="40sp"
214
+
215
+ app:layout_constraintTop_toBottomOf="@id/lectureRoom"
216
+
217
+ app:layout_constraintLeft_toLeftOf="parent"
218
+
219
+ app:layout_constraintRight_toRightOf="parent" />
220
+
221
+ <NumberPicker
222
+
223
+ android:id="@+id/timePicker"
224
+
225
+ android:layout_width="wrap_content"
226
+
227
+ android:layout_height="wrap_content"
228
+
229
+ android:background="#aaf"
230
+
231
+ android:layout_margin="10dp"
232
+
233
+ app:layout_constraintTop_toBottomOf="@id/scheduleLabel"
234
+
235
+ app:layout_constraintLeft_toLeftOf="parent"
236
+
237
+ app:layout_constraintRight_toRightOf="parent" />
238
+
239
+ <Button
240
+
241
+ android:id="@+id/okButton"
242
+
243
+ android:layout_width="match_parent"
244
+
245
+ android:layout_height="wrap_content"
246
+
247
+ android:layout_margin="40dp"
248
+
249
+ android:text="@string/ok"
250
+
251
+ app:layout_constraintTop_toBottomOf="@id/timePicker"
252
+
253
+ app:layout_constraintLeft_toLeftOf="parent"
254
+
255
+ app:layout_constraintRight_toRightOf="parent" />
256
+
257
+ </androidx.constraintlayout.widget.ConstraintLayout>
258
+
259
+ ```

4

button88 部分修正

2020/03/22 09:06

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -30,9 +30,9 @@
30
30
 
31
31
 
32
32
 
33
- また, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
33
+ また, id="@+id/button88" のボタンに onClick="btn88_onClick" していた場合, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
34
34
 
35
- xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされてい.
35
+ setOnClickListener で別の処理を設定しているため, xml の設定は上書きされ, btn88_onClick は呼ばれせん.
36
36
 
37
37
 
38
38
 

3

修正

2020/03/21 09:14

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
 
32
32
 
33
- また, 現状のコードではボタンを押しても SubActivity から MainActivity には戻りません.
33
+ また, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
34
34
 
35
35
  xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされています.
36
36
 

2

追記

2020/03/21 07:33

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -27,3 +27,17 @@
27
27
 
28
28
 
29
29
  2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.
30
+
31
+
32
+
33
+ また, 現状のコードではボタンを押しても SubActivity から MainActivity には戻りません.
34
+
35
+ xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされています.
36
+
37
+
38
+
39
+ setOnClickListener で行っている処理も無駄に意味不明です.
40
+
41
+ NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.
42
+
43
+ 選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.

1

追記

2020/03/21 07:31

投稿

jimbe
jimbe

スコア12737

test CHANGED
@@ -23,3 +23,7 @@
23
23
  の "36" です.
24
24
 
25
25
  pref 配列の"要素数"は 6x6=36 ですが, MinValue(0) と "0" 始まりですので, MaxValue は "35" でなければなりません.
26
+
27
+
28
+
29
+ 2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.