首先已經把 Android 的環境都灌好了,
就開始來寫第一個 Android 程式囉!!
找到檔案,點選adt-bundle-windows
再點選 eclipse檔
看到有紫色圓圓的小圖示,eclipse點開
點開後他會要你選擇你的workspace的位置,自己要先設一個workspace的資料夾喔!
我的workspace設在 C:\Users\Nicky\Desktop\workspace
因為要寫的程式是公尺和公分的轉換,
所以我把Application Name 的名字取做:mandcm (公尺和公分)
SDK 我都設為 API17 : Android4.2(Jelly Bean)
按Next
按Next
按Next
按Next
創好的專案長這樣子
因為不需要Hello World ,右上角 TextView -"Hello World" 點右鍵 delete掉
Palette 我習慣看文字的,也有另一種是小圖示的
現在開始來拉介面
點選Layouts 的 LinearLayout(Horizontal)
顧名思義:拉出的會是水平的框框,在手機上的字會是水平的
再拉一個 TextView 到剛剛建的linearLayout1下
同樣的步驟,再拉一個LinearLayout水平
然後加一個textView在裡面
然後改掉Text名字,一個改為公尺一個改為公分
再來要拉的是,使用者可以打數字進去的區域,
點選Text Fields 裡的 Number(Decimal) 到公尺下面和公分下面 各拉一個
如果系統顯示錯誤,點選undo回到上一個步驟,如下圖畫圈的地方,把它改為17就可以了
拉完的圖如下:
然後點選editText1 按右鍵,更改他的id :edtM
editText2,id:edtCm 依自己的喜好改id,方便後面程式撰寫
現在來拉button ,就是按紐,要拉兩個按鈕
然後改掉Text,一個改 轉換,一個改清除
一樣按右鍵改button的id
轉換的id是buttonChange
清除的id是buttonClean
現在來寫主程式
要怎麼看主程式呢?
點選src檔,再點選裡面的MainActivity.java,就開始來寫囉
首先:
在import的地方 加入
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
通常他會自己跳出來選項例如edtM
如果沒跳出來,在自己手動加入
edtM=(EditText) this.findViewById(R.id.edtM);
edtCm=(EditText) this.findViewById(R.id.edtCm);
打完
buttonChange=(Button) this.findViewById(R.id.buttonChange);
buttonClean=(Button) this.findViewById(R.id.buttonClean);
來設定按紐(轉換)的功能,一樣會自己顯示有什麼功能
buttonChange.setOnClickListener()
選擇 setOnClickListener()
程式碼如下:
package com.example.mandcm; // 每個人不一樣 要改喔!!
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
public class MainActivity extends Activity {
EditText edtM,edtCm;
Button buttonChange,buttonClean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtM=(EditText) this.findViewById(R.id.edtM);
edtCm=(EditText) this.findViewById(R.id.edtCm);
buttonChange=(Button) this.findViewById(R.id.buttonChange);
buttonClean=(Button) this.findViewById(R.id.buttonClean);
buttonChange.setOnClickListener (new Button.OnClickListener()
{
public void onClick(View v) {
//TODO 自動產生的方法Stub
double m,cm;
String in=edtM.getText().toString();
if(!in.equals("")){
m=Double.parseDouble(in);
cm=m*100;
edtCm.setText(cm+"");
} else
{
cm=Double.parseDouble(edtCm.getText().toString());
m=cm/100;
edtM.setText(m+"");
}
} } );
buttonClean.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
edtM.setText("");
edtCm.setText("");
}
}
);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}