Android Studio 上Activity的互相切换

Activity上有两个按钮,切换到其他两个Activity。

1、先创建两个Button,一个Textview

 

 

2、给两个Button添加监听器,实现如下代码

public class MainActivity extends AppCompatActivity {
    Button bt1_2, bt1_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1_2 = (Button) findViewById(R.id.button_t2);
        bt1_3 = (Button) findViewById(R.id.button_t3);
        //创建监听器
        bt1_2.setOnClickListener(listener);
        bt1_3.setOnClickListener(listener2);
    }

    //实现监听器1:按键1,跳到第2张
    Button.OnClickListener listener = new Button.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);
            MainActivity.this.finish();
        }
    };
    //实现监听器2:按键2,跳到第3张
    Button.OnClickListener listener2 = new Button.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Main3Activity.class);
            startActivity(intent);
            MainActivity.this.finish();
        }
    };
}

 

posted @ 2016-07-15 16:07  梦里花开~  阅读(10145)  评论(2编辑  收藏  举报