EX03_10 EX03_10

 package irdc.ex03_10;

 

/* import相關class */

import java.text.DecimalFormat;

import java.text.NumberFormat;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class EX03_10_1 extends Activity 

{

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    /* 載入main.xml Layout */

    setContentView(R.layout.myalyout);

    

    /* 取得Intent中的Bundle物件 */

    Bundle bunde = this.getIntent().getExtras();

    

    /* 取得Bundle物件中的資料 */

    String sex = bunde.getString("sex");

    double height = bunde.getDouble("height");

    

    /* 判斷性別 */

    String sexText="";

    if(sex.equals("M")){

    sexText="男性";

    }else{

    sexText="女性";

    }

    

    /* 取得標準體重 */

    String weight=this.getWeight(sex, height);

    

    /* 設定輸出文字 */

    TextView tv1=(TextView) findViewById(R.id.text1);

    tv1.setText("你是一位"+sexText+"\n你的身高是"+height+"公分\n你的標準體重是"+weight+"公斤");

  }

  

  /* 四捨五入的method */

  private String format(double num)

  {

    NumberFormat formatter = new DecimalFormat("0.00");

String s=formatter.format(num);

return s;

  }

 

  /* 以findViewById()取得Button物件,並加入onClickListener */

  private String getWeight(String sex,double height)

  {

    String weight="";

if(sex.equals("M"))

{

 weight=format((height-80)*0.7);

    }else

{

 weight=format((height-70)*0.6);

}

return weight;

  }

}

 
Ch09_IntentStarter Ch09_IntentStarter

 package tw.com.flag.ch09_intentstarter;

 
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
 
public class MainActivity extends Activity {
    public void onClick(View v) {
    Intent it = new Intent(Intent.ACTION_VIEW); //建立意圖並指定預設動作
   
    switch(v.getId()) {//讀取按鈕的 Id 來做相關處理
    case R.id.buttonEmail:   //指定 E-mail 地址
    it.setData(Uri.parse("mailto:lcc@ms01.dahan.edu.tw"));
    it.putExtra(Intent.EXTRA_CC,                      //設定副本收件人
    new String[] {"test@flag.com.tw"});
    it.putExtra(Intent.EXTRA_SUBJECT, "您好");        //設定主旨
    it.putExtra(Intent.EXTRA_TEXT, "謝謝!");//設定內容
    break;
    case R.id.buttonSms:  //指定簡訊的傳送對象及內容
    it.setData(Uri.parse("sms:0920-156522?body=您好!")); 
        break;
    case R.id.buttonWeb:  //指定網址
    it.setData(Uri.parse("http://203.64.42.73/ksnews/"));
    break;
    case R.id.buttonGps:  //指定 GPS 座標:台北火車站
    it.setData(Uri.parse("geo:25.047095,121.517308"));
    break;
    case R.id.buttonTEL:  //指定 GPS 座標:台北火車站
     it.setData(Uri.parse("tel:03-8210873")); 
    break;
    case R.id.buttonWebSearch:  //搜尋 Web 資料
    it.setAction(Intent.ACTION_WEB_SEARCH);  //將動作改為搜尋
    it.putExtra(SearchManager.QUERY, "旗標出版");
    break;
    }
    startActivity(it);  //啟動適合意圖的程式
    }
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
 
ATestClient ATestClient

 package First.AtestClient;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
 
public class ATestClientActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
setContentView(R.layout.main);
 
Socket socket;
 
try {
 
InetAddress serverAddr = InetAddress.getByName("192.168.0.145");
Log.d("TCP", "C: Connecting...");
try {
 
socket = new Socket(serverAddr, 2222);
String message = "L";
Log.d("TCP", "StringAfter");
try {
Log.d("TCP", "C: Sending: '" + message + "'");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(message);
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (Exception e) {
}
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// TCPServer.SERVERIP
    }
}
EX03_08 EX03_08

 package irdc.ex03_08;

 
/* import相關class */
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class EX03_08 extends Activity implements OnClickListener 
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    /* 載入main.xml Layout */
    setContentView(R.layout.main);
   
    /* 以findViewById()取得Button物件,並加入onClickListener */
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);
  }
  public void onClick(View v)
  {
 jumpToLayout2();
  }
  /* method jumpToLayout2:將layout由main.xml切換成mylayout.xml */
  public void jumpToLayout2()
  {
/* 將layout改成mylayout.xml */
setContentView(R.layout.mylayout);
/* 以findViewById()取得Button物件,並加入onClickListener */
    Button b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(new Button.OnClickListener()
    {
    public void onClick(View v)
    {
    jumpToLayout1();
    }
    });
  }
  
  /* method jumpToLayout1:將layout由mylayout.xml切換成main.xml */
  public void jumpToLayout1()
  {
/* 將layout改成main.xml */
setContentView(R.layout.main);
    
    /* 以findViewById()取得Button物件,並加入onClickListener */
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
    public void onClick(View v)
    {
    jumpToLayout2();
    }
    });
  }
}