[安卓] 1、頁面跳轉+按鈕監聽

來源:互聯網
上載者:User

標籤:

 

 

共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、頁面跳轉+按鈕監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.