[安卓] 1、页面跳转+按钮监听

本文介绍了一个简单的Android应用程序示例,展示了如何通过按钮点击从一个活动(Activity)跳转到另一个活动,并提供了MainActivity与OtherActivity的具体实现代码。


 

 

共2个layout:main.xml和other.xml;2个activity:MainActivity.java,OtherActivity.java


 

在mainactivity中重写onCreate,第6行设置按钮监听~

复制代码
1 public void onCreate(Bundle savedInstanceState) {
2     super.onCreate(savedInstanceState);
3     setContentView(R.layout.main);
4     Log.v("MainActivity", "onCreate");
5     btn = (Button) findViewById(R.id.Main_btn);
6     btn.setOnClickListener(this);
7     //this.finish(); //结束当前MainActivity
8 }
复制代码

下面是对按钮监听的实现:即当按钮被按下时,跳转到另一页面:

复制代码
1 @Override
2 public void onClick(View arg0) {
3     if (arg0 == btn) {
4         Intent intent = new Intent();
5         intent.setClass(this, OtherActivity.class);
6         this.startActivity(intent);
7     }
8 }
复制代码

 


另一个activity也类似:

复制代码
1 public void onCreate(Bundle savedInstanceState) {
2     super.onCreate(savedInstanceState);
3     setContentView(R.layout.other);
4     Log.v("MainActivity", "onCreate");
5     btn = (Button) findViewById(R.id.Other_btn);
6     btn.setOnClickListener(this);
7 }
复制代码
1 public void onClick(View arg0) {
2     if (arg0 == btn) {
3         this.finish();
4     }
5 }

 


有人会觉得OtherActivity为什么是那个样子的,暂时我也不清楚,先把他的xml文件贴下:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <TextView  
 8     android:layout_width="fill_parent" 
 9     android:layout_height="wrap_content" 
10     android:text="@string/OtherActiviy_hello"
11     />
12 <Button   
13     android:layout_width="fill_parent" 
14     android:layout_height="wrap_content" 
15     android:text="@string/OtherActiviy_BtnClose"
16     android:id="@+id/Other_btn"
17     />
18 </LinearLayout>
复制代码

 

 

 

 

本文链接:http://www.cnblogs.com/zjutlitao/p/4229540.html

更多精彩:http://www.cnblogs.com/zjutlitao/



本文转自beautifulzzzz博客园博客,原文链接:http://www.cnblogs.com/zjutlitao/p/4229540.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值