###前提・実現したいこと
初心者です。
androidstudioでswitchボタンをONにすると設定されていません。から設定されています。にかわるシステムを作っています。
それで、条件分岐であるswitch文を使ってみたのですが、実行時、case1のswitchボタンをONにするとcase1とcase2が同時に設定されています。に変わってしまいます。
僕はそれをcase1の部分だけを設定されています。に変わるようにしたいです。
説明が下手ですいません。
###該当のソースコード
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import static to.msn.wings.boshitetyo.R.id.textView1;
public class MainActivity extends Activity implements OnCheckedChangeListener {
private TextView textView[] = new TextView[5];
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView[0] = (TextView) findViewById(textView1); Switch switch1 = (Switch) findViewById(R.id.switch1); switch1.setOnCheckedChangeListener(this); textView[1] = (TextView) findViewById(R.id.textView2); Switch switch2 = (Switch) findViewById(R.id.switch2); switch2.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ for(int textViewNum = 1; textViewNum < 6; textViewNum++){ switch (textViewNum) { case 1: if (isChecked) { textView[0].setText("設定中です。"); Toast.makeText(MainActivity.this, "ONにしました。", Toast.LENGTH_SHORT).show(); } else { textView[0].setText("設定されていません。"); Toast.makeText(MainActivity.this, "OFFにしました。", Toast.LENGTH_SHORT).show(); } break; case 2: if (isChecked) { textView[1].setText("設定中です。"); Toast.makeText(MainActivity.this, "ONにしました。", Toast.LENGTH_SHORT).show(); } else { textView[1].setText("設定されていません。"); Toast.makeText(MainActivity.this, "OFFにしました。", Toast.LENGTH_SHORT).show(); } break; } } }
}
###試したこと
switch文のほかにif文などを使ってみたのですが、やっぱりだめでした。
###補足情報(言語/FW/ツール等のバージョンなど)
一番新しいバージョンを使っています。
回答3件
あなたの回答
tips
プレビュー