贊助商連結

Google Android 初探

上禮拜課堂報告 Google Android 的一些東西

同時也抓了 SDK 下來,照著範例程式做了一個猜數字的小遊戲

Android demo program

功能就是簡單到爆的猜數字,太小會說 more,正確會說 correct

Google Android 開發還蠻簡單的

先拉 layout,在寫 class,最後呼叫一下就 OK 了

以下是程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//猜數字
package tw.cheyingwu.demo;
public class GuessNumber {
int answer;
int input;
public GuessNumber(){
answer =(int) (Math.random()*10);
input = n;
}
public String result(int n){
input = n;
if (input==answer)
return "correct";
if (input<answer) {
return "more";
}
else{
return "less";
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//主程式
package tw.cheyingwu.demo;import tw.cheyingwu.demo.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
public class demo extends Activity {
private GuessNumber gn = new GuessNumber();;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.guessnumber);
Button addButton = (Button) findViewById(R.id.add);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView mText = (TextView) findViewById(R.id.label);
EditText eText = (EditText) findViewById(R.id.entry);
int n = Integer.parseInt(eText.getText().toString());
mText.append("Your input: " + eText.getText() + " Result: "+
gn.result(n) + "\n");
eText.setText("");
}
}
);
}
}

就這樣