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

質問編集履歴

8

誤字

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -70,7 +70,8 @@
70
70
  //これには、Attribute android:theme is not allowed hereがでます。
71
71
  ```
72
72
 
73
- MainActivityです。```java
73
+ MainActivityです。
74
+ ```java
74
75
  package com.example.android.sample.calculator;
75
76
 
76
77
  import android.content.Intent;

7

コードの追加

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -68,4 +68,227 @@
68
68
 
69
69
  android:theme="@style/AppTheme" >
70
70
  //これには、Attribute android:theme is not allowed hereがでます。
71
- ```
71
+ ```
72
+
73
+ MainActivityです。```java
74
+ package com.example.android.sample.calculator;
75
+
76
+ import android.content.Intent;
77
+ import android.support.v7.app.AppCompatActivity;
78
+ import android.os.Bundle;
79
+ import android.text.Editable;
80
+ import android.text.TextUtils;
81
+ import android.text.TextWatcher;
82
+ import android.view.View;
83
+ import android.widget.EditText;
84
+ import android.widget.Spinner;
85
+ import android.widget.TextView;
86
+
87
+ public class MainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener{
88
+
89
+ //上の計算ボタンを押した時のリクエストコード
90
+ private static final int REQUEST_CODE_ANOTHER_CALC_1=1;
91
+ //上の計算ボタンを押した時のリクエストコード
92
+ private static final int REQUEST_CODE_ANOTHER_CALC_2=2;
93
+
94
+
95
+
96
+ //上のEditText
97
+ private EditText numberInput1;
98
+ //下のEditText
99
+ private EditText numberInput2;
100
+
101
+
102
+ //演算子選択用のSpinner
103
+ private Spinner operatorSelector;
104
+
105
+ //計算結果表示用のTextView
106
+ private TextView calcResult;
107
+
108
+ @Override
109
+ protected void onCreate(Bundle savedInstanceState) {
110
+ super.onCreate(savedInstanceState);
111
+ setContentView(R.layout.activity_main);
112
+
113
+
114
+ //上のEditText
115
+ numberInput1=(EditText)findViewById(R.id.numberInput);
116
+ //上のEditTextの文字入力イベントを受け取る
117
+ numberInput1.addTextChangedListener(this);
118
+
119
+ //下のEditText
120
+ numberInput2=(EditText)findViewById(R.id.numberInput2);
121
+
122
+ //下のEditTextの文字入力を受け取る
123
+ numberInput2.addTextChangedListener(this);
124
+
125
+
126
+
127
+
128
+ //演算子選択用のSpinner
129
+ operatorSelector=(Spinner)findViewById(R.id.operatorSelector);
130
+
131
+ //計算結果表示用のTextView
132
+ calcResult=(TextView)findViewById(R.id.calcResult);
133
+
134
+
135
+ //上の計算ボタン
136
+ findViewById(R.id.calcButton1).setOnClickListener(this);
137
+ //下の計算ボタン
138
+ findViewById(R.id.calcButton2).setOnClickListener(this);
139
+ //続けて計算するボタン
140
+ findViewById(R.id.nextoButton).setOnClickListener(this);
141
+
142
+ }
143
+
144
+
145
+ @Override
146
+ public void onClick(View v){
147
+ //タップされた時の処理を実装する
148
+ int id=v.getId();
149
+
150
+ //IDごとに違う
151
+ switch(id){
152
+ case R.id.calcButton1:
153
+ //上の計算ボタンかが押された時の処理
154
+
155
+ Intent intent1=new Intent(this,AnotherCalcActivity.class);
156
+ startActivityForResult(intent1,REQUEST_CODE_ANOTHER_CALC_1);
157
+ break;
158
+ case R.id.calcButton2:
159
+ //下の計算ボタンが押された時の処理
160
+
161
+ Intent intent2=new Intent(this,AnotherCalcActivity.class);
162
+ startActivityForResult(intent2,REQUEST_CODE_ANOTHER_CALC_2);
163
+ break;
164
+ case R.id.nextButton:
165
+ //続けて計算するボタンが押された時の処理
166
+ //両方のEditTextに値が設定されていれば、処理を行う
167
+ if(checkEditTextInput()){
168
+ //計算する
169
+ int result=calc();
170
+ //上のEditTextの値を書き換える
171
+ numberInput1.setText(String.valueOf(result));
172
+ //計算し直して、画面を更新する
173
+ refreshResult();
174
+ }
175
+
176
+ break;
177
+ }
178
+
179
+
180
+ }
181
+
182
+ @Override
183
+ protected void onActivityResult(int requestCode,int resultCode,Intent data){
184
+ //startActivityResult()から戻ってきた時に呼ばれる
185
+ super.onActivityResult(requestCode,resultCode,data);
186
+
187
+ //結果がOKではない場合は何もしない
188
+ if(resultCode !=RESULT_OK)return;
189
+
190
+ //結果データセットを取り出す
191
+ Bundle resultBundle=data.getExtras();
192
+
193
+ //結果データセットに、所定のキーが含まれていない場合、何もしない
194
+ if(!resultBundle.containsKey("result"))return;
195
+
196
+ //結果データから、"result"キーに対応するint値を取り出す。
197
+ int result=resultBundle.getInt("result");
198
+
199
+ if(requestCode == REQUEST_CODE_ANOTHER_CALC_1) {
200
+ //上の計算ボタンを押した後戻ってきた場合
201
+ numberInput1.setText(String.valueOf(result));
202
+
203
+ }else if(requestCode==REQUEST_CODE_ANOTHER_CALC_2){
204
+ //下の計算ボタンを押した後戻ってきた場合
205
+ numberInput2.setText(String.valueOf(result));
206
+ }
207
+
208
+ //計算をしなおして、結果を表示する
209
+ refreshResult();
210
+ }
211
+
212
+
213
+ //2つのEditTextに入力がされているかをチェックする
214
+ private boolean checkEditTextInput(){
215
+ //入力内容を取得する
216
+ String input1=numberInput1.getText().toString();
217
+ String input2=numberInput2.getText().toString();
218
+
219
+ //2つともから文字列(あるいはnull)でなければ、true
220
+ return !TextUtils.isEmpty(input1)&& !TextUtils.isEmpty(input2);
221
+ }
222
+
223
+ @Override
224
+ public void beforeTextChanged(CharSequence s,int start,int count,int after){
225
+ //テキストが変更される直前に呼ばれる。Sは変更前の内容
226
+
227
+ }
228
+
229
+ @Override
230
+ public void onTextChanged(CharSequence s,int start,int before,int count){
231
+ //テキストが変更される時に呼ばれる。sは変更後の内容で編集不可
232
+ }
233
+
234
+ @Override
235
+ public void afterTextChanged(Editable s){
236
+ //テキストが変更された後に呼ばれる。sは変更後の内容で編集可能
237
+ //必要があれば、計算を行い、結果を表示する
238
+ refreshResult();
239
+
240
+ }
241
+
242
+ //計算結果の表示を更新する
243
+ private void refreshResult(){
244
+ if(checkEditTextInput()) {
245
+ //計算を行う
246
+ int result = calc();
247
+
248
+ //計算結果用のTextVieを書き換える
249
+ String resultText = getString(R.string.calc_result_text, result);
250
+ calcResult.setText(resultText);
251
+
252
+ }else{
253
+ //どちらかが入力されていない状態の場合、計算結果用の表示をデフォルトに戻す
254
+ calcResult.setText(R.string.calc_result_default);
255
+ }
256
+ }
257
+
258
+
259
+ //計算を行う
260
+ private int calc(){
261
+ //入力内容を取得する
262
+ String input1=numberInput1.getText().toString();
263
+ String input2=numberInput2.getText().toString();
264
+
265
+ //int型に変換する
266
+ int number1=Integer.parseInt(input1);
267
+ int number2=Integer.parseInt(input2);
268
+
269
+ //Spinnerから、洗濯中のindexを取得する
270
+ int operator=operatorSelector.getSelectedItemPosition();
271
+
272
+ //indexに応じて計算結果を返す
273
+ switch(operator){
274
+ case 0://足し算
275
+ return number1+number2;
276
+ case 1://引き算
277
+ return number1-number2;
278
+ case 2://掛け算
279
+ return number1*number2;
280
+ case 3://割り算
281
+ return number1/number2;
282
+ default:
283
+ //通常発生しない
284
+ throw new RuntimeException();
285
+ }
286
+ }
287
+
288
+
289
+
290
+
291
+ }
292
+
293
+ ```
294
+ どうぞよろしくお願いします。

6

誤字

2016/12/04 09:25

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,7 @@
18
18
  ```
19
19
  どうすれば良いのでしょうか? 教えていただけないでしょうか?
20
20
 
21
- エラーが沢山出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
21
+ エラーが5つ出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
22
22
  ```java
23
23
  <?xml version="1.0" encoding="utf-8"?>
24
24
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"

5

コードのつい亜

2016/12/02 04:40

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -47,24 +47,22 @@
47
47
 
48
48
  </manifest>
49
49
  ```
50
-
50
+ 上のdebug/Manifest.mlxのエラー部分だけをカーソルを合わせた時に出てくるエラーとセットでまとめました。
51
51
  ```java
52
52
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
53
53
  //ここには、URI is not registered(Settings|Project Settings|Schemas and DTDsがでます。
54
54
 
55
55
  android:versionCode="1"
56
56
  //これには、Attribute android:versionCode is not allowed hereがでます。
57
+
57
58
  android:versionName="1.0" >
58
59
  //これには、Attribute android:versionName is not allowed hereがでます。
59
60
 
60
-
61
- android:allowBackup="true"
61
+ android:allowBackup="true"
62
62
  //これには、Attribute android:allowBackup is not allowed hereがでます。
63
-
64
- android:icon="@mipmap/ic_launcher"
63
+ android:icon="@mipmap/ic_launcher"
65
64
  //これには、Attribute android:icon is not allowed hereがでます。
66
65
 
67
-
68
66
  android:supportsRtl="true"
69
67
  //これには、Attribute android:supportsRtl is not allowed hereがでます。
70
68
 

4

修正

2016/12/02 04:39

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -47,6 +47,7 @@
47
47
 
48
48
  </manifest>
49
49
  ```
50
+
50
51
  ```java
51
52
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
52
53
  //ここには、URI is not registered(Settings|Project Settings|Schemas and DTDsがでます。
@@ -69,4 +70,4 @@
69
70
 
70
71
  android:theme="@style/AppTheme" >
71
72
  //これには、Attribute android:theme is not allowed hereがでます。
72
- ```
73
+ ```

3

エラーの追加

2016/12/02 04:37

投稿

edoooooo
edoooooo

スコア478

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

2

エラーの追加

2016/12/02 04:37

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,6 @@
1
+ ```ここに言語を入力
2
+ コード
1
- setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
3
+ ```setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
2
4
 
3
5
 
4
6
  ```java
@@ -46,4 +48,27 @@
46
48
  </application>
47
49
 
48
50
  </manifest>
49
- ```
51
+ ```
52
+ ```java
53
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
54
+ //ここには、URI is not registered(Settings|Project Settings|Schemas and DTDsがでます。
55
+
56
+ android:versionCode="1"
57
+ //これには、Attribute android:versionCode is not allowed hereがでます。
58
+ android:versionName="1.0" >
59
+ //これには、Attribute android:versionName is not allowed hereがでます。
60
+
61
+
62
+ android:allowBackup="true"
63
+ //これには、Attribute android:allowBackup is not allowed hereがでます。
64
+
65
+ android:icon="@mipmap/ic_launcher"
66
+ //これには、Attribute android:icon is not allowed hereがでます。
67
+
68
+
69
+ android:supportsRtl="true"
70
+ //これには、Attribute android:supportsRtl is not allowed hereがでます。
71
+
72
+ android:theme="@style/AppTheme" >
73
+ //これには、Attribute android:theme is not allowed hereがでます。
74
+ ```

1

コードの追加

2016/12/02 04:36

投稿

edoooooo
edoooooo

スコア478

title CHANGED
File without changes
body CHANGED
@@ -16,4 +16,34 @@
16
16
  }
17
17
  }
18
18
  ```
19
- どうすれば良いのでしょうか? 教えていただけないでしょうか?
19
+ どうすれば良いのでしょうか? 教えていただけないでしょうか?
20
+
21
+ エラーが沢山出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
22
+ ```java
23
+ <?xml version="1.0" encoding="utf-8"?>
24
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
25
+ package="com.example.android.sample.calculator"
26
+ android:versionCode="1"
27
+ android:versionName="1.0" >
28
+
29
+ <uses-sdk
30
+ android:minSdkVersion="23"
31
+ android:targetSdkVersion="24" />
32
+
33
+ <application
34
+ android:allowBackup="true"
35
+ android:icon="@mipmap/ic_launcher"
36
+ android:label="@string/app_name"
37
+ android:supportsRtl="true"
38
+ android:theme="@style/AppTheme" >
39
+ <activity android:name="com.example.android.sample.calculator.MainActivity" >
40
+ <intent-filter>
41
+ <action android:name="android.intent.action.MAIN" />
42
+
43
+ <category android:name="android.intent.category.LAUNCHER" />
44
+ </intent-filter>
45
+ </activity>
46
+ </application>
47
+
48
+ </manifest>
49
+ ```