———————————————————————
『很多童鞋說我的代碼運作後,點選home或者back後會程式異常,如果你也這樣遇到過,那麼你肯定沒有仔細讀完himi的博文,第十九篇himi專門寫了關于這些錯誤的原因和解決方法,這裡我在部落格都補充說明下,省的童鞋們總疑惑這一塊;請點選下面聯系進入閱讀:
——————————————————————-
今天講下在surfaceview中如何實作兩個或者多個activity之間的切換與資料互動,為了更形象一些我做了一個遊戲登入界面的demo,其中對于輸入界面的布局這些我也是随意寫了下,主要是實作功能并沒有花時間去美化,是以大家可以自己去xml或者代碼中去改變布局,每個小元件的寬高等。好了,下面先上圖(本人超愛~哆啦a夢,是以少不了用哆啦a夢的圖了,娃哈哈)
由于代碼中一共寫了三個類,這裡不再全部貼出來,不友善;大家可以在文章的末尾去下載下傳源碼;
兩個activity之間切換我概括的分為兩步:
1. 代碼實作切換操作。2.配置中聲明另外一個acitivity!
我們先看第一步:這裡是觸屏進行中的一段代碼:
1
2
3
4
5
6
7
8
9
10
11
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是如何建立并且怎麼接受資料的。
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
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!
<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” 就可以了。