Android学习手记(1) Activity跳转

  1. 新建Project,并将主页命名为MainActivity。

  2. 创建一个Activity
    在App上“右键->New->Activity->Empty Activity”, 将新建的Activity命名为AnotherAty。

  3. 在MainActivity上添加一个按钮,并设置 id 和 text 属性
    打开”activity_main.xml”, 添加如下代码:

1 <Button
2         android:layout_width="wrap_content"
3         android:layout_height="wrap_content"
4         android:text="Another Activity"
5         android:id="@+id/btnStartAnotherAty"
6         android:layout_below="@+id/textView"
7         android:layout_alignParentLeft="true"
8         android:layout_alignParentStart="true" />

  4.  为按钮添加行为
       打开“MainActivity.java”,在OnCreate方法内添加:

1 findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {
2             @Override
3             public void onClick(View v) {
4                 startActivity(new Intent(MainActivity.this,AnotherAty.class));
5             }
6 });

这是完整代码:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         findViewById(R.id.btnStartAnotherAty).setOnClickListener(new View.OnClickListener() {
 9             @Override
10             public void onClick(View v) {
11                 startActivity(new Intent(MainActivity.this,AnotherAty.class));
12             }
13         });
14     }
15 }
这样就实现了单击MainActivity中的Button跳转到AnotherAty的功能。

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/doodle777/p/Android_Activity_Jump.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值