teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

9

スコープとキャスト修正

2020/03/24 09:38

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -35,7 +35,7 @@
35
35
 
36
36
  public class MainActivity extends AppCompatActivity {
37
37
  //ドラムロールの表示と同じ順にボタンの id を並べた配列を作る
38
- final int[] buttons={
38
+ private static final int[] buttons={
39
39
  R.id.btn月1,R.id.btn月2,R.id.btn月3,R.id.btn月4,R.id.btn月5,R.id.btn月6,
40
40
  R.id.btn火1,R.id.btn火2,R.id.btn火3,R.id.btn火4,R.id.btn火5,R.id.btn火6,
41
41
  R.id.btn水1,R.id.btn水2,R.id.btn水3,R.id.btn水4,R.id.btn水5,R.id.btn水6,
@@ -61,7 +61,7 @@
61
61
  //結果のコードを取得&ボタンのテクストに記入
62
62
  String txtName=data.getStringExtra("txtName");
63
63
  int numPicker=data.getIntExtra("numPicker", -1); //0-35
64
- Button button=(Button)findViewById(buttons[numPicker]);
64
+ Button button=findViewById(buttons[numPicker]);
65
65
  button.setText(txtName);
66
66
  }
67
67
  }

8

コードやり直し

2020/03/24 09:38

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -21,222 +21,222 @@
21
21
  NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.
22
22
  選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.
23
23
 
24
- #追加
24
+ #コードやり直し
25
25
 
26
+ MainActivity.java
26
27
  ```java
27
- import android.os.*;
28
+ package to.msn.wings.ourapp;
28
29
 
29
- class Lecture implements Parcelable {
30
- static final String INTENT_NAME = "Lecture";
31
- final String name;
32
- final String room;
33
- final int dayOfWeek; //0-5
34
- final int time; //0-5
35
- Lecture(int dayOfWeek, int time) {
36
- this("","", dayOfWeek, time);
37
- }
38
- Lecture(String name, String room, int dayOfWeek, int time) {
39
- this.name = name;
40
- this.room = room;
41
- this.dayOfWeek = dayOfWeek;
42
- this.time = time;
43
- }
44
- @Override
45
- public int describeContents() {
46
- return 0;
47
- }
48
- @Override
49
- public void writeToParcel(Parcel dest, int flags) {
50
- dest.writeString(name);
51
- dest.writeString(room);
52
- dest.writeInt(dayOfWeek);
53
- dest.writeInt(time);
54
- }
55
- public static final Creator<Lecture> CREATOR = new Creator<Lecture>() {
56
- public Lecture createFromParcel(Parcel source) {
57
- return new Lecture(source);
58
- }
59
- public Lecture[] newArray(int size) {
60
- return new Lecture[size];
61
- }
62
- };
63
- //Creator<Lecture> 用
64
- private Lecture(Parcel source) {
65
- name = source.readString();
66
- room = source.readString();
67
- dayOfWeek = source.readInt();
68
- time = source.readInt();
69
- }
70
- }
71
- ```
72
- ```java
73
30
  import androidx.appcompat.app.AppCompatActivity;
74
- import android.app.Activity;
31
+ import android.os.Bundle;
75
32
  import android.content.Intent;
76
- import android.os.Bundle;
77
33
  import android.view.View;
78
- import android.widget.*;
34
+ import android.widget.Button;
79
35
 
80
36
  public class MainActivity extends AppCompatActivity {
81
- private static final String[] WEEKDAY_HEADER = {"月","火","水","木","金","土"};
37
+ //ドラムロールの表示と同じ順にボタンの id を並べた配列を作る
38
+ final int[] buttons={
82
- private static final int TIME_COUNT = 6; //一日の最大講義数
39
+ R.id.btn月1,R.id.btn月2,R.id.btn月3,R.id.btn月4,R.id.btn月5,R.id.btn月6,
40
+ R.id.btn火1,R.id.btn火2,R.id.btn火3,R.id.btn火4,R.id.btn火5,R.id.btn火6,
41
+ R.id.btn水1,R.id.btn水2,R.id.btn水3,R.id.btn水4,R.id.btn水5,R.id.btn水6,
42
+ R.id.btn木1,R.id.btn木2,R.id.btn木3,R.id.btn木4,R.id.btn木5,R.id.btn木6,
43
+ R.id.btn金1,R.id.btn金2,R.id.btn金3,R.id.btn金4,R.id.btn金5,R.id.btn金6,
83
- private Button[][] lectureSchedule;
44
+ 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
-
45
+ };
85
46
  @Override
86
47
  protected void onCreate(Bundle savedInstanceState) {
87
48
  super.onCreate(savedInstanceState);
88
49
  setContentView(R.layout.activity_main);
89
-
90
- Button scheduleSelectButton = findViewById(R.id.scheduleSelectButton);
91
- scheduleSelectButton.setOnClickListener(new View.OnClickListener() {
92
- @Override
93
- public void onClick(View v) {
94
- Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKDAY_HEADER, TIME_COUNT);
95
- startActivityForResult(intent, 0);
96
- }
97
- });
98
-
99
- lectureSchedule = initScheduleGrid(WEEKDAY_HEADER, TIME_COUNT);
100
50
  }
101
- private Button[][] initScheduleGrid(String[] columnLabels, int rowCount) {
102
- lectureSchedule = new Button[rowCount][columnLabels.length];
103
-
104
- GridLayout grid = findViewById(R.id.grid);
105
- grid.setColumnCount(1+columnLabels.length);
106
- grid.setRowCount(1+rowCount);
107
-
108
- //一行目(曜日)
109
- Space space = new Space(this); //左上角
110
- grid.addView(space, createLayoutParamsOfTimeLabel());
111
- for(int j=0; j<columnLabels.length; j++) {
112
- TextView textView = new TextView(this);
51
+ public void btn2_onClick(View view){
113
- textView.setText(columnLabels[j]);
114
- textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
52
+ Intent intent=new Intent(this, SubActivity2.class);
115
- grid.addView(textView, createLayoutParamsOfWeekday());
116
- }
117
- //二行目以降(時間割)
53
+ //アクティビティを起動(サブ画面起動)
118
- for(int i=0; i<rowCount; i++) {
119
- TextView timeLabel = new TextView(this);
120
- timeLabel.setText(""+(i+1));
121
- grid.addView(timeLabel, createLayoutParamsOfTimeLabel());
122
- for(int j=0; j<columnLabels.length; j++) {
123
- Button button = new Button(this);
124
- button.setText("");
54
+ startActivityForResult(intent,0);
125
- button.setTag(new Lecture(j, i));
126
- //button.setOnClickListener(clickListener);
127
- grid.addView(button, createLayoutParamsOfWeekday());
128
- lectureSchedule[i][j] = button;
129
- }
130
- }
131
- return lectureSchedule;
132
55
  }
133
- private GridLayout.LayoutParams createLayoutParamsOfTimeLabel() {
134
- return createLayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, 0, GridLayout.CENTER);
135
- }
136
- private GridLayout.LayoutParams createLayoutParamsOfWeekday() {
137
- return createLayoutParams( 0, 1, GridLayout.FILL);
138
- }
139
- private GridLayout.LayoutParams createLayoutParams(int width, int columnSpecWeight, GridLayout.Alignment rowSpecAlignment) {
140
- GridLayout.LayoutParams params = new GridLayout.LayoutParams();
141
- params.width = width;
142
- params.height = GridLayout.LayoutParams.WRAP_CONTENT;
143
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, columnSpecWeight);
144
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, rowSpecAlignment, 1);
145
- return params;
146
- }
147
56
  @Override
148
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
57
+ protected void onActivityResult(int requestCode,int resultCode,Intent data){
149
58
  super.onActivityResult(requestCode, resultCode, data);
150
59
  //リクエストコードと結果コードをチェック
151
- if(requestCode == 0 && resultCode == Activity.RESULT_OK){
60
+ if(requestCode==0 && resultCode==RESULT_OK){
61
+ //結果のコードを取得&ボタンのテクストに記入
152
- Lecture lecture = data.getParcelableExtra(Lecture.INTENT_NAME);
62
+ String txtName=data.getStringExtra("txtName");
63
+ int numPicker=data.getIntExtra("numPicker", -1); //0-35
153
- Button button = lectureSchedule[lecture.time][lecture.dayOfWeek];
64
+ Button button=(Button)findViewById(buttons[numPicker]);
154
- button.setText(lecture.name);
65
+ button.setText(txtName);
155
- button.setTag(lecture);
156
66
  }
157
67
  }
158
68
  }
159
69
  ```
70
+ activity_main.xml
160
- ```java
71
+ ```xml
72
+ <?xml version="1.0" encoding="utf-8"?>
73
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
74
+ xmlns:app="http://schemas.android.com/apk/res-auto"
161
- import androidx.appcompat.app.AppCompatActivity;
75
+ android:layout_width="match_parent"
76
+ android:layout_height="match_parent"
162
- import android.app.Activity;
77
+ android:orientation="vertical">
163
- import android.content.*;
164
- import android.os.Bundle;
165
- import android.text.*;
166
- import android.view.View;
167
- import android.widget.*;
168
78
 
79
+ <androidx.constraintlayout.widget.ConstraintLayout
80
+ android:layout_width="match_parent"
169
- public class SubActivity2 extends AppCompatActivity {
81
+ android:layout_height="wrap_content">
170
- public static final String INTENT_EXTRA_WEEKDAY = "Weekday";
171
- public static final String INTENT_EXTRA_TIMECOUNT = "TimeCount";
172
- private String[] weekday;
173
82
 
174
- @Override
83
+ <Button
84
+ android:id="@+id/btn2"
85
+ android:layout_width="wrap_content"
175
- protected void onCreate(Bundle savedInstanceState){
86
+ android:layout_height="wrap_content"
176
- super.onCreate(savedInstanceState);
87
+ android:layout_gravity="end"
88
+ android:layout_marginTop="8dp"
89
+ android:layout_marginEnd="8dp"
177
- setContentView(R.layout.activity_sub2);
90
+ android:text="時間割選択"
91
+ app:layout_constraintEnd_toEndOf="parent"
92
+ app:layout_constraintTop_toTopOf="parent"
93
+ android:onClick="btn2_onClick"/>
178
94
 
95
+ <Button
96
+ android:id="@+id/btn1"
179
- final EditText lectureName = findViewById(R.id.lectureName);
97
+ android:layout_width="wrap_content"
180
- final EditText lectureRoom = findViewById(R.id.lectureRoom);
98
+ android:layout_height="wrap_content"
99
+ android:layout_gravity="top"
181
- final NumberPicker timePicker = findViewById(R.id.timePicker);
100
+ android:layout_marginStart="8dp"
101
+ android:layout_marginTop="8dp"
102
+ android:text="写真選択"
182
- final Button okButton = findViewById(R.id.okButton);
103
+ app:layout_constraintStart_toStartOf="parent"
104
+ app:layout_constraintTop_toTopOf="parent" />
105
+ </androidx.constraintlayout.widget.ConstraintLayout>
183
106
 
107
+ <LinearLayout
184
- // name と room の入力状態によって ok の有効/無効を設定
108
+ android:layout_width="match_parent"
109
+ android:layout_height="50dp"
185
- TextWatcher textWatcher = new TextWatcher() {
110
+ android:orientation="vertical">
186
- @Override
187
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
188
- @Override
189
- public void onTextChanged(CharSequence s, int start, int before, int count) {
190
- okButton.setEnabled(lectureName.length() > 0 && lectureRoom.length() > 0);
191
- }
192
- @Override
193
- public void afterTextChanged(Editable s) {}
111
+ </LinearLayout>
194
- };
195
112
 
113
+ <TableLayout
114
+ android:layout_width="match_parent"
196
- lectureName.addTextChangedListener(textWatcher);
115
+ android:layout_height="match_parent"
197
- lectureRoom.addTextChangedListener(textWatcher);
116
+ android:orientation="vertical"
117
+ android:shrinkColumns="0,1,2,3,4,5,6">
198
118
 
119
+ <TableRow
120
+ android:layout_width="match_parent"
121
+ android:layout_height="match_parent">
122
+ <Button
123
+ android:layout_width="wrap_content"
124
+ android:layout_height="wrap_content"/>
125
+ <Button
126
+ android:layout_width="wrap_content"
127
+ android:layout_height="wrap_content"
128
+ android:text="月"/>
129
+ <Button
130
+ android:layout_width="wrap_content"
131
+ android:layout_height="wrap_content"
132
+ android:text="火"/>
133
+ <Button
134
+ android:layout_width="wrap_content"
135
+ android:layout_height="wrap_content"
136
+ android:text="水"/>
137
+ <Button
138
+ android:layout_width="wrap_content"
139
+ android:layout_height="wrap_content"
140
+ android:text="木"/>
141
+ <Button
142
+ android:layout_width="wrap_content"
143
+ android:layout_height="wrap_content"
144
+ android:text="金"/>
145
+ <Button
146
+ android:layout_width="wrap_content"
147
+ android:layout_height="wrap_content"
148
+ android:text="土"/>
199
- okButton.setEnabled(false);
149
+ </TableRow>
200
- okButton.setOnClickListener(new View.OnClickListener() {
201
- @Override
202
- public void onClick(View v) {
203
- String name = lectureName.getText().toString();
204
- String room = lectureRoom.getText().toString();
205
- int dayOfWeek = timePicker.getValue() / weekday.length;
206
- int time = timePicker.getValue() % weekday.length;
207
150
 
151
+ <TableRow
152
+ android:layout_width="match_parent"
153
+ android:layout_height="match_parent">
154
+ <Button
208
- Intent data = new Intent();
155
+ android:layout_width="wrap_content"
209
- data.putExtra(Lecture.INTENT_NAME, new Lecture(name, room, dayOfWeek, time));
156
+ android:layout_height="wrap_content"
210
- setResult(Activity.RESULT_OK, data);
157
+ android:text="1" />
211
- finish();
158
+ <Button
212
- }
159
+ android:id="@+id/btn月1"
160
+ android:layout_width="wrap_content"
161
+ android:layout_height="wrap_content" />
213
- });
162
+ <Button
163
+ android:id="@+id/btn火1"
164
+ android:layout_width="wrap_content"
165
+ android:layout_height="wrap_content" />
166
+ <Button
167
+ android:id="@+id/btn水1"
168
+ android:layout_width="wrap_content"
169
+ android:layout_height="wrap_content" />
170
+ <Button
171
+ android:id="@+id/btn木1"
172
+ android:layout_width="wrap_content"
173
+ android:layout_height="wrap_content" />
174
+ <Button
175
+ android:id="@+id/btn金1"
176
+ android:layout_width="wrap_content"
177
+ android:layout_height="wrap_content" />
178
+ <Button
179
+ android:id="@+id/btn土1"
180
+ android:layout_width="wrap_content"
181
+ android:layout_height="wrap_content" />
182
+ </TableRow>
214
183
 
215
- Intent data = getIntent(); //必須
184
+ (以下5行, 時間ボタンの text 曜日ボタンの id を変える他は同様)
216
- if(data == null) throw new IllegalArgumentException("必要な intent データがありません");
217
185
 
186
+ </TableLayout>
187
+ </LinearLayout>
188
+ ```
189
+ SubActivity2.java
190
+ ```java
218
- weekday = data.getStringArrayExtra(INTENT_EXTRA_WEEKDAY);
191
+ package to.msn.wings.ourapp;
219
- int timeCount = data.getIntExtra(INTENT_EXTRA_TIMECOUNT, 0);
220
192
 
193
+ import android.content.Intent;
221
- timePicker.setMinValue(0);
194
+ import android.os.Bundle;
222
- timePicker.setMaxValue(weekday.length*timeCount-1);
195
+ import androidx.appcompat.app.AppCompatActivity;
223
- timePicker.setDisplayedValues(createDisplayedValues(weekday, timeCount));
224
- timePicker.setValue(0);
196
+ import android.view.View;
225
- }
197
+ import android.widget.EditText;
198
+ import android.widget.NumberPicker;
199
+
200
+ public class SubActivity2 extends AppCompatActivity{
226
201
  //ドラムロールに表示したい値を含んだ配列を作る
227
- private String[] createDisplayedValues(String[] weekday, int timeCount) {
202
+ private static final String[] pref={
228
- String[] displayedValues = new String[weekday.length * timeCount];
203
+ "月1","月2","月3","月4","月5","月6",
229
- for(int i=0, k=0; i<weekday.length; i++) {
204
+ "火1","火2","火3","火4","火5","火6",
205
+ "水1","水2","水3","水4","水5","水6",
206
+ "木1","木2","木3","木4","木5","木6",
207
+ "金1","金2","金3","金4","金5","金6",
230
- for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = weekday[i] + j;
208
+ "土1","土2","土3","土4","土5","土6"
231
- }
209
+ };
210
+ @Override
211
+ protected void onCreate(Bundle savedInstanceState){
232
- return displayedValues;
212
+ super.onCreate(savedInstanceState);
233
- }
213
+ setContentView(R.layout.activity_sub2);
234
214
 
235
- public static Intent createStartActivityIntent(Context packageContext, String[] weekday, int timeCount) {
215
+ //numberPickerをインスタンス化する
236
- Intent intent = new Intent(packageContext, SubActivity2.class);
216
+ final NumberPicker picker=findViewById(R.id.numPicker0);
237
- intent.putExtra(INTENT_EXTRA_WEEKDAY, weekday);
217
+ //配列のインデックスの最小、最大を指定する
238
- intent.putExtra(INTENT_EXTRA_TIMECOUNT, timeCount);
239
- return intent;
218
+ picker.setMinValue(0);
219
+ picker.setMaxValue(pref.length-1);
220
+ //numberpickerに配列をセットする
221
+ picker.setDisplayedValues(pref);
240
222
  }
223
+ //ボタンクリック時に呼び出されるメソッド
224
+ public void btn88_onClick(View v){
225
+ //インテントを生成&データをセット
226
+ Intent i=new Intent();
227
+ //講義名
228
+ EditText txtName=findViewById(R.id.txtName);
229
+ i.putExtra("txtName",txtName.getText().toString());
230
+ //教室
231
+ EditText txtName2=findViewById(R.id.txtName2);
232
+ i.putExtra("txtName2",txtName2.getText().toString());
233
+ //numberPicker
234
+ NumberPicker numPicker=findViewById(R.id.numPicker0);
235
+ i.putExtra("numPicker",numPicker.getValue()); //0-35
236
+ //結果情報をセット
237
+ setResult(RESULT_OK,i);
238
+ //現在のアクティビティを終了
239
+ finish();
240
+ }
241
241
  }
242
242
  ```

7

コード修正

2020/03/24 09:26

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -73,12 +73,12 @@
73
73
  import androidx.appcompat.app.AppCompatActivity;
74
74
  import android.app.Activity;
75
75
  import android.content.Intent;
76
- import android.os.*;
76
+ import android.os.Bundle;
77
77
  import android.view.View;
78
78
  import android.widget.*;
79
79
 
80
80
  public class MainActivity extends AppCompatActivity {
81
- private static final String[] WEEKRY_HEADER = {"月","火","水","木","金","土"};
81
+ private static final String[] WEEKDAY_HEADER = {"月","火","水","木","金","土"};
82
82
  private static final int TIME_COUNT = 6; //一日の最大講義数
83
83
  private Button[][] lectureSchedule;
84
84
 
@@ -91,62 +91,57 @@
91
91
  scheduleSelectButton.setOnClickListener(new View.OnClickListener() {
92
92
  @Override
93
93
  public void onClick(View v) {
94
- Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKRY_HEADER, TIME_COUNT);
94
+ Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKDAY_HEADER, TIME_COUNT);
95
95
  startActivityForResult(intent, 0);
96
96
  }
97
97
  });
98
98
 
99
- GridLayout grid = findViewById(R.id.grid);
100
- lectureSchedule = initScheduleGrid(grid, WEEKRY_HEADER.length, TIME_COUNT);
99
+ lectureSchedule = initScheduleGrid(WEEKDAY_HEADER, TIME_COUNT);
101
100
  }
102
- private Button[][] initScheduleGrid(GridLayout grid, int width, int height) {
101
+ private Button[][] initScheduleGrid(String[] columnLabels, int rowCount) {
103
- grid.setColumnCount(1+width);
102
+ lectureSchedule = new Button[rowCount][columnLabels.length];
104
- grid.setRowCount(1+height);
105
103
 
104
+ GridLayout grid = findViewById(R.id.grid);
106
- lectureSchedule = new Button[height][width];
105
+ grid.setColumnCount(1+columnLabels.length);
106
+ grid.setRowCount(1+rowCount);
107
107
 
108
108
  //一行目(曜日)
109
- Space space = new Space(this);
109
+ Space space = new Space(this); //左上角
110
- grid.addView(space, createTimeLabelLayoutParams()); //左上角
110
+ grid.addView(space, createLayoutParamsOfTimeLabel());
111
- for(int j=0; j<WEEKRY_HEADER.length; j++) {
111
+ for(int j=0; j<columnLabels.length; j++) {
112
112
  TextView textView = new TextView(this);
113
- textView.setText(WEEKRY_HEADER[j]);
113
+ textView.setText(columnLabels[j]);
114
114
  textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
115
- textView.setLayoutParams(createLayoutParams());
115
+ grid.addView(textView, createLayoutParamsOfWeekday());
116
- grid.addView(textView);
117
116
  }
118
117
  //二行目以降(時間割)
119
- for(int i=0; i<TIME_COUNT; i++) {
118
+ for(int i=0; i<rowCount; i++) {
120
119
  TextView timeLabel = new TextView(this);
121
120
  timeLabel.setText(""+(i+1));
122
- timeLabel.setLayoutParams(createTimeLabelLayoutParams());
121
+ grid.addView(timeLabel, createLayoutParamsOfTimeLabel());
123
- grid.addView(timeLabel);
124
- for(int j=0; j<WEEKRY_HEADER.length; j++) {
122
+ for(int j=0; j<columnLabels.length; j++) {
125
123
  Button button = new Button(this);
126
124
  button.setText("");
127
125
  button.setTag(new Lecture(j, i));
128
- button.setLayoutParams(createLayoutParams());
129
126
  //button.setOnClickListener(clickListener);
130
- grid.addView(button);
127
+ grid.addView(button, createLayoutParamsOfWeekday());
131
128
  lectureSchedule[i][j] = button;
132
129
  }
133
130
  }
134
131
  return lectureSchedule;
135
132
  }
136
- private GridLayout.LayoutParams createTimeLabelLayoutParams() {
133
+ private GridLayout.LayoutParams createLayoutParamsOfTimeLabel() {
137
- GridLayout.LayoutParams params = new GridLayout.LayoutParams();
138
- params.width = GridLayout.LayoutParams.WRAP_CONTENT;
139
- params.height = GridLayout.LayoutParams.WRAP_CONTENT;
140
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 0);
141
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.CENTER, 1);
134
+ return createLayoutParams(GridLayout.LayoutParams.WRAP_CONTENT, 0, GridLayout.CENTER);
142
- return params;
143
135
  }
144
- private GridLayout.LayoutParams createLayoutParams() {
136
+ private GridLayout.LayoutParams createLayoutParamsOfWeekday() {
137
+ return createLayoutParams( 0, 1, GridLayout.FILL);
138
+ }
139
+ private GridLayout.LayoutParams createLayoutParams(int width, int columnSpecWeight, GridLayout.Alignment rowSpecAlignment) {
145
140
  GridLayout.LayoutParams params = new GridLayout.LayoutParams();
146
- params.width = 0;
141
+ params.width = width;
147
142
  params.height = GridLayout.LayoutParams.WRAP_CONTENT;
148
- params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
143
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, columnSpecWeight);
149
- params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
144
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, rowSpecAlignment, 1);
150
145
  return params;
151
146
  }
152
147
  @Override
@@ -172,9 +167,9 @@
172
167
  import android.widget.*;
173
168
 
174
169
  public class SubActivity2 extends AppCompatActivity {
175
- public static final String INTENT_DAY_OF_WEEK_LABELS = "dayOfWeekLabels";
170
+ public static final String INTENT_EXTRA_WEEKDAY = "Weekday";
176
- public static final String INTENT_TIME_COUNT = "TimeCount";
171
+ public static final String INTENT_EXTRA_TIMECOUNT = "TimeCount";
177
- private String[] dayOfWeekLabels;
172
+ private String[] weekday;
178
173
 
179
174
  @Override
180
175
  protected void onCreate(Bundle savedInstanceState){
@@ -186,7 +181,7 @@
186
181
  final NumberPicker timePicker = findViewById(R.id.timePicker);
187
182
  final Button okButton = findViewById(R.id.okButton);
188
183
 
189
- // name と room の入力状態によって regist の有効/無効を設定
184
+ // name と room の入力状態によって ok の有効/無効を設定
190
185
  TextWatcher textWatcher = new TextWatcher() {
191
186
  @Override
192
187
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@@ -207,8 +202,8 @@
207
202
  public void onClick(View v) {
208
203
  String name = lectureName.getText().toString();
209
204
  String room = lectureRoom.getText().toString();
210
- int dayOfWeek = timePicker.getValue() / dayOfWeekLabels.length;
205
+ int dayOfWeek = timePicker.getValue() / weekday.length;
211
- int time = timePicker.getValue() % dayOfWeekLabels.length;
206
+ int time = timePicker.getValue() % weekday.length;
212
207
 
213
208
  Intent data = new Intent();
214
209
  data.putExtra(Lecture.INTENT_NAME, new Lecture(name, room, dayOfWeek, time));
@@ -220,27 +215,27 @@
220
215
  Intent data = getIntent(); //必須
221
216
  if(data == null) throw new IllegalArgumentException("必要な intent データがありません");
222
217
 
223
- dayOfWeekLabels = data.getStringArrayExtra(INTENT_DAY_OF_WEEK_LABELS);
218
+ weekday = data.getStringArrayExtra(INTENT_EXTRA_WEEKDAY);
224
- int timeCount = data.getIntExtra(INTENT_TIME_COUNT, 0);
219
+ int timeCount = data.getIntExtra(INTENT_EXTRA_TIMECOUNT, 0);
225
220
 
226
221
  timePicker.setMinValue(0);
227
- timePicker.setMaxValue(dayOfWeekLabels.length*timeCount-1);
222
+ timePicker.setMaxValue(weekday.length*timeCount-1);
228
- timePicker.setDisplayedValues(createDisplayedValues(dayOfWeekLabels, timeCount));
223
+ timePicker.setDisplayedValues(createDisplayedValues(weekday, timeCount));
229
224
  timePicker.setValue(0);
230
225
  }
231
226
  //ドラムロールに表示したい値を含んだ配列を作る
232
- private String[] createDisplayedValues(String[] dayOfWeekLabels, int timeCount) {
227
+ private String[] createDisplayedValues(String[] weekday, int timeCount) {
233
- String[] displayedValues = new String[dayOfWeekLabels.length * timeCount];
228
+ String[] displayedValues = new String[weekday.length * timeCount];
234
- for(int i=0, k=0; i<dayOfWeekLabels.length; i++) {
229
+ for(int i=0, k=0; i<weekday.length; i++) {
235
- for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = dayOfWeekLabels[i] + j;
230
+ for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = weekday[i] + j;
236
231
  }
237
232
  return displayedValues;
238
233
  }
239
234
 
240
- public static Intent createStartActivityIntent(Context packageContext, String[] dayOfWeekLabels, int timeCount) {
235
+ public static Intent createStartActivityIntent(Context packageContext, String[] weekday, int timeCount) {
241
236
  Intent intent = new Intent(packageContext, SubActivity2.class);
242
- intent.putExtra(INTENT_DAY_OF_WEEK_LABELS, dayOfWeekLabels);
237
+ intent.putExtra(INTENT_EXTRA_WEEKDAY, weekday);
243
- intent.putExtra(INTENT_TIME_COUNT, timeCount);
238
+ intent.putExtra(INTENT_EXTRA_TIMECOUNT, timeCount);
244
239
  return intent;
245
240
  }
246
241
  }

6

コード 追加

2020/03/22 10:54

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -23,108 +23,225 @@
23
23
 
24
24
  #追加
25
25
 
26
- activity_main.xml
27
- ```xml
26
+ ```java
28
- <?xml version="1.0" encoding="utf-8"?>
29
- <androidx.constraintlayout.widget.ConstraintLayout
27
+ import android.os.*;
30
- xmlns:android="http://schemas.android.com/apk/res/android"
28
+
31
- xmlns:app="http://schemas.android.com/apk/res-auto"
32
- xmlns:tools="http://schemas.android.com/tools"
33
- android:layout_width="match_parent"
34
- android:layout_height="match_parent"
35
- tools:context=".MainActivity">
29
+ class Lecture implements Parcelable {
36
- <androidx.constraintlayout.widget.ConstraintLayout
37
- android:id="@+id/header"
38
- android:layout_width="match_parent"
39
- android:layout_height="wrap_content"
40
- android:layout_marginBottom="50dp"
41
- app:layout_constraintTop_toTopOf="parent"
30
+ static final String INTENT_NAME = "Lecture";
42
- app:layout_constraintLeft_toLeftOf="parent"
43
- app:layout_constraintRight_toRightOf="parent">
44
- <Button
45
- android:id="@+id/photoSelectButton"
46
- android:layout_width="wrap_content"
47
- android:layout_height="wrap_content"
48
- android:layout_margin="8dp"
31
+ final String name;
49
- android:text="写真選択"
50
- app:layout_constraintTop_toTopOf="parent"
51
- app:layout_constraintLeft_toLeftOf="parent" />
52
- <Button
53
- android:id="@+id/scheduleSelectButton"
54
- android:layout_width="wrap_content"
55
- android:layout_height="wrap_content"
56
- android:layout_margin="8dp"
57
- android:text="時間割選択"
58
- app:layout_constraintTop_toTopOf="parent"
59
- app:layout_constraintRight_toRightOf="parent" />
60
- </androidx.constraintlayout.widget.ConstraintLayout>
61
- <GridLayout
32
+ final String room;
62
- android:id="@+id/grid"
33
+ final int dayOfWeek; //0-5
63
- android:layout_width="match_parent"
34
+ final int time; //0-5
35
+ Lecture(int dayOfWeek, int time) {
64
- android:layout_height="wrap_content"
36
+ this("","", dayOfWeek, time);
65
- app:layout_constraintTop_toBottomOf="@id/header"
66
- app:layout_constraintLeft_toLeftOf="parent"
37
+ }
67
- app:layout_constraintRight_toRightOf="parent"
38
+ Lecture(String name, String room, int dayOfWeek, int time) {
39
+ this.name = name;
40
+ this.room = room;
41
+ this.dayOfWeek = dayOfWeek;
42
+ this.time = time;
43
+ }
44
+ @Override
45
+ public int describeContents() {
46
+ return 0;
47
+ }
48
+ @Override
49
+ public void writeToParcel(Parcel dest, int flags) {
50
+ dest.writeString(name);
51
+ dest.writeString(room);
52
+ dest.writeInt(dayOfWeek);
53
+ dest.writeInt(time);
54
+ }
68
- app:layout_constraintBottom_toBottomOf="parent" />
55
+ public static final Creator<Lecture> CREATOR = new Creator<Lecture>() {
56
+ public Lecture createFromParcel(Parcel source) {
57
+ return new Lecture(source);
58
+ }
59
+ public Lecture[] newArray(int size) {
60
+ return new Lecture[size];
61
+ }
62
+ };
69
- </androidx.constraintlayout.widget.ConstraintLayout>
63
+ //Creator<Lecture>
64
+ private Lecture(Parcel source) {
65
+ name = source.readString();
66
+ room = source.readString();
67
+ dayOfWeek = source.readInt();
68
+ time = source.readInt();
69
+ }
70
+ }
70
71
  ```
71
- activity_sub2.xml
72
- ```xml
72
+ ```java
73
- <?xml version="1.0" encoding="utf-8"?>
74
- <androidx.constraintlayout.widget.ConstraintLayout
73
+ import androidx.appcompat.app.AppCompatActivity;
75
- xmlns:android="http://schemas.android.com/apk/res/android"
76
- xmlns:app="http://schemas.android.com/apk/res-auto"
74
+ import android.app.Activity;
77
- android:layout_width="match_parent"
75
+ import android.content.Intent;
76
+ import android.os.*;
77
+ import android.view.View;
78
- android:layout_height="match_parent">
78
+ import android.widget.*;
79
79
 
80
- <EditText
81
- android:id="@+id/lectureName"
82
- android:layout_width="match_parent"
83
- android:layout_height="wrap_content"
84
- android:inputType="text"
85
- android:hint="講義名を入力してください。"
86
- app:layout_constraintTop_toTopOf="parent"
87
- app:layout_constraintLeft_toLeftOf="parent"
88
- app:layout_constraintRight_toRightOf="parent" />
89
- <EditText
90
- android:id="@+id/lectureRoom"
91
- android:layout_width="match_parent"
92
- android:layout_height="wrap_content"
93
- android:inputType="text"
94
- android:hint="教室を入力してください。"
95
- app:layout_constraintTop_toBottomOf="@id/lectureName"
96
- app:layout_constraintLeft_toLeftOf="parent"
97
- app:layout_constraintRight_toRightOf="parent" />
98
- <TextView
99
- android:id="@+id/scheduleLabel"
100
- android:layout_width="wrap_content"
101
- android:layout_height="wrap_content"
102
- android:layout_gravity="center"
103
- android:layout_margin="30dp"
104
- android:textStyle="bold"
105
- android:text="@string/時間割"
106
- android:textColor="#00f"
107
- android:textSize="40sp"
108
- app:layout_constraintTop_toBottomOf="@id/lectureRoom"
109
- app:layout_constraintLeft_toLeftOf="parent"
110
- app:layout_constraintRight_toRightOf="parent" />
111
- <NumberPicker
112
- android:id="@+id/timePicker"
113
- android:layout_width="wrap_content"
114
- android:layout_height="wrap_content"
115
- android:background="#aaf"
116
- android:layout_margin="10dp"
117
- app:layout_constraintTop_toBottomOf="@id/scheduleLabel"
118
- app:layout_constraintLeft_toLeftOf="parent"
119
- app:layout_constraintRight_toRightOf="parent" />
120
- <Button
121
- android:id="@+id/okButton"
122
- android:layout_width="match_parent"
123
- android:layout_height="wrap_content"
124
- android:layout_margin="40dp"
125
- android:text="@string/ok"
126
- app:layout_constraintTop_toBottomOf="@id/timePicker"
127
- app:layout_constraintLeft_toLeftOf="parent"
128
- app:layout_constraintRight_toRightOf="parent" />
129
- </androidx.constraintlayout.widget.ConstraintLayout>
80
+ public class MainActivity extends AppCompatActivity {
81
+ private static final String[] WEEKRY_HEADER = {"","火","水","木","金","土"};
82
+ private static final int TIME_COUNT = 6; //一日の最大講義数
83
+ private Button[][] lectureSchedule;
84
+
85
+ @Override
86
+ protected void onCreate(Bundle savedInstanceState) {
87
+ super.onCreate(savedInstanceState);
88
+ setContentView(R.layout.activity_main);
89
+
90
+ Button scheduleSelectButton = findViewById(R.id.scheduleSelectButton);
91
+ scheduleSelectButton.setOnClickListener(new View.OnClickListener() {
92
+ @Override
93
+ public void onClick(View v) {
94
+ Intent intent = SubActivity2.createStartActivityIntent( MainActivity.this, WEEKRY_HEADER, TIME_COUNT);
95
+ startActivityForResult(intent, 0);
96
+ }
97
+ });
98
+
99
+ GridLayout grid = findViewById(R.id.grid);
100
+ lectureSchedule = initScheduleGrid(grid, WEEKRY_HEADER.length, TIME_COUNT);
101
+ }
102
+ private Button[][] initScheduleGrid(GridLayout grid, int width, int height) {
103
+ grid.setColumnCount(1+width);
104
+ grid.setRowCount(1+height);
105
+
106
+ lectureSchedule = new Button[height][width];
107
+
108
+ //一行目(曜日)
109
+ Space space = new Space(this);
110
+ grid.addView(space, createTimeLabelLayoutParams()); //左上角
111
+ for(int j=0; j<WEEKRY_HEADER.length; j++) {
112
+ TextView textView = new TextView(this);
113
+ textView.setText(WEEKRY_HEADER[j]);
114
+ textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
115
+ textView.setLayoutParams(createLayoutParams());
116
+ grid.addView(textView);
117
+ }
118
+ //二行目以降(時間割)
119
+ for(int i=0; i<TIME_COUNT; i++) {
120
+ TextView timeLabel = new TextView(this);
121
+ timeLabel.setText(""+(i+1));
122
+ timeLabel.setLayoutParams(createTimeLabelLayoutParams());
123
+ grid.addView(timeLabel);
124
+ for(int j=0; j<WEEKRY_HEADER.length; j++) {
125
+ Button button = new Button(this);
126
+ button.setText("");
127
+ button.setTag(new Lecture(j, i));
128
+ button.setLayoutParams(createLayoutParams());
129
+ //button.setOnClickListener(clickListener);
130
+ grid.addView(button);
131
+ lectureSchedule[i][j] = button;
132
+ }
133
+ }
134
+ return lectureSchedule;
135
+ }
136
+ private GridLayout.LayoutParams createTimeLabelLayoutParams() {
137
+ GridLayout.LayoutParams params = new GridLayout.LayoutParams();
138
+ params.width = GridLayout.LayoutParams.WRAP_CONTENT;
139
+ params.height = GridLayout.LayoutParams.WRAP_CONTENT;
140
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 0);
141
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.CENTER, 1);
142
+ return params;
143
+ }
144
+ private GridLayout.LayoutParams createLayoutParams() {
145
+ GridLayout.LayoutParams params = new GridLayout.LayoutParams();
146
+ params.width = 0;
147
+ params.height = GridLayout.LayoutParams.WRAP_CONTENT;
148
+ params.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
149
+ params.rowSpec = GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1);
150
+ return params;
151
+ }
152
+ @Override
153
+ protected void onActivityResult(int requestCode, int resultCode, Intent data){
154
+ super.onActivityResult(requestCode, resultCode, data);
155
+ //リクエストコードと結果コードをチェック
156
+ if(requestCode == 0 && resultCode == Activity.RESULT_OK){
157
+ Lecture lecture = data.getParcelableExtra(Lecture.INTENT_NAME);
158
+ Button button = lectureSchedule[lecture.time][lecture.dayOfWeek];
159
+ button.setText(lecture.name);
160
+ button.setTag(lecture);
161
+ }
162
+ }
163
+ }
164
+ ```
165
+ ```java
166
+ import androidx.appcompat.app.AppCompatActivity;
167
+ import android.app.Activity;
168
+ import android.content.*;
169
+ import android.os.Bundle;
170
+ import android.text.*;
171
+ import android.view.View;
172
+ import android.widget.*;
173
+
174
+ public class SubActivity2 extends AppCompatActivity {
175
+ public static final String INTENT_DAY_OF_WEEK_LABELS = "dayOfWeekLabels";
176
+ public static final String INTENT_TIME_COUNT = "TimeCount";
177
+ private String[] dayOfWeekLabels;
178
+
179
+ @Override
180
+ protected void onCreate(Bundle savedInstanceState){
181
+ super.onCreate(savedInstanceState);
182
+ setContentView(R.layout.activity_sub2);
183
+
184
+ final EditText lectureName = findViewById(R.id.lectureName);
185
+ final EditText lectureRoom = findViewById(R.id.lectureRoom);
186
+ final NumberPicker timePicker = findViewById(R.id.timePicker);
187
+ final Button okButton = findViewById(R.id.okButton);
188
+
189
+ // name と room の入力状態によって regist の有効/無効を設定
190
+ TextWatcher textWatcher = new TextWatcher() {
191
+ @Override
192
+ public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
193
+ @Override
194
+ public void onTextChanged(CharSequence s, int start, int before, int count) {
195
+ okButton.setEnabled(lectureName.length() > 0 && lectureRoom.length() > 0);
196
+ }
197
+ @Override
198
+ public void afterTextChanged(Editable s) {}
199
+ };
200
+
201
+ lectureName.addTextChangedListener(textWatcher);
202
+ lectureRoom.addTextChangedListener(textWatcher);
203
+
204
+ okButton.setEnabled(false);
205
+ okButton.setOnClickListener(new View.OnClickListener() {
206
+ @Override
207
+ public void onClick(View v) {
208
+ String name = lectureName.getText().toString();
209
+ String room = lectureRoom.getText().toString();
210
+ int dayOfWeek = timePicker.getValue() / dayOfWeekLabels.length;
211
+ int time = timePicker.getValue() % dayOfWeekLabels.length;
212
+
213
+ Intent data = new Intent();
214
+ data.putExtra(Lecture.INTENT_NAME, new Lecture(name, room, dayOfWeek, time));
215
+ setResult(Activity.RESULT_OK, data);
216
+ finish();
217
+ }
218
+ });
219
+
220
+ Intent data = getIntent(); //必須
221
+ if(data == null) throw new IllegalArgumentException("必要な intent データがありません");
222
+
223
+ dayOfWeekLabels = data.getStringArrayExtra(INTENT_DAY_OF_WEEK_LABELS);
224
+ int timeCount = data.getIntExtra(INTENT_TIME_COUNT, 0);
225
+
226
+ timePicker.setMinValue(0);
227
+ timePicker.setMaxValue(dayOfWeekLabels.length*timeCount-1);
228
+ timePicker.setDisplayedValues(createDisplayedValues(dayOfWeekLabels, timeCount));
229
+ timePicker.setValue(0);
230
+ }
231
+ //ドラムロールに表示したい値を含んだ配列を作る
232
+ private String[] createDisplayedValues(String[] dayOfWeekLabels, int timeCount) {
233
+ String[] displayedValues = new String[dayOfWeekLabels.length * timeCount];
234
+ for(int i=0, k=0; i<dayOfWeekLabels.length; i++) {
235
+ for(int j=1; j<=timeCount; j++, k++) displayedValues[k] = dayOfWeekLabels[i] + j;
236
+ }
237
+ return displayedValues;
238
+ }
239
+
240
+ public static Intent createStartActivityIntent(Context packageContext, String[] dayOfWeekLabels, int timeCount) {
241
+ Intent intent = new Intent(packageContext, SubActivity2.class);
242
+ intent.putExtra(INTENT_DAY_OF_WEEK_LABELS, dayOfWeekLabels);
243
+ intent.putExtra(INTENT_TIME_COUNT, timeCount);
244
+ return intent;
245
+ }
246
+ }
130
247
  ```

5

xml 追加

2020/03/22 09:08

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -19,4 +19,112 @@
19
19
 
20
20
  setOnClickListener で行っている処理も無駄に意味不明です.
21
21
  NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.
22
- 選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.
22
+ 選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.
23
+
24
+ #追加
25
+
26
+ activity_main.xml
27
+ ```xml
28
+ <?xml version="1.0" encoding="utf-8"?>
29
+ <androidx.constraintlayout.widget.ConstraintLayout
30
+ xmlns:android="http://schemas.android.com/apk/res/android"
31
+ xmlns:app="http://schemas.android.com/apk/res-auto"
32
+ xmlns:tools="http://schemas.android.com/tools"
33
+ android:layout_width="match_parent"
34
+ android:layout_height="match_parent"
35
+ tools:context=".MainActivity">
36
+ <androidx.constraintlayout.widget.ConstraintLayout
37
+ android:id="@+id/header"
38
+ android:layout_width="match_parent"
39
+ android:layout_height="wrap_content"
40
+ android:layout_marginBottom="50dp"
41
+ app:layout_constraintTop_toTopOf="parent"
42
+ app:layout_constraintLeft_toLeftOf="parent"
43
+ app:layout_constraintRight_toRightOf="parent">
44
+ <Button
45
+ android:id="@+id/photoSelectButton"
46
+ android:layout_width="wrap_content"
47
+ android:layout_height="wrap_content"
48
+ android:layout_margin="8dp"
49
+ android:text="写真選択"
50
+ app:layout_constraintTop_toTopOf="parent"
51
+ app:layout_constraintLeft_toLeftOf="parent" />
52
+ <Button
53
+ android:id="@+id/scheduleSelectButton"
54
+ android:layout_width="wrap_content"
55
+ android:layout_height="wrap_content"
56
+ android:layout_margin="8dp"
57
+ android:text="時間割選択"
58
+ app:layout_constraintTop_toTopOf="parent"
59
+ app:layout_constraintRight_toRightOf="parent" />
60
+ </androidx.constraintlayout.widget.ConstraintLayout>
61
+ <GridLayout
62
+ android:id="@+id/grid"
63
+ android:layout_width="match_parent"
64
+ android:layout_height="wrap_content"
65
+ app:layout_constraintTop_toBottomOf="@id/header"
66
+ app:layout_constraintLeft_toLeftOf="parent"
67
+ app:layout_constraintRight_toRightOf="parent"
68
+ app:layout_constraintBottom_toBottomOf="parent" />
69
+ </androidx.constraintlayout.widget.ConstraintLayout>
70
+ ```
71
+ activity_sub2.xml
72
+ ```xml
73
+ <?xml version="1.0" encoding="utf-8"?>
74
+ <androidx.constraintlayout.widget.ConstraintLayout
75
+ xmlns:android="http://schemas.android.com/apk/res/android"
76
+ xmlns:app="http://schemas.android.com/apk/res-auto"
77
+ android:layout_width="match_parent"
78
+ android:layout_height="match_parent">
79
+
80
+ <EditText
81
+ android:id="@+id/lectureName"
82
+ android:layout_width="match_parent"
83
+ android:layout_height="wrap_content"
84
+ android:inputType="text"
85
+ android:hint="講義名を入力してください。"
86
+ app:layout_constraintTop_toTopOf="parent"
87
+ app:layout_constraintLeft_toLeftOf="parent"
88
+ app:layout_constraintRight_toRightOf="parent" />
89
+ <EditText
90
+ android:id="@+id/lectureRoom"
91
+ android:layout_width="match_parent"
92
+ android:layout_height="wrap_content"
93
+ android:inputType="text"
94
+ android:hint="教室を入力してください。"
95
+ app:layout_constraintTop_toBottomOf="@id/lectureName"
96
+ app:layout_constraintLeft_toLeftOf="parent"
97
+ app:layout_constraintRight_toRightOf="parent" />
98
+ <TextView
99
+ android:id="@+id/scheduleLabel"
100
+ android:layout_width="wrap_content"
101
+ android:layout_height="wrap_content"
102
+ android:layout_gravity="center"
103
+ android:layout_margin="30dp"
104
+ android:textStyle="bold"
105
+ android:text="@string/時間割"
106
+ android:textColor="#00f"
107
+ android:textSize="40sp"
108
+ app:layout_constraintTop_toBottomOf="@id/lectureRoom"
109
+ app:layout_constraintLeft_toLeftOf="parent"
110
+ app:layout_constraintRight_toRightOf="parent" />
111
+ <NumberPicker
112
+ android:id="@+id/timePicker"
113
+ android:layout_width="wrap_content"
114
+ android:layout_height="wrap_content"
115
+ android:background="#aaf"
116
+ android:layout_margin="10dp"
117
+ app:layout_constraintTop_toBottomOf="@id/scheduleLabel"
118
+ app:layout_constraintLeft_toLeftOf="parent"
119
+ app:layout_constraintRight_toRightOf="parent" />
120
+ <Button
121
+ android:id="@+id/okButton"
122
+ android:layout_width="match_parent"
123
+ android:layout_height="wrap_content"
124
+ android:layout_margin="40dp"
125
+ android:text="@string/ok"
126
+ app:layout_constraintTop_toBottomOf="@id/timePicker"
127
+ app:layout_constraintLeft_toLeftOf="parent"
128
+ app:layout_constraintRight_toRightOf="parent" />
129
+ </androidx.constraintlayout.widget.ConstraintLayout>
130
+ ```

4

button88 部分修正

2020/03/22 09:06

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -14,8 +14,8 @@
14
14
 
15
15
  2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.
16
16
 
17
- また, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
17
+ また, id="@+id/button88" のボタンに onClick="btn88_onClick" していた場合, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
18
- xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされてい.
18
+ setOnClickListener で別の処理を設定しているため, xml の設定は上書きされ, btn88_onClick は呼ばれせん.
19
19
 
20
20
  setOnClickListener で行っている処理も無駄に意味不明です.
21
21
  NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.

3

修正

2020/03/21 09:14

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.
16
16
 
17
- また, 現状のコードではボタンを押しても SubActivity から MainActivity には戻りません.
17
+ また, 現状のコードではボタンを押しても SubActivity2 から MainActivity には戻りません.
18
18
  xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされています.
19
19
 
20
20
  setOnClickListener で行っている処理も無駄に意味不明です.

2

追記

2020/03/21 07:33

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -12,4 +12,11 @@
12
12
  の "36" です.
13
13
  pref 配列の"要素数"は 6x6=36 ですが, MinValue(0) と "0" 始まりですので, MaxValue は "35" でなければなりません.
14
14
 
15
- 2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.
15
+ 2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.
16
+
17
+ また, 現状のコードではボタンを押しても SubActivity から MainActivity には戻りません.
18
+ xml で onClick を指定しているボタンに対して setOnClickListener で別の処理を設定しているため, xml の設定は上書きされています.
19
+
20
+ setOnClickListener で行っている処理も無駄に意味不明です.
21
+ NumberPicker は選択するだけで決定するインターフェースであり, 選択してボタンを押して決定すると言うものではありません.
22
+ 選択した時に呼ばれるリスナーがありますので, お調べになっては如何でしょうか.

1

追記

2020/03/21 07:31

投稿

jimbe
jimbe

スコア13355

answer CHANGED
@@ -10,4 +10,6 @@
10
10
  picker.setMaxValue(36);
11
11
  ```
12
12
  の "36" です.
13
- pref 配列の"要素数"は 6x6=36 ですが, MinValue(0) と "0" 始まりですので, MaxValue は "35" でなければなりません.
13
+ pref 配列の"要素数"は 6x6=36 ですが, MinValue(0) と "0" 始まりですので, MaxValue は "35" でなければなりません.
14
+
15
+ 2つ目は3つ目の一部分のことのようですし, 3つ目はボタンがどうなっているかに因りますので, 全体のレイアウトのご提示が必要です.