a30e1269-6d54-34fe-a55d-118d53e7d53819d60937-b8b8-3baa-96e3-dcff30f8dbe35e098ba2-56b7-364a-907c-5366980c6fd0  

Himi 原創, 轉載請注明! 謝謝。

 

原文位址:HTTP://blog.csdn.net/xiaominghimi/archive/2010/12/23/6094182.aspx



今天講下在Surfaceview中如何實現兩個或者多個Activity之間的切換與資料交互,為了更形象一些我做了一個遊戲登錄介面的demo,其中對於輸入介面的佈局這些我也是隨意寫了下,主要是實現功能並沒有花時間去美化,所以大家可以自己去xml或者代碼中去改變佈局,每個小元件的寬高等。好了,下面先上圖(本人超愛~哆啦A夢,所以少不了用哆啦A夢的圖了,娃哈哈)
由於代碼中一共寫了三個類,這裡不再全部貼出來,不方便;大家可以在文章的末尾去下載源碼;

 

兩個activity之間切換我概括的分為兩步:

 

1. 代碼實現切換操作。2.配置中聲明另外一個acitivity!

 

我們先看第一步:這裡是觸屏處理中的一段代碼:




JAVA代碼
1.public boolean onTouchEvent(MotionEvent event) {
2. float pointx = event.getX();
3. float pointy = event.getY();
4. if (pointx > bp_x + 14 && pointx < bp_x + 14 + 117) {
5. if (pointy > bp_y + 43 && pointy < bp_y + 43 + 15) {
6. // 帳號
7. Intent i = new Intent();// 得到一個意圖的實例
8. i.putExtra("count", 1);// 寫出資料
9. i.putExtra("himi", str_zh);
10. i.setClass(MainActivity.instance, Register.class);// 設置當前activity以及將要操作的類
11. MainActivity.instance.startActivity(i);// 用當前activity來啟動另外一個activity
12. }
13. }
14.}
public boolean onTouchEvent(MotionEvent event) {
float pointx = event.getX();
float pointy = event.getY();
if (pointx > bp_x + 14 && pointx < bp_x + 14 + 117) {
if (pointy > bp_y + 43 && pointy < bp_y + 43 + 15) {
// 帳號
Intent i = new Intent();// 得到一個意圖的實例
i.putExtra("count", 1);// 寫出資料
i.putExtra("himi", str_zh);
i.setClass(MainActivity.instance, Register.class);// 設置當前activity以及將要操作的類
MainActivity.instance.startActivity(i);// 用當前activity來啟動另外一個activity
}
}
} 顯示定義一個intent 物件,Intent 這個類的機制是協助交互的,詳細的說明這裡不多講;





Intent 中的putExtra()函數是起到兩個activity之間交互交互的,這個方法類似 hashtable 或者hashmap中的put是一樣的,第一個參數是key(索引) ,後一個參數volue(值),根據key我們可以得到對應的volue了。那麼後面我也附上接受的處理。

 

Intent 中的setClass()函數也是傳入兩個參數,第一個是傳入當前實例的activity物件,後面一個參數指需要打開的activity這個類!然後我們就可以利用當前activity物件來啟動另外一個activity了。然後我們看下在另外一個activity是如何創建並且怎麼接受資料的。




JAVA代碼
1.package com.himi;
2.import android.app.Activity;
3.import android.content.Intent;
4.import android.os.Bundle;
5.import android.view.View;
6.import android.view.View.OnClickListener;
7.import android.widget.Button;
8.import android.widget.EditText;
9.import android.widget.LinearLayout;
10.import android.widget.TextView;
11./**
12. * @author Himi
13. *
14. */
15.public class Register extends Activity {
16. private Button button_ok;
17. private EditText et;
18. private TextView tv;
19. private LinearLayout ly;
20. private Register rs;
21. private byte count;
22. @Override
23. protected void onCreate(Bundle savedInstanceState) {
24. super.onCreate(savedInstanceState);
25. rs = this;
26. ly = new LinearLayout(this);
27. button_ok = new Button(this);
28. button_ok.setWidth(100);
29. button_ok.setText("確定");
30. button_ok.setOnClickListener(new OnClickListener() {
31. public void onClick(View v) {
32. if (count == 1) {
33. MySurfaceView.str_zh = et.getText().toString();
34. } else if (count == 2) {
35. MySurfaceView.str_pass = et.getText().toString();
36. }
37. rs.finish();
38. }
39. });
40. Intent intent = this.getIntent();
41. count = (byte) intent.getIntExtra("count", 0);
42. String temp_str = "";
43. String temp_str2 = "";
44. et = new EditText(this);
45. tv = new TextView(this);
46. if (count != 3) {
47. temp_str = intent.getStringExtra("himi");
48. if (count == 1) {
49. rs.setTitle("請輸入帳號!");
50. } else {
51. rs.setTitle("請輸入密碼!");
52. }
53. ly.addView(tv);
54. ly.addView(et);
55. ly.addView(button_ok);
56. if (temp_str != null) {
57. et.setText(temp_str);
58. }
59. } else {
60. temp_str = intent.getStringExtra("himi_zh");
61. temp_str2 = intent.getStringExtra("himi_pass");
62. rs.setTitle("您輸入的資訊:");
63. tv.setText("帳號:" + temp_str + "\n" + "密碼" + temp_str2);
64. ly.addView(tv);
65. ly.addView(button_ok);
66. if (temp_str != null) {
67. et.setText(temp_str);
68. }
69. }
70. setContentView(ly);
71. }
72.}
package com.himi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author Himi
*
*/
public class Register extends Activity {
private Button button_ok;
private EditText et;
private TextView tv;
private LinearLayout ly;
private Register rs;
private byte count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rs = this;
ly = new LinearLayout(this);
button_ok = new Button(this);
button_ok.setWidth(100);
button_ok.setText("確定");
button_ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (count == 1) {
MySurfaceView.str_zh = et.getText().toString();
} else if (count == 2) {
MySurfaceView.str_pass = et.getText().toString();
}
rs.finish();
}
});
Intent intent = this.getIntent();
count = (byte) intent.getIntExtra("count", 0);
String temp_str = "";
String temp_str2 = "";
et = new EditText(this);
tv = new TextView(this);
if (count != 3) {
temp_str = intent.getStringExtra("himi");
if (count == 1) {
rs.setTitle("請輸入帳號!");
} else {
rs.setTitle("請輸入密碼!");
}
ly.addView(tv);
ly.addView(et);
ly.addView(button_ok);
if (temp_str != null) {
et.setText(temp_str);
}
} else {
temp_str = intent.getStringExtra("himi_zh");
temp_str2 = intent.getStringExtra("himi_pass");
rs.setTitle("您輸入的資訊:");
tv.setText("帳號:" + temp_str + "\n" + "密碼" + temp_str2);
ly.addView(tv);
ly.addView(button_ok);
if (temp_str != null) {
et.setText(temp_str);
}
}
setContentView(ly);
}
} 以上代碼可以看出,新建一個activity其實只需要繼承Activity以及重寫onCreate()方法即可。當然創建的還需要一步很重要的步驟,那麼在第二步中會詳細說明,這裡我們看下是如何接受之前的activity傳來資料的、。 Intent intent = this.getIntent();

count = (byte) intent.getIntExtra("count", 0);

接受也是很簡明易懂,創建一個Intent 意圖物件,調用來去getIntExtra函數得到之前傳來的資料,根據key!當然還有getStringExtra()等等函數都是類似,只是根據你傳入的資料不同選擇不同函數罷了。童鞋們應該注意的是getIntExtra中第二個參數是什麼意思,其實就是一個對於找不到key相匹配的時候會預設return 0 ;

那麼下面介紹第二步:在配置中聲明

當創建一個activity的時候我們必須去在AndroidMainFeset.xml中去生命我們創建的這個類是個Activity!下面附上xml中的code!



JAVA代碼
1.<activity android:name="com.himi.Register" android:theme="@android:style/Theme.Dialog"
2. android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation">
3. </activity>
4. <!--我是注釋-->
5. <activity android:name="com.himi.Register"></activity>
<activity android:name="com.himi.Register" android:theme="@android:style/Theme.Dialog"
android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation">
</activity>
<!--我是注釋-->
<activity android:name="com.himi.Register"></activity> <!---->此符號是注釋 這裡我們有兩個聲明方法,用注釋隔開了,上面一個聲明中還設定了一些屬性,theme顯示的形式,等等,其實最簡單的聲明也是可以的就像注釋下面那一句聲明就足以起到作用了。

當有新的activity的時候一定要去聲明;千萬不要忘記這一步。

備註:配置中有一屬性 -> android:screenOrientation 這是設置橫屏,所以豎屏中點擊會有問題,如果想豎屏顯示正常那麼可以自行刪除xml中的 android:screenOrientation="landscape" 就可以了。

(推薦大家訂閱本博客,因為咱的更新速度可是很快的~娃哈哈)





源碼下載位址:HTTP://download.csdn.net/source/2931619
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()