android程式設計實例入門 -Sample改寫分享 (2014/09/02)
android程式設計實例入門 -Sample改寫分享 (2014/09/02)
此範例為最簡易的Android的程式碼(03\Sample1),單純利用LinearLayout、TextView來顯示文字和外加一個按鈕,程式碼如下:
package com.jashsample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);//設定布局方式-VERTICAL(垂直)、HORIZONTAL(水平)
setContentView(ll);
TextView tv1 = new TextView(this);
tv1.setText("歡迎光臨。");
Button bt = new Button(this);
bt.setText("購買");
ll.addView(tv1);
ll.addView(bt);
}
@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;
}
}