不知什么原因,我的纽扣什么都没做。 我以前使用过这个方法来实现按钮,它从来没有给我带来任何问题。 这款应用有七个不同的按钮,它们都可以移动到不同的activity。
public class ScheduleActivity extends AppCompatActivity implements View.OnClickListener {
private Button mondayButton,tuesdayButton,wednesdayButton,thursdayButton,fridayButton,saturdayButton,sundayButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
mondayButton = findViewById(R.id.monday_button);
tuesdayButton = findViewById(R.id.tuesday_button);
wednesdayButton = findViewById(R.id.wednesday_button);
thursdayButton = findViewById(R.id.thursday_button);
fridayButton = findViewById(R.id.friday_button);
saturdayButton = findViewById(R.id.saturday_button);
sundayButton = findViewById(R.id.sunday_button);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.monday_button:
Intent monday_intent = new Intent(ScheduleActivity.this, MondayActivity.class);
startActivity(monday_intent);
break;
case R.id.tuesday_button:
Intent tuesday_intent = new Intent(ScheduleActivity.this, TuesdayActivity.class);
startActivity(tuesday_intent);
break;
case R.id.wednesday_button:
Intent wednesday_intent = new Intent(ScheduleActivity.this, WednesdayActivity.class);
startActivity(wednesday_intent);
break;
case R.id.thursday_button:
Intent thursday_intent = new Intent(ScheduleActivity.this, ThursdayActivity.class);
startActivity(thursday_intent);
break;
case R.id.friday_button:
Intent friday_intent = new Intent(ScheduleActivity.this, FridayActivity.class);
startActivity(friday_intent);
break;
case R.id.saturday_button:
Intent saturday_intent = new Intent(ScheduleActivity.this, SaturdayActivity.class);
startActivity(saturday_intent);
case R.id.sunday_button:
Intent sunday_intent = new Intent(ScheduleActivity.this, SundayActivity.class);
startActivity(sunday_intent);
}
}
}
您正在获取按钮的实例,但从未为它们设置OnClickListener。 您需要为按钮设置click listener:
mondayButton.setOnClickListener(this)
您需要对所有按钮执行此操作,这告诉您的代码在单击按钮时在何处通知事件。
您没有将侦听器View.OnClickListener
附加到任何按钮。 在您的oncreate()
中添加此选项后,您的按钮将工作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
...
sundayButton = findViewById(R.id.sunday_button);
// attaching listeners
mondayButton.setOnClickListener(this);
tuesdayButton.setOnClickListener(this);
wednesdayButton.setOnClickListener(this);
thursdayButton.setOnClickListener(this);
fridayButton.setOnClickListener(this);
saturdayButton.setOnClickListener(this);
sundayButton.setOnClickListener(this);
}
必须在OnCreate方法上设置view.setOnClickListener{}
私人按钮星期一按钮,星期二按钮,星期三按钮,星期四按钮,星期五按钮,星期六按钮,星期日按钮;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedule);
mondayButton = findViewById(R.id.monday_button);
tuesdayButton = findViewById(R.id.tuesday_button);
wednesdayButton = findViewById(R.id.wednesday_button);
thursdayButton = findViewById(R.id.thursday_button);
fridayButton = findViewById(R.id.friday_button);
saturdayButton = findViewById(R.id.saturday_button);
sundayButton = findViewById(R.id.sunday_button);
mondayButton.setOnClickListener() {
Intent intent = new Intent(...) ;
startActivity(intent) ;
}
}