編輯:關於Android編程
本章使用Intent,回傳需要的結果;
流程:
啟動主頁->按下按鍵啟動第二個activity->輸入用戶名,按下OK按鍵->回傳用戶名回主頁->主頁讀取用戶名並顯示出來。
1 啟動主頁圖:
2 啟動第二個activity
3 輸入用戶名:
4 回傳到主頁,讀取並顯示:
新建項目,然後輸入對應的代碼就OK了。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MSDW99KzvefD5rT6wusoYWN0aXZpdHlfbWFpbi54bWwpo7o8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">
建立一個Button,然後指定相應時間是onClick
2 主頁的響應時間代碼:
public class MainActivity extends Activity { /* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */ int requestCode = 1; public staticboolean equ(T t1, T t2){ return t1 == t2; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode); } }
3 進入第二個activity的界面代碼:
4 第二個activity的邏輯代碼
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class SecondaryActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.secondary_activity); } public void onClick(View v){ Intent data = new Intent(); EditText txt_username = (EditText) findViewById(R.id.txt_username); data.setData(Uri.parse(txt_username.getText().toString())); setResult(RESULT_OK, data); finish(); } }
5 主頁邏輯的讀取並顯示結果的代碼:
@Override public void onActivityResult(int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show(); } }從上一個對應的activity返回的時候就自動調用處理的函數,其中的表示符號就是requestCode和返回的標識符reqCode對應起來,以便確認這個返回的Intent是對應的activity返回的。
主頁邏輯的完整代碼如下:
package com.example.intenttest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { /* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */ int requestCode = 1; public staticboolean equ(T t1, T t2){ return t1 == t2; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode); } @Override public void onActivityResult(int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show(); } } @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); } }
就是熟悉好Android的一些機制,理解起來倒是沒有什麼難度的。
說好是百日程序,看來不能百日之內完成了,因為還有很多其他事情做。
爭取做快點吧,以後出點精品教程,把學到的,讓更多人學會也是很有意義的事情的。
Android應用程序內存洩漏介紹內存洩漏和內存溢出的區別內存溢出(out of memory)是指程序在申請內存時,沒有足夠的內存空間供其使用,出現out of mem
好的,我們繼續來了解IPC機制,在上篇我們可能就是把理論的知識寫完了,然後現在基本上是可以實戰了。一.Android中的IPC方式 本節我們開始詳細的分析各中跨進程的方
Java代碼 /** * 打開文件 * @param file */ private void openFile(File file){ Intent inte
內存洩露,是Android開發者最頭疼的事。可能一處小小的內存洩露,都可能是毀於千裡之堤的蟻穴。 怎麼才能檢測內存洩露呢?網上教程非常多,不過很多都是使用Eclipse