Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –

Similar presentations


Presentation on theme: "Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –"— Presentation transcript:

1 Android 基本 I/O

2 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK – 基本的按鈕事件 OnClickListener

3 範例: TextView + Button 學會布置 UI layout TextView 與 Button 初探 基本 Button.OnClickListener 的應用

4 步驟 1. 開啟 android 模擬器 2. 利用 Eclipse 新增 android 專案 3. 開啟 layout 布置檔 4. 布置 UI widget 5. 撰寫 widget 動作的 java 程式 6. 執行並測試於模擬器上

5 步驟 3 /* Res/layout/main.xml */ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btnOK" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click me" /> <TextView android:id="@+id/txtDisplay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello" />

6 步驟 4

7 步驟 5 撰寫 java 程式 – Import related packages – Declare UI widgets – findViews() * – setListeners() * – OnClickListener() * : optional

8 Import packages 預設 ( 自動補上 ) – import android.app.Activity; – import android.os.Bundle; 自行加入 – import android.widget.*; // 與 widget 相關 – import android.view.View; // 與 OnClick 相關

9 onCreate(): 程式的啟 始進入點 public class Test extends Activity { /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }

10 Android 程式的構造 四項元件: – 活動 (Activity) 簡言之, Activity 就是「使用者介面」,每個 activity 擁有自己的 View ,因此一個 android 應用 程式可能會擁有多個 activity 交互執行。 – 服務 (Service) – 廣播接收器 (Broadcast Receiver) – 內容提供器 (Content Provider)

11 宣告 widget 宣告程式中所需的 widget ,對應 main.xml // 宣告 UI widget; private Button btn; private TextView txtDisplay;

12 撰寫 findViews() private void findViews(){ btn=(Button)findViewById(R.id.btnOK); txtDisplay=(TextView)findViewById(R.id.tx tDisplay); } /* 必須將宣告的元件與布置於 View 的元件做連結。可 以在任何適當位置放置 findViewById ,然在此將程式 結構化 */

13 撰寫 setListeners() private void setListeners(){ btn.setOnClickListener(btnOK); } /* 按鈕元件必須透過 Listener 來監聽是否有外來的事件, 例如 Click 事件必須由 OnClickListener 來監聽 */

14 撰寫 Listener 的內容 private Button.OnClickListener btnOK=new Button.OnClickListener(){ public void onClick(View v){ txtDisplay.setText(“ 這是第一個 android 專案 "); } }; // 注意要有「 ; 」

15 EditText – 文字輸入方 塊 基本輸入方塊 – 形狀屬性: android:layout_width, android:layout_height – 輸入限制屬性: android:inputType android:numeric – 內容屬性: android:text EditText 幾乎與 TextView 屬性一樣,差 別在於可以接受輸入

16 練習 請練習配置一個 Button, TextView, EditText ,使得按下按鈕後可以將輸入 方塊的內容顯示於 TextView 上 – 配置元件並且指定元件 id – 請試著參考上個單元,先完成 findViews(), setListeners()

17 main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/edtHello" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" /> <TextView android:id="@+id/txtResult" android:layout_width="fill_parent" android:layout_height="wrap_content" />

18 import android.widget.*; import android.view.View; public class test extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListeners(); } private Button btn; private EditText edtHello; private TextView txtResult; private void findViews(){ btn=(Button)findViewById(R.id.btnOK); edtHello=(EditText)findViewById(R.id.edtHello); txtResult=(TextView)findViewById(R.id.txtResult); } private void setListeners(){ btn.setOnClickListener(btnOK); }

19 private Button.OnClickListener btnOK=new Button.OnClickListener(){ public void onClick(View v){ txtDisplay.setText("hello"); } };

20 EditText 的常用方法

21 setText(“ 字串 ”) : – 相同於 TextView.setText() ,用於設定一個 字串資料顯示於 EditText 中 getText() : – 將 EditText 方塊中的現有資料回傳成為 Editable 物件 – 常見搭配.toString() ,將 Editable 轉為字串值 – 若需要轉為數值資料,需搭配數值物件

22 TextView 常見方法.setText() – 數值資料必須經過 DecimalFormat 物件的處 理,才能正確的顯示於 TextView – 練習:倘若 1 美金 = 29.675 台幣,試著寫出 一個美金兌換台幣的計算器

23 DecimalFormat 當 EditText 資料必須轉為 10 進位數值資料時, 則需要 DecimalFormat – Import java.text.DecimalFormat private Button.OnClickListener btnOK=new Button.OnClickListener(){ public void onClick(View v){ double circle; DecimalFormat df=new DecimalFormat(); double num=Double.parseDouble(edtHello.getText().toString()); circle=3.14*num*num; txtResult.setText(" 圓面積: " + df.format(circle)); } };

24 課堂練習 假設「基礎代謝率」公式如下: 女性 BMR=655 + (9.6* 體重 )+(1.8* 身高 ) – (4.7* 年齡 ) 男性 BMR=66 + (13.7* 體重 )+(5* 身高 ) – (6.8* 年齡 ) 請預設性別後,設計一個可計算基礎代謝 的計算器,如此可以提供使用者隨時計 算一天所需的基本卡路里

25

26 package ff.fom; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.widget.*; // 與 widget 相關 import android.view.View; // 與 OnClick 相關 import android.app.AlertDialog; import android.content.DialogInterface; public class Texs extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { package om.dom; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.widget.*; // 與 widget 相關 import android.view.View; // 與 OnClick 相關 import android.app.AlertDialog; import android.content.DialogInterface; public class Ftyt extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListeners(); } private Button btn; private TextView txtDisplay; private EditText edt; private EditText edt2; private void findViews(){ btn=(Button)findViewById(R.id.btn); txtDisplay=(TextView)findViewById(R.id.tx tDisplay); edt=(EditText)findViewById(R.id.edt); edt2=(EditText)findViewById(R.id.edt2); } private void setListeners(){ btn.setOnClickListener(btnClick); } private Button.OnClickListener btnClick=new Button.OnClickListener(){ public void onClick(View v) { double num=Double.parseDouble(edt.getText().t oString()); double l=Double.parseDouble(edt2.getText().toSt ring()); txtDisplay.setText(" 圓柱體積: " + (num*num*3.14*l)); } //String s=Edittttt.getText().toString(); //txtDisplay.setText(s); }; }

27 package ff.fom; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.EditText; import android.widget.TextView; public class Texs extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListeners(); } private RadioButton option1; private RadioButton option2; private EditText edt; private Button btn; private TextView txtDisplay1; private void findViews(){ option1=(RadioButton )findViewById(R.id.option1); option2=(RadioButton )findViewById(R.id.option2); edt=(EditText)findViewById(R.id.edt); btn=(Button)findViewById(R.id.btn); txtDisplay1=(TextView)findViewById(R.id.t xtDisplay1); } private void setListeners(){ btn.setOnClickListener(btnOK); } private Button.OnClickListener btnOK=new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub double x=0; DecimalFormat df=new DecimalFormat(); double num=Double.parseDouble(edt.getText().t oString()); double total=0; if(option1.isChecked()) total+=(num- 80)*0.7; if(option2.isChecked()) total+=(num-70)*0.6; txtDisplay1.setText(" 體重 :"+total); } };

28

29

30 package demo.Android; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.EditText; import android.widget.TextView; public class Test1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViews(); setListeners(); } private RadioButton c1; private RadioButton c2; private EditText a1; private Button click; private TextView d4; private TextView a3; private void findViews(){ c1=(RadioButton )findViewById(R.id.c1); c2=(RadioButton )findViewById(R.id.c2); a1=(EditText)findViewById(R.id.a1); click=(Button)findViewById(R.id.click); d4=(TextView)findViewById(R.id.d4); a3=(TextView)findViewById(R.id.a3); } private void setListeners(){ click.setOnClickListener(btnOK); } private Button.OnClickListener btnOK=new Button.OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub double x=0; DecimalFormat df=new DecimalFormat(); double num=Double.parseDouble(a1.getText().to String()); double total=0; double y=0; double z=0; if(c1.isChecked()) total+=(num-80)*0.7; if(c2.isChecked()) total+=(num-70)*0.6; if(c1.isChecked()) y+=(total-total*0.1); if(c1.isChecked()) z+=(total+total*0.1); if(c2.isChecked()) y+=(total-total*0.1); if(c2.isChecked()) z+=(total+total*0.1); d4.setText(" 體重 "+total); a3.setText(" 體重合理範圍 "+y+"~"+z); } }; }


Download ppt "Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –"

Similar presentations


Ads by Google