Android中activity跳轉(zhuǎn)按鈕事件的四種寫法
具體實現(xiàn)代碼:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 方法1. 采用實現(xiàn)OnClickListener接口的類 ((Button) findViewById(R.id.btn1)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ButtonActivity1.class); startActivity(intent); } }); // 方法2. 采用匿名內(nèi)部類 ((Button) findViewById(R.id.btn2)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ButtonActivity2.class); startActivity(intent); } }); // 方法3. Activity直接實現(xiàn)OnClickListener接口 ((Button) findViewById(R.id.btn3)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ButtonActivity3.class); MainActivity.this.startActivity(intent); } }); // 方法4.標(biāo)簽直接標(biāo)注觸發(fā)事件 ((Button) findViewById(R.id.btn4)) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ButtonActivity4.class); MainActivity.this.startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }
上面代碼中:
1、我們建立的MainActivity類需要繼承自Activity
2、需要覆寫onCreate方法,并通過setContentView方法加載相應(yīng)的layout(布局)文件
3、通過findViewById方法找到相應(yīng)的控件(在layout布局文件中定義的控件)并綁定一個Click事件(Java中通過監(jiān)聽器實現(xiàn),C#中通過委托實現(xiàn))
4、可以通過Intent對象傳遞數(shù)據(jù)并跳轉(zhuǎn)到其它的Activity
5、onCreateOptionsMenu和onOptionsItemSelected是添加和選中菜單項時的方法。
一下是分別四個activity的內(nèi)容:
第一種:
public class ButtonActivity1 extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button1); button = (Button) findViewById(R.id.btn1); button.setOnClickListener(new MyListener()); } public class MyListener implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(ButtonActivity1.this, "這是事件的第一種寫法,內(nèi)部類定義事件", 2000).show(); } } }
第二種:
public class ButtonActivity2 extends Activity { Button button ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button2); button = (Button)findViewById(R.id.btn1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(ButtonActivity2.this, "這是事件的第二種寫法,匿名內(nèi)部類的形式", 2000).show(); } }); } }
第三種:
public class ButtonActivity3 extends Activity implements OnClickListener { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button3); button= (Button)findViewById(R.id.btn1); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: Toast.makeText(ButtonActivity3.this, "這是事件的第三種寫法,直接實現(xiàn)OnClickListener接口的OnClick方法", 2000).show(); break; default: Toast.makeText(ButtonActivity3.this, "沒有觸發(fā)", 2000).show(); break; } } }
第四種:
需要在layout布局文件xml中指定btnClickEvent方法。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.test.Button4Activity" > <Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點(diǎn)擊我" android:onClick="btnClickEvent"/> </LinearLayout> public class ButtonActivity4 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button4); } public void btnClickEvent(View v){ Toast.makeText(ButtonActivity4.this, "這是事件的第四種寫法,直接在布局文件的Button標(biāo)簽上綁定Click事件", 2000).show(); } }
上面的4種寫法當(dāng)中第3種方法相對用的更多一點(diǎn)。當(dāng)一個activity當(dāng)中有多個按鈕需要觸發(fā)click事件時,通過第3種寫法更容易管理和維護(hù)按鈕事件代碼。
布局是很重要的一塊內(nèi)容,我將在下面的博客中進(jìn)行講解,這里簡單提及一下。
我們用的是LinearLayout(線性布局,其它的還有相對布局、絕對布局等等),設(shè)置了Android:orientation屬性值為vertical(垂直),依次從上往下開始顯示控件。
其它3個布局文件跟這個內(nèi)容一樣,都是只放置了一個按鈕。
activity_main.xml的配置如下(就簡單的放置了4個按鈕):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.test.MainActivity" > <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe1" /> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe2" /> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe3" /> <Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe4" android:onClick="btnClickEvent"/> </LinearLayout>
最終更重要的一步,需要在AndroidManifest.xml文件中配置注冊Activity,完整的配置如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ButtonActivity1" android:label="@string/button1" /> <activity android:name=".ButtonActivity2" android:label="@string/button2" /> <activity android:name=".ButtonActivity3" android:label="@string/button3" /> <activity android:name=".ButtonActivity4" android:label="@string/button4" /> </application> </manifest>
這里面有一個需要注意的地方,
<action android:name="android.intent.action.MAIN" />
設(shè)置MainActivity為"主Activity",即啟動時最先顯示的是哪一個Activity。
下面的多個activity都需要在"清單文件"中注冊,這樣程序中才能找到這些Activity。
strings.xml文件配置的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">test</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="clickMe1">按鈕事件1</string> <string name="clickMe2">按鈕事件2</string> <string name="clickMe3">按鈕事件3</string> <string name="clickMe4">按鈕事件4</string> <string name="button1">按鈕1</string> <string name="button2">按鈕2</string> <string name="button3">按鈕3</string> <string name="button4">按鈕4</string> </resources>
當(dāng)然你也可以直接在layout文件中寫死,但是這樣更利于維護(hù),這也是Android開發(fā)所推薦的方法。
以上所述是小編給大家介紹的Android中activity跳轉(zhuǎn)按鈕事件的四種寫法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android中fragment與activity之間的交互(兩種實現(xiàn)方式)
- Android設(shè)置Activity背景為透明style的簡單方法(必看)
- Android基于OpenGL的GLSurfaceView創(chuàng)建一個Activity實現(xiàn)方法
- Android activity堆棧及管理實例詳解
- Android使用Dialog風(fēng)格彈出框的Activity
- Android中g(shù)etActivity()為null的解決辦法
- 不依賴于Activity的Android全局懸浮窗的實現(xiàn)
- Android實現(xiàn)Activity水平和垂直滾動條的方法
- Android使用Theme自定義Activity進(jìn)入退出動畫的方法
- Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法
- 詳解Android中Activity運(yùn)行時屏幕方向與顯示方式
相關(guān)文章
Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之獲取網(wǎng)絡(luò)上的Json數(shù)據(jù)實例,本文用完整的代碼實例講解了在Android中讀取網(wǎng)絡(luò)中Json數(shù)據(jù)的方法,需要的朋友可以參考下2014-10-10淺析Android手機(jī)衛(wèi)士之號碼歸屬地查詢
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之號碼歸屬地查詢的相關(guān)資料,需要的朋友可以參考下2016-04-04Kotlin中Lambda表達(dá)式與高階函數(shù)使用分析講解
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對 lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡潔高效的特性。下面我們來仔細(xì)看一下2022-12-12安卓GET與POST網(wǎng)絡(luò)請求的三種方式
今天小編就為大家分享一篇關(guān)于安卓GET與POST網(wǎng)絡(luò)請求的三種方式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12android RecyclerView添加footerview詳解
大家好,本篇文章主要講的是android RecyclerView添加footerview詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01