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

質問編集履歴

2

質問内容

2019/11/01 03:26

投稿

souja
souja

スコア13

title CHANGED
File without changes
body CHANGED
@@ -14,10 +14,13 @@
14
14
 
15
15
  public class nextpage5 extends AppCompatActivity {
16
16
  private TextView Checkview;
17
- private CheckBox[] checkBox = new CheckBox[10];
17
+ private CheckBox[] checkBox = new CheckBox[21];
18
- //チェック数を数えるため//
18
+ int arraycount = 11;//チェック数を数えるため//
19
- int arraycount = 10;
19
+ int checkcount = 0;//チェック数を保持//
20
+ boolean[] checkarray = new boolean[arraycount];//チェック状態を保存する配列checkarray//
20
21
 
22
+
23
+
21
24
  protected void onCreate(Bundle savedInstanceState) {
22
25
  super.onCreate(savedInstanceState);
23
26
  setContentView(R.layout.activity_nextpage5);
@@ -38,15 +41,6 @@
38
41
 
39
42
  Checkview = findViewById(R.id.checkview);
40
43
 
41
- //チェック数を数えるため//
42
- final int arraycount = 10;
43
-
44
- //チェック状態を保存する配列checkarray//
45
- final boolean[] checkarray = new boolean[arraycount];
46
-
47
- //チェック数を保持//
48
- final int checkcount = 0;
49
-
50
44
  //チェックされていたら、checkarrayの要素がtureとなる。//
51
45
  for (int i = 0; i < arraycount; i++) {
52
46
  checkarray[i] = false;
@@ -56,16 +50,15 @@
56
50
  //チェックボタンが押されたときの処理//
57
51
  checkbutton.setOnClickListener(new View.OnClickListener() {
58
52
  public void onClick(View view){
59
- for (int i = 0; i <= 10; i++) {
53
+ for (int i = 0; i < arraycount; i++) {
60
54
  if (true == checkarray[i]) {
61
55
  checkcount++;
62
56
  }
63
57
  }
64
- Checkview.setText(checkcount);
58
+ Checkview.setText(String.valueOf(checkcount));
65
59
  }
66
60
  });
67
61
 
68
-
69
62
  //ボタンが押された時の処理//
70
63
  nextbutton.setOnClickListener(new View.OnClickListener() {
71
64
  public void onClick(View view) {

1

質問内容

2019/11/01 03:26

投稿

souja
souja

スコア13

title CHANGED
@@ -1,1 +1,1 @@
1
- android studioで複数のcheckboxに1つでもチェックが入ればOKを返して、入らなかたらNOTを返すとプログラム
1
+ android studioで複数のcheckboxのうち何個にチェックが入っるか数えるプログラム
body CHANGED
@@ -1,5 +1,5 @@
1
+ 新たな仕様で、10個のうち、何個にチェックが入っているかをボタンを押して表示したいです。
1
- 途中までかいたのでが、よくわからなくなってしいましたOKNOTを返した後に画面遷移を行いたいと考えています。checkboxひとつづつリスナーを呼び出して分岐をるのはあまりいい方法ないと思うのですがい方法思いつかないです。
2
+ エラーfinal変数checkcountに値を代入ることはできせん。と表示されます。ですが、final宣言しないと、final宣言る必要あるとうエラーでます。どたか教えてただきたいです。
2
- どなたか教えていただきたいです。
3
3
  ```java
4
4
  package com.example.soja.page1;
5
5
 
@@ -8,54 +8,72 @@
8
8
  import android.view.View;
9
9
  import android.widget.Button;
10
10
  import android.widget.CheckBox;
11
+ import android.widget.TextView;
11
12
 
12
13
  import androidx.appcompat.app.AppCompatActivity;
13
14
 
14
15
  public class nextpage5 extends AppCompatActivity {
16
+ private TextView Checkview;
17
+ private CheckBox[] checkBox = new CheckBox[10];
18
+ //チェック数を数えるため//
19
+ int arraycount = 10;
15
20
 
16
- private CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7;
17
- private CheckBox checkBox8, checkBox9, checkBox10;
18
-
19
21
  protected void onCreate(Bundle savedInstanceState) {
20
22
  super.onCreate(savedInstanceState);
21
23
  setContentView(R.layout.activity_nextpage5);
22
24
 
23
- checkBox1 = findViewById(R.id.box1);
25
+ checkBox[0] = findViewById(R.id.box1);
24
- checkBox2 = findViewById(R.id.box2);
26
+ checkBox[1] = findViewById(R.id.box2);
25
- checkBox3 = findViewById(R.id.box3);
27
+ checkBox[2] = findViewById(R.id.box3);
26
- checkBox4 = findViewById(R.id.box4);
28
+ checkBox[3] = findViewById(R.id.box4);
27
- checkBox5 = findViewById(R.id.box5);
29
+ checkBox[4] = findViewById(R.id.box5);
28
- checkBox6 = findViewById(R.id.box6);
30
+ checkBox[5] = findViewById(R.id.box6);
29
- checkBox7 = findViewById(R.id.box7);
31
+ checkBox[6] = findViewById(R.id.box7);
30
- checkBox8 = findViewById(R.id.box8);
32
+ checkBox[7] = findViewById(R.id.box8);
31
- checkBox9 = findViewById(R.id.box9);
33
+ checkBox[8] = findViewById(R.id.box9);
32
- checkBox10 = findViewById(R.id.box10);
34
+ checkBox[9] = findViewById(R.id.box10);
33
35
 
34
36
  Button nextbutton = findViewById(R.id.nextbutton);
37
+ Button checkbutton = findViewById(R.id.checkbutton);
35
38
 
36
- ここから先がよくわかりません。
39
+ Checkview = findViewById(R.id.checkview);
37
40
 
41
+ //チェック数を数えるため//
42
+ final int arraycount = 10;
43
+
44
+ //チェック状態を保存する配列checkarray//
45
+ final boolean[] checkarray = new boolean[arraycount];
46
+
47
+ //チェック数を保持//
48
+ final int checkcount = 0;
49
+
50
+ //チェックされていたら、checkarrayの要素がtureとなる。//
51
+ for (int i = 0; i < arraycount; i++) {
52
+ checkarray[i] = false;
53
+ checkarray[i] = checkBox[i].isChecked();
54
+ }
55
+
56
+ //チェックボタンが押されたときの処理//
38
- checkBox1.setOnClickListener(new View.OnClickListener() {
57
+ checkbutton.setOnClickListener(new View.OnClickListener() {
39
- public void onClick(View v) {
58
+ public void onClick(View view){
59
+ for (int i = 0; i <= 10; i++) {
40
- if (checkBox1.isChecked() == true) {
60
+ if (true == checkarray[i]) {
41
-
61
+ checkcount++;
62
+ }
42
63
  }
43
- else {
64
+ Checkview.setText(checkcount);
44
-
45
- }
46
65
  }
47
-
48
66
  });
49
67
 
68
+
50
69
  //ボタンが押された時の処理//
51
70
  nextbutton.setOnClickListener(new View.OnClickListener() {
52
71
  public void onClick(View view) {
53
72
  //インテントの作成//
54
- Intent intent = new Intent(nextpage5.this, nextpage6.class);
73
+ Intent intent = new Intent(nextpage5.this, nextpage1sample.class);
55
74
  startActivity(intent);
56
75
  }
57
- });
76
+ });
58
77
  }
59
78
  }
60
-
61
79
  ```