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

 

 

共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/

 

posted @ 2015-01-16 18:52  beautifulzzzz  阅读(2106)  评论(0编辑  收藏  举报