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

Slides:



Advertisements
Similar presentations
Android Fonts Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Advertisements

Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Basic Functionality in Android. Functionality in Android Events in Java – mouse related mouse clicked button down or up mouse entered – many others key.
Who Am I And Why Am I Here I’m professor Stephen Fickas in CIS – you can call me Steve. I have a research group that works with mobile devices (since 1995!)
User Interface Android Applications. Activities An activity presents a visual user interface. Each activity is given a default window to draw in. The.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
Client / Server Programming in Android Eclipse IDE Android Development Tools (ADT) Android SDK
Android Application Development with Java UPenn CS4HS 2011 Chris Murphy
Layout and Control in UI The user interface (UI) is the graphical interface user can see and interact with your app comprising UI controls like textbox,
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
1 Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources, Listener 10/9/2012 Y. Richard Yang.
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
Android development the first app. Andoid vs iOS which is better? Short answer: neither Proponents on both sides For an iOS side, see this article on.
Mobile Programming Lecture 2 Layouts, Widgets, Toasts, and Event Handling.
Wireless Mobility with Android 1 Presented by: Ung Yean MS. Computer Science American University, Washington DC, USA.
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Android Dialog Boxes AlertDialog - Toast
Programming with Android: Layouts, Widgets and Events Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Import import android.graphics.Bitmap; import android.widget.ImageView;
1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Handling View Events. Open the *MainActivity.java* which is the Activity that hosts the layout in "activity_main.xml". The setContentView method inside.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
ANDROID – A FIRST PROGRAM L. Grewe Using AndroidStudio –basic Android  Lets do a “Hello World Project”  Start up AndroidStudio (assume you have installed.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Android and s Ken Nguyen Clayton state University 2012.
아주대학교 LifecareScienceLAB Android Seminar 3 rd class Android Software Development 2011/05/04 – p.m. 06:00 – 팔달관 409 호 아주대학교.
Http :// developer. android. com / guide / topics / fundamentals. html.
You have to remember that  To create an AVD(Android Virtual Device)  The Structure of Android Project  XML Layout  The advantage of XML Layout  Android.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Android Programming.
Lab7 – Advanced.
BMI 程式.
Android Programming - Features
Cleveland State University
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Application Development 1 6 May 2018
Android N Amanquah.
GUI Programming Fundamentals
Android – Event Handling
Android Introduction Hello World.
Cleveland State University
Android Widgets 1 7 August 2018
Android Introduction Hello Views Part 1.
ITEC535 – Mobile Programming
HUJI Post PC Workshop 1 Introduction to Android Development Ari Sprung
Android Dialog Boxes AlertDialog - Toast
תכנות ב android אליהו חלסצ'י.
Picasso Revisted.
Android Introduction Hello Views Part 2.
CIS 470 Mobile App Development
MultiUni Trần Vũ Tất Bình
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Note Q) How to format code in Enclipse? A) Ctrl + i
Programski jezici za mobilne aplikacije
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Notifications
ארועים ומאזינים android.
Adding Components to Activity
CMPE419 Mobile Application Development
Android Sensor Programming
Android Sensor Programming
Android Sensor Programming
Presentation transcript:

Android 基本 I/O

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

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

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

步驟 3 /* Res/layout/main.xml */ <LinearLayout xmlns: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" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click me" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello" />

步驟 4

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

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

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); }

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

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

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

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

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

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

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

main.xml <LinearLayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" />

import android.widget.*; import android.view.View; public class test extends Activity { /** Called when the activity is first created. 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); }

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

EditText 的常用方法

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

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

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)); } };

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

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. 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. 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); }; }

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. 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); } };

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. 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); } }; }