標籤:
共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(); //結束當前MainActivity8 }
下面是對按鈕監聽的實現:即當按鈕被按下時,跳轉到另一頁面:
1 @Override2 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/
[安卓] 1、頁面跳轉+按鈕監聽