アクティビティにボタンのグループがあります。ボタンをクリックすると、テキストビューにテキストが設定され、ボタンの背景色が変更されます。次に同じボタンを押すと次のアクティビティに移動します。以下のコードではこれらの操作を行います。ボタンのテキストを変更して、2回目のクリック操作を行いました。テキストを変更することなくそれを行うより良い方法がありますか?2つのクリックアクションを行う1つのボタンを作成する
私は1つのボタンのために示しているコードです。同じように私は他のボタンのためにそれを行います。
活性
public class Activity2 extends ActionBarActivity implements View.OnClickListener {
Button R1btn, R2btn, R3btn;
private TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
tableLayout=(TableLayout)findViewById(R.id.tl);
R1btn = (Button) findViewById(R.id.button1);
R1btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
Button button1 = (Button)view;
String btnText = button1.getText().toString();
if (btnText.equals("1")) {
R1btn.setBackgroundColor(Color.RED);
R2btn.setBackgroundColor(Color.GREEN));
R3btn.setBackgroundColor(Color.GREEN));
TextView rTV = (TextView) findViewById(R.id.rTV);
rTV.setText(R1);
button1.setText("1 ");
} else if (btnText.equals("1 ")) {
Intent intent = new Intent(Activity2.this, Activity3.class);
startActivity(intent);
button1.setText("1");
break;
}
}
}
各ボタンにブール変数を使用し、その値に応じてアクションを変更します。 –