日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android学习笔记(七):多个Activity和Intent

發布時間:2025/5/22 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记(七):多个Activity和Intent 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

根據www.mars-droid.com:Andriod開發視頻教學,先跳過書本《Beginning Android 2》的幾個章,我是這兩個資源一起看,需要進行一下同步。先初步了解一下應用具有多個Activity的處理情況。

視頻中自然不會如同書本講的仔細,勝在快速明了,反正也只是工具,所以這次主要重點在于如何操作。Intent用于在一個應用中多個Activity的調用和數據傳遞,也可用于調用其他服務(應用)。

1、Button觸發

在《Android學習筆記(六):xml和widget》中,我們通過Android XML以及實現View.OnClickListener接口的方式來處理button觸發調用,這里我們采用后一種方式,并做了稍稍改動。

?

... ...?
public class Activity01 extends Activity {
??? private Button mybutton = null;
?
??? public void onCreate(Bundle savedInstanceState) {
?????? ... ...
??????? mybutton.setOnClickListener(new MyButtonListener());
??? }
????
??? class MyButtonListener?implements View.OnClickListener{
??? ??? public void onClick(View v) {
??? ?? ? ?? ... ...?/* 在此,我們將調用另一個Activity */
??? ??? }
??? }
}

?

2、編寫另一個Activity

我們編寫一個簡單的Activity類OtherActivity,其中只有一個TextView。

... ...?
public class OtherActivity extends Activity{
??? private TextView myTextView = null;?
???
??? protected void onCreate(Bundle savedInstanceState) {
??? ??? super.onCreate(savedInstanceState);
??? ??? setContentView(R.layout.other);
??? ??? myTextView = (TextView)findViewById(R.id.myTextView);
??? }
}

編寫一個Activity,必須在AndroidManifest.xml中進行注冊:

??? <?xml version="1.0" encoding="utf-8"?>
??? <manifest? ... ...>????
??? ??? <application?android:icon="@drawable/icon" android:label="@string/app_name">
??? ??????? <activity android:name=".Activity01" android:label="@string/app_name">??? ... ...??? </activity>
??? ??? ????<activity android:name=".OtherActivity" android:label="@string/other" /><!-- 在res/values/strings.xml中增加other的定義 <string name="other">It/'s other activity!</string> -->
??? ??? </application>
??? </manifest>

3、通過Intent,在Activity01中調起OtherActivity,并向OtherActivity傳遞某個信息

在MyButtonListener中的onClick:

?Intent?intent =?new Intent();
?intent.putExtra("param_str", "Info from Activity01");?//向另一個Activity傳遞<name,value>,value采用string的格式,也可以是其他
?intent.setClass(Activity01.this, OtherActivity.class);//指出是哪個Activity,setClass(對象,類),對于嵌套類,為了提供良好的閱讀方式并避免奇異,我們都指明是哪個類
?Activity01.this.startActivity(intent);?//啟動另外的Activity,作為View的方法,可以直接使用startActivity,由于嵌套類,這樣些可以清晰一些。

4、在Otherctivity中接受傳遞的信息

?Intent intent =?getIntent();
String value = intent.getStringExtra("param_str");

5、intent也可以調用其他的應用,例如發送短信

Uri uri = Uri.parse("smsto:0000123456");
Intent intent = new?Intent(Intent.ACTION_SENDTO,uri);//Intent(String?action,Uri?uri)對uri進行某個操作,ACTION_SENDTO:Send a message to someone specified by the data.
intent.putExtra("sms_body", "This is my text info from Activity01.");?//傳遞SMS的文本內容
Activity01.this.startActivity(intent);?//啟動另外的Activity,并不限于是否是同一個應用。系統收到相關消息,將調起相關應用

?

相關鏈接:?我的Android開發相關文章

轉載于:https://www.cnblogs.com/mzsoft/p/4370436.html

總結

以上是生活随笔為你收集整理的Android学习笔记(七):多个Activity和Intent的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。