Android學習筆記 - Intent篇

1.Intent的主要作用
負責從一個Actvity傳遞數據到另一個Activity或其它對象
傳遞的對象不一定要是程序自身的Activity,可以是系統的,或其它程序的,或服務等


2.一個Intent對象包含的一組信息
Component name:傳遞到哪個對象
Action:傳遞的動作(Intent.ACTION_??)
Data:傳遞的URI
Extras:傳遞參數(多種數據類型的鍵值對)


3.調用跳轉另一個Activity
使用startActivity(Intent)方法


4.添加監聽事件的內部類
class btnGotoActivity02Listener implements OnClickListener {
  public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(Activity01Activity.this, Activity02Activity.class); //從哪個對象傳遞到哪個對象
    intent.putExtra("text_content", "我是一隻小小小小鳥"); //傳鍵值對
    startActivity(intent); //跳轉
  }
}


5.爲按鈕添加監聽事件
btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());


6.使用Intent打開短信發送界面
class btnSendSmsListener implements OnClickListener {
  public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000")); //SENDTO調用短信界面
    intent.putExtra("sms_body", "親,我想你了……"); //短信內容
    startActivity(intent);
  }
}


7.另一個Activity中接收Intent傳遞過來的鍵值對
Intent intent = getIntent();
txtActivity02 = (TextView) findViewById(R.id.txtActivity02);

txtActivity02.setText(intent.getStringExtra("text_content"));


//完整佈局示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnGotoActivity02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnGotoActivity02" />

    <Button
        android:id="@+id/btnSendSms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnSendSms" />
    
</LinearLayout>


//完整類示例

public class Activity01Activity extends Activity {
	/** Called when the activity is first created. */
	Button btnGotoActivity02 = null;
	Button btnSendSms = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity01);

		btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
		btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());
		
		btnSendSms = (Button) findViewById(R.id.btnSendSms);
		btnSendSms.setOnClickListener(new btnSendSmsListener());
	}

	// 內部類:btnGotoActivity02點擊事件監聽
	class btnGotoActivity02Listener implements OnClickListener {

		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(Activity01Activity.this, Activity02Activity.class);
			intent.putExtra("text_content", "我是一隻小小小小鳥");
			startActivity(intent);
		}

	}

	// 內部類:btnSendSms點擊事件監聽
	class btnSendSmsListener implements OnClickListener {

		public void onClick(View v) {
			Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000"));
			intent.putExtra("sms_body", "親,我想你了……");
			startActivity(intent);
		}

	}
}


 

 

發佈了36 篇原創文章 · 獲贊 5 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章