質問編集履歴

8

誤字

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -142,7 +142,9 @@
142
142
 
143
143
 
144
144
 
145
- MainActivityです。```java
145
+ MainActivityです。
146
+
147
+ ```java
146
148
 
147
149
  package com.example.android.sample.calculator;
148
150
 

7

コードの追加

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -139,3 +139,449 @@
139
139
  //これには、Attribute android:theme is not allowed hereがでます。
140
140
 
141
141
  ```
142
+
143
+
144
+
145
+ MainActivityです。```java
146
+
147
+ package com.example.android.sample.calculator;
148
+
149
+
150
+
151
+ import android.content.Intent;
152
+
153
+ import android.support.v7.app.AppCompatActivity;
154
+
155
+ import android.os.Bundle;
156
+
157
+ import android.text.Editable;
158
+
159
+ import android.text.TextUtils;
160
+
161
+ import android.text.TextWatcher;
162
+
163
+ import android.view.View;
164
+
165
+ import android.widget.EditText;
166
+
167
+ import android.widget.Spinner;
168
+
169
+ import android.widget.TextView;
170
+
171
+
172
+
173
+ public class MainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener{
174
+
175
+
176
+
177
+ //上の計算ボタンを押した時のリクエストコード
178
+
179
+ private static final int REQUEST_CODE_ANOTHER_CALC_1=1;
180
+
181
+ //上の計算ボタンを押した時のリクエストコード
182
+
183
+ private static final int REQUEST_CODE_ANOTHER_CALC_2=2;
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+ //上のEditText
192
+
193
+ private EditText numberInput1;
194
+
195
+ //下のEditText
196
+
197
+ private EditText numberInput2;
198
+
199
+
200
+
201
+
202
+
203
+ //演算子選択用のSpinner
204
+
205
+ private Spinner operatorSelector;
206
+
207
+
208
+
209
+ //計算結果表示用のTextView
210
+
211
+ private TextView calcResult;
212
+
213
+
214
+
215
+ @Override
216
+
217
+ protected void onCreate(Bundle savedInstanceState) {
218
+
219
+ super.onCreate(savedInstanceState);
220
+
221
+ setContentView(R.layout.activity_main);
222
+
223
+
224
+
225
+
226
+
227
+ //上のEditText
228
+
229
+ numberInput1=(EditText)findViewById(R.id.numberInput);
230
+
231
+ //上のEditTextの文字入力イベントを受け取る
232
+
233
+ numberInput1.addTextChangedListener(this);
234
+
235
+
236
+
237
+ //下のEditText
238
+
239
+ numberInput2=(EditText)findViewById(R.id.numberInput2);
240
+
241
+
242
+
243
+ //下のEditTextの文字入力を受け取る
244
+
245
+ numberInput2.addTextChangedListener(this);
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+ //演算子選択用のSpinner
256
+
257
+ operatorSelector=(Spinner)findViewById(R.id.operatorSelector);
258
+
259
+
260
+
261
+ //計算結果表示用のTextView
262
+
263
+ calcResult=(TextView)findViewById(R.id.calcResult);
264
+
265
+
266
+
267
+
268
+
269
+ //上の計算ボタン
270
+
271
+ findViewById(R.id.calcButton1).setOnClickListener(this);
272
+
273
+ //下の計算ボタン
274
+
275
+ findViewById(R.id.calcButton2).setOnClickListener(this);
276
+
277
+ //続けて計算するボタン
278
+
279
+ findViewById(R.id.nextoButton).setOnClickListener(this);
280
+
281
+
282
+
283
+ }
284
+
285
+
286
+
287
+
288
+
289
+ @Override
290
+
291
+ public void onClick(View v){
292
+
293
+ //タップされた時の処理を実装する
294
+
295
+ int id=v.getId();
296
+
297
+
298
+
299
+ //IDごとに違う
300
+
301
+ switch(id){
302
+
303
+ case R.id.calcButton1:
304
+
305
+ //上の計算ボタンかが押された時の処理
306
+
307
+
308
+
309
+ Intent intent1=new Intent(this,AnotherCalcActivity.class);
310
+
311
+ startActivityForResult(intent1,REQUEST_CODE_ANOTHER_CALC_1);
312
+
313
+ break;
314
+
315
+ case R.id.calcButton2:
316
+
317
+ //下の計算ボタンが押された時の処理
318
+
319
+
320
+
321
+ Intent intent2=new Intent(this,AnotherCalcActivity.class);
322
+
323
+ startActivityForResult(intent2,REQUEST_CODE_ANOTHER_CALC_2);
324
+
325
+ break;
326
+
327
+ case R.id.nextButton:
328
+
329
+ //続けて計算するボタンが押された時の処理
330
+
331
+ //両方のEditTextに値が設定されていれば、処理を行う
332
+
333
+ if(checkEditTextInput()){
334
+
335
+ //計算する
336
+
337
+ int result=calc();
338
+
339
+ //上のEditTextの値を書き換える
340
+
341
+ numberInput1.setText(String.valueOf(result));
342
+
343
+ //計算し直して、画面を更新する
344
+
345
+ refreshResult();
346
+
347
+ }
348
+
349
+
350
+
351
+ break;
352
+
353
+ }
354
+
355
+
356
+
357
+
358
+
359
+ }
360
+
361
+
362
+
363
+ @Override
364
+
365
+ protected void onActivityResult(int requestCode,int resultCode,Intent data){
366
+
367
+ //startActivityResult()から戻ってきた時に呼ばれる
368
+
369
+ super.onActivityResult(requestCode,resultCode,data);
370
+
371
+
372
+
373
+ //結果がOKではない場合は何もしない
374
+
375
+ if(resultCode !=RESULT_OK)return;
376
+
377
+
378
+
379
+ //結果データセットを取り出す
380
+
381
+ Bundle resultBundle=data.getExtras();
382
+
383
+
384
+
385
+ //結果データセットに、所定のキーが含まれていない場合、何もしない
386
+
387
+ if(!resultBundle.containsKey("result"))return;
388
+
389
+
390
+
391
+ //結果データから、"result"キーに対応するint値を取り出す。
392
+
393
+ int result=resultBundle.getInt("result");
394
+
395
+
396
+
397
+ if(requestCode == REQUEST_CODE_ANOTHER_CALC_1) {
398
+
399
+ //上の計算ボタンを押した後戻ってきた場合
400
+
401
+ numberInput1.setText(String.valueOf(result));
402
+
403
+
404
+
405
+ }else if(requestCode==REQUEST_CODE_ANOTHER_CALC_2){
406
+
407
+ //下の計算ボタンを押した後戻ってきた場合
408
+
409
+ numberInput2.setText(String.valueOf(result));
410
+
411
+ }
412
+
413
+
414
+
415
+ //計算をしなおして、結果を表示する
416
+
417
+ refreshResult();
418
+
419
+ }
420
+
421
+
422
+
423
+
424
+
425
+ //2つのEditTextに入力がされているかをチェックする
426
+
427
+ private boolean checkEditTextInput(){
428
+
429
+ //入力内容を取得する
430
+
431
+ String input1=numberInput1.getText().toString();
432
+
433
+ String input2=numberInput2.getText().toString();
434
+
435
+
436
+
437
+ //2つともから文字列(あるいはnull)でなければ、true
438
+
439
+ return !TextUtils.isEmpty(input1)&& !TextUtils.isEmpty(input2);
440
+
441
+ }
442
+
443
+
444
+
445
+ @Override
446
+
447
+ public void beforeTextChanged(CharSequence s,int start,int count,int after){
448
+
449
+ //テキストが変更される直前に呼ばれる。Sは変更前の内容
450
+
451
+
452
+
453
+ }
454
+
455
+
456
+
457
+ @Override
458
+
459
+ public void onTextChanged(CharSequence s,int start,int before,int count){
460
+
461
+ //テキストが変更される時に呼ばれる。sは変更後の内容で編集不可
462
+
463
+ }
464
+
465
+
466
+
467
+ @Override
468
+
469
+ public void afterTextChanged(Editable s){
470
+
471
+ //テキストが変更された後に呼ばれる。sは変更後の内容で編集可能
472
+
473
+ //必要があれば、計算を行い、結果を表示する
474
+
475
+ refreshResult();
476
+
477
+
478
+
479
+ }
480
+
481
+
482
+
483
+ //計算結果の表示を更新する
484
+
485
+ private void refreshResult(){
486
+
487
+ if(checkEditTextInput()) {
488
+
489
+ //計算を行う
490
+
491
+ int result = calc();
492
+
493
+
494
+
495
+ //計算結果用のTextVieを書き換える
496
+
497
+ String resultText = getString(R.string.calc_result_text, result);
498
+
499
+ calcResult.setText(resultText);
500
+
501
+
502
+
503
+ }else{
504
+
505
+ //どちらかが入力されていない状態の場合、計算結果用の表示をデフォルトに戻す
506
+
507
+ calcResult.setText(R.string.calc_result_default);
508
+
509
+ }
510
+
511
+ }
512
+
513
+
514
+
515
+
516
+
517
+ //計算を行う
518
+
519
+ private int calc(){
520
+
521
+ //入力内容を取得する
522
+
523
+ String input1=numberInput1.getText().toString();
524
+
525
+ String input2=numberInput2.getText().toString();
526
+
527
+
528
+
529
+ //int型に変換する
530
+
531
+ int number1=Integer.parseInt(input1);
532
+
533
+ int number2=Integer.parseInt(input2);
534
+
535
+
536
+
537
+ //Spinnerから、洗濯中のindexを取得する
538
+
539
+ int operator=operatorSelector.getSelectedItemPosition();
540
+
541
+
542
+
543
+ //indexに応じて計算結果を返す
544
+
545
+ switch(operator){
546
+
547
+ case 0://足し算
548
+
549
+ return number1+number2;
550
+
551
+ case 1://引き算
552
+
553
+ return number1-number2;
554
+
555
+ case 2://掛け算
556
+
557
+ return number1*number2;
558
+
559
+ case 3://割り算
560
+
561
+ return number1/number2;
562
+
563
+ default:
564
+
565
+ //通常発生しない
566
+
567
+ throw new RuntimeException();
568
+
569
+ }
570
+
571
+ }
572
+
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+ }
582
+
583
+
584
+
585
+ ```
586
+
587
+ どうぞよろしくお願いします。

6

誤字

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
 
40
40
 
41
- エラーが沢山出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
41
+ エラーが5つ出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
42
42
 
43
43
  ```java
44
44
 

5

コードのつい亜

2016/12/02 04:40

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -96,7 +96,7 @@
96
96
 
97
97
  ```
98
98
 
99
-
99
+ 上のdebug/Manifest.mlxのエラー部分だけをカーソルを合わせた時に出てくるエラーとセットでまとめました。
100
100
 
101
101
  ```java
102
102
 
@@ -110,27 +110,23 @@
110
110
 
111
111
  //これには、Attribute android:versionCode is not allowed hereがでます。
112
112
 
113
+
114
+
113
115
  android:versionName="1.0" >
114
116
 
115
117
  //これには、Attribute android:versionName is not allowed hereがでます。
116
118
 
117
119
 
118
120
 
119
-
120
-
121
- android:allowBackup="true"
121
+ android:allowBackup="true"
122
122
 
123
123
  //これには、Attribute android:allowBackup is not allowed hereがでます。
124
124
 
125
-
126
-
127
- android:icon="@mipmap/ic_launcher"
125
+ android:icon="@mipmap/ic_launcher"
128
126
 
129
127
  //これには、Attribute android:icon is not allowed hereがでます。
130
128
 
131
129
 
132
-
133
-
134
130
 
135
131
  android:supportsRtl="true"
136
132
 

4

修正

2016/12/02 04:39

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -96,6 +96,8 @@
96
96
 
97
97
  ```
98
98
 
99
+
100
+
99
101
  ```java
100
102
 
101
103
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
@@ -140,4 +142,4 @@
140
142
 
141
143
  //これには、Attribute android:theme is not allowed hereがでます。
142
144
 
143
- ```
145
+ ```

3

エラーの追加

2016/12/02 04:37

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,8 +1,4 @@
1
- ```ここに言語を入力
2
-
3
- コード
4
-
5
- ```setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
1
+ setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
6
2
 
7
3
 
8
4
 

2

エラーの追加

2016/12/02 04:37

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,8 @@
1
+ ```ここに言語を入力
2
+
3
+ コード
4
+
1
- setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
5
+ ```setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
2
6
 
3
7
 
4
8
 
@@ -95,3 +99,49 @@
95
99
  </manifest>
96
100
 
97
101
  ```
102
+
103
+ ```java
104
+
105
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
106
+
107
+ //ここには、URI is not registered(Settings|Project Settings|Schemas and DTDsがでます。
108
+
109
+
110
+
111
+ android:versionCode="1"
112
+
113
+ //これには、Attribute android:versionCode is not allowed hereがでます。
114
+
115
+ android:versionName="1.0" >
116
+
117
+ //これには、Attribute android:versionName is not allowed hereがでます。
118
+
119
+
120
+
121
+
122
+
123
+ android:allowBackup="true"
124
+
125
+ //これには、Attribute android:allowBackup is not allowed hereがでます。
126
+
127
+
128
+
129
+ android:icon="@mipmap/ic_launcher"
130
+
131
+ //これには、Attribute android:icon is not allowed hereがでます。
132
+
133
+
134
+
135
+
136
+
137
+ android:supportsRtl="true"
138
+
139
+ //これには、Attribute android:supportsRtl is not allowed hereがでます。
140
+
141
+
142
+
143
+ android:theme="@style/AppTheme" >
144
+
145
+ //これには、Attribute android:theme is not allowed hereがでます。
146
+
147
+ ```

1

コードの追加

2016/12/02 04:36

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -35,3 +35,63 @@
35
35
  ```
36
36
 
37
37
  どうすれば良いのでしょうか? 教えていただけないでしょうか?
38
+
39
+
40
+
41
+ エラーが沢山出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
42
+
43
+ ```java
44
+
45
+ <?xml version="1.0" encoding="utf-8"?>
46
+
47
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
48
+
49
+ package="com.example.android.sample.calculator"
50
+
51
+ android:versionCode="1"
52
+
53
+ android:versionName="1.0" >
54
+
55
+
56
+
57
+ <uses-sdk
58
+
59
+ android:minSdkVersion="23"
60
+
61
+ android:targetSdkVersion="24" />
62
+
63
+
64
+
65
+ <application
66
+
67
+ android:allowBackup="true"
68
+
69
+ android:icon="@mipmap/ic_launcher"
70
+
71
+ android:label="@string/app_name"
72
+
73
+ android:supportsRtl="true"
74
+
75
+ android:theme="@style/AppTheme" >
76
+
77
+ <activity android:name="com.example.android.sample.calculator.MainActivity" >
78
+
79
+ <intent-filter>
80
+
81
+ <action android:name="android.intent.action.MAIN" />
82
+
83
+
84
+
85
+ <category android:name="android.intent.category.LAUNCHER" />
86
+
87
+ </intent-filter>
88
+
89
+ </activity>
90
+
91
+ </application>
92
+
93
+
94
+
95
+ </manifest>
96
+
97
+ ```