Android——Activity和Intent,androidintent

來源:互聯網
上載者:User

Android——Activity和Intent,androidintent
 一,關於Intent


          先看看一個Intent包含啥:

1,Component name:決定啟動哪一個組件

2,action:activity應該做哪些動作

3,data:一個activity向另外一個activity傳遞的資料

4,category:

5,extras:索引值對

6,flags


         跟.net裡面的httprequest對比,感覺還是蠻像的,好巧啊。



二,在一個Activity中啟動另外一個Activity


           首先我們建立兩個Activity,在啟動時進入的Activity中加入一個按鈕,點擊這個按鈕,然後跳轉到另外一個Activity.


            

package com.example.activity_02;import android.net.Uri;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button myButton=null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main); //設定當前布局使用的xml        myButton=(Button)findViewById(R.id.myButton);  //擷取activity上的button        myButton.setOnClickListener(new MyButtonListerner());  //將MyButtonListerner捆綁到myButton上面   ,實現點擊的時候跳轉    }      @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;    }            class MyButtonListerner implements OnClickListener{  @Override  public void onClick(View arg0) {  // TODO 自動產生的方法存根    //產生一個Intent對象  Intent intent=new Intent();  intent.putExtra("textIntent", "pbc");//在Intent中放入索引值對,進行傳遞  intent.setClass(MainActivity.this,OtherActivity.class); //設定從當前activity跳轉到OtherActivity  MainActivity.this.startActivity(intent);//啟動另外的Activity        /*Uri uri=Uri.parse("smsto://0800000123");  Intent intent=new Intent(Intent.ACTION_SENDTO,uri);  intent.putExtra("the sms body", "the sms text");  startActivity(intent);*/    }    }}    



        首先我們要在按鈕上添加一個類似事件監聽的東西,在點擊的時候去做一些跳轉處理。之後在這個具體的處理過程中,我們使用Intent對象來實現傳遞值,設定從當前Activity跳轉到那個Activity.也可以使用Intent調用系統的功能。

          


 小結:

         舉個例子,在BS還是在CS中,頁面表單傳值都是必不可少的東西,到了安卓開發這裡,又出現了這類東西;所以根據前面東西的學習,類推下,可以對比得出安卓需要哪些基礎的知識。







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.