import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); }});
import后面的包在系统报错后点小灯泡会自动给你。
注意大小写。注意不要重名。名字尽量按实际意义
在Android 应用开发过程中,常用监听事件如下:
(1) ListView事件监听 @H_403_13@ setOn ItemSelectedListener:鼠标滚动时触发 @H_403_13@ setOnItemClickListener: 点击时触发
(2 )EditText 事件监听 @H_403_13@ setOnKeyListener: 获取焦点时触发
(3) RadioGroup事件监听 @H_403_13@ setOnCheckedChangeListener: 点击时触发
(4) CheckBox事件监听 @H_403_13@ setOnCheckedChangeListener :点击时触发
(5 )Spinner事件监听 @H_403_13@ setOnltemSelectedListener:点击时触发
(6) DatePicker 事件监听
onDateChangedListener: 日期改变时触发
(7 )DatePickerDialog 事件监听 @H_403_13@ onDateSetListener:设置日期时触发
(8) TimePicker 事件监听 @H_403_13@ onTimeChangedListener:时间改变时触发
(9 )TimePickerDialog 事件监听 @H_403_13@ onTimeSetListener: 设置时间时触发
(10)Button.ImageButton 事件监听 @H_403_13@ setOnClickListener: 点击时触发
(11)Menu 事件监听 @H_403_13@ onOptionsltemSelected; 点击时触发
(12 )Gallery 事件监听 @H_403_13@ setOnItemClickListener: 点击时触发
(13 )GridView 事件监听 @H_403_13@ setOnltemClickListener: 点击时触发