Google Android 初探

十二月 20th, 2007 | Categories: 電腦啦 | Tags: , ,

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

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

Google Android demo program

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

Google Android 開發還蠻簡單的

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

以下是程式碼

//猜數字
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";
		}
	}
}
//主程式
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("");
		 		}
			}
		);
	}
}

另外 layout 的部份,打包下載

就這樣

  1. drik_wen
    七月 10th, 2008 at 21:53
    Reply | Quote | #1

    R.id.add 這句是什麼意思?
    然後 R.java 哪個檔案是需要自己改嗎?
    初學者.. 看不太懂..

  2. 七月 12th, 2008 at 00:01
    Reply | Quote | #2

    sorry 其實我也忘記了 @@

    沒辦法給你解答

    而且這是很早的版本

    之後 Android SDK 還有改版

    也許就不一樣了