方法一:通过SetContentView切换Layout来实现界面的切换,这种方法相当于重绘Activity.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnInsert = (Button) this.findViewById(R.id.btnInsert); //获取btn btnInsert.setOnClickListener(new View.OnClickListener() { //添加监听器 @Override public void onClick(View v) { setContentView(R.layout.activity_insert); //跳转 } }); }
方法二:在一个程序中使用Intent对象来指定一个Activity,并通过startActivity方法启动这个Activity.
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnInsert = (Button) this.findViewById(R.id.btnInsert); //获取按钮 btnInsert.setOnClickListener(new View.OnClickListener() { //添加监听器 @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, InsertActivity.class); //设置Intent属性 MainActivity.this.startActivity(intent); //跳转 } }); }
跳转实例:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnInsert = (Button) this.findViewById(R.id.btnInsert); //获取Button Button btnDelete = (Button) this.findViewById(R.id.btnDelete); Button btnSelect = (Button) this.findViewById(R.id.btnSelect); Button btnMOdify = (Button) this.findViewById(R.id.btnModify); ButtonListener buttonListener = new ButtonListener(); //绑定Button单击事件 btnInsert.setOnClickListener(buttonListener); btnDelete.setOnClickListener(buttonListener); btnMOdify.setOnClickListener(buttonListener); btnSelect.setOnClickListener(buttonListener); } //设置Button跳转 class ButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { int id = v.getId(); Intent intent = new Intent(); switch (id){ case R.id.btnInsert: intent.setClass(MainActivity.this, InsertActivity.class); MainActivity.this.startActivity(intent); break; case R.id.btnDelete: intent.setClass(MainActivity.this, DeleteActivity.class); MainActivity.this.startActivity(intent); break; case R.id.btnModify: intent.setClass(MainActivity.this, ModifyActivity.class); MainActivity.this.startActivity(intent); break; case R.id.btnSelect: intent.setClass(MainActivity.this, SelectActivity.class); MainActivity.this.startActivity(intent); break; } } }
来源:https://www.cnblogs.com/bincoding/p/4975990.html