android Intent (初步進入intent)

來源:互聯網
上載者:User

在一個項目之中,會由多個Activity程式所組成,那麼此時,這多個Activity程式之間就需要進行通訊,而這之間的通訊就依靠Intent完成。Activity程式支援的Intent操作方法

No. 方法 類型 描述
1 public void startActivity(Intent intent) 普通 啟動一個Activity,並通過Intent傳送資料
2 public void startActivityForResult(Intent intent, int requestCode) 普通 啟動並接收另一個Activity程式回傳資料,當requestCode大於0才可以觸發onActivityResult()
3 public Intent getIntent() 普通 返回啟動當前Activity程式的Intent
4 protected void onActivityResult(int requestCode, int resultCode, Intent data) 普通 當需要接收Intent回傳資料的時候覆寫此方法對回傳操作進行處理
5 public void finish() 普通 調用此方法會返回之前的Activity程式,並自動調用onActivityResult()方法
6 public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 普通 處理返回的Cursor結果集
XML設定檔盒JAVA檔案如下
?布局管理器高度為螢幕高度?預設顯示文字?組件高度為文字高度

public class Send extends Activity {private Button mybut = null ;// 按鈕組件private TextView msg = null ;// 文本組件@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.send_main);// 預設布局管理器this.mybut = (Button) super.findViewById(R.id.mybut) ;// 取得組件this.msg = (TextView) super.findViewById(R.id.msg) ;// 取得組件this.mybut.setOnClickListener(new OnClickListenerImpl());// 定義單擊事件}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {Intent it = new Intent(Send.this, Receive.class);// 執行個體化Intentit.putExtra("myinfo", "我打算打算打算打算的水電費") ;// 附加資訊Send.this.startActivityForResult(it, 1);// 啟動Activity}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (resultCode) {// 判斷操作類型case RESULT_OK:// 成功操作msg.setText("返回的內容是:" + data.getStringExtra("retmsg"));break;case RESULT_CANCELED:// 取消操作msg.setText("操作取消。");break ;default:break;}}}


?此布局管理器高度為螢幕高度?組件高度為文字高度?預設顯示文字


public class Receive extends Activity {private TextView show = null ;// 文本顯示組件private Button retbut = null ;// 按鈕組件@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.receive_main);// 調用預設布局管理器this.show = (TextView) super.findViewById(R.id.show) ;// 取得組件this.retbut = (Button) super.findViewById(R.id.retbut) ;// 取得組件Intent it = super.getIntent() ;// 取得啟動此程式的IntentString info = it.getStringExtra("myinfo") ;// 取得設定的附加資訊this.show.setText(info) ;// 設定文本顯示資訊this.retbut.setOnClickListener(new OnClickListenerImpl()) ;// 設定監聽}private class OnClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {Receive.this.getIntent().putExtra("retmsg", "哈哈哈") ;// 返回資訊// 設定返回資料的狀態,RESULT_OK與Send.java中的onActivityResult()裡判斷的對應Receive.this.setResult(RESULT_OK, Receive.this.getIntent()) ;Receive.this.finish() ;// 結束Intent}}}




聯繫我們

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