Android activity跳转方式

我们两清 提交于 2020-03-03 11:57:45

方法一:通过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);        //跳转
            }
        });
    }

image

 

方法二:在一个程序中使用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);    //跳转
            }
        });
    }

image

 

 

跳转实例:

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;
            }
        }
    }
View Code

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!