2012年3月29日 星期四

Android-TQC202 公尺與英呎轉換

學習重點:
(一)在linearLayout下使用relativeLayout
(二)MenuInflater使用方式
(三)onPrepareOptionsMenu使用方式,主要用於點擊事件
相關資料來源

由於系統內容有些不要必要程式,因此以註記的方式取代

題目說明:
英呎 = 公尺 * 3.28
公尺 = 英呎 * 0.3048


package COM.TQC.GDD02;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class GDD02 extends Activity {
 private boolean MenuChange = false;
 //protected String PREF_M = "pref_m";
 //protected String PREF_FT = "pref_ft";
 //protected String pref_m;
 //protected String pref_ft;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button01=(Button)findViewById(R.id.submit01);
        Button button02=(Button)findViewById(R.id.submit02);
        
        button01.setOnClickListener(MtoFT);
        button02.setOnClickListener(FTtoM);
                
    }
    private Button.OnClickListener MtoFT = new Button.OnClickListener ()
    {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   DecimalFormat nf = new DecimalFormat("0.00");
   EditText input_value = (EditText)findViewById(R.id.input_value);
   
   double input = Double.parseDouble(input_value.getText().toString());
   double MFT=input*3.28;
   
   TextView result=(TextView)findViewById(R.id.result);
   result.setText(input+"公尺="+nf.format(MFT)+"英呎");
   
   MenuChange = false;
   
   
  }
     
    };
    
    private Button.OnClickListener FTtoM = new Button.OnClickListener ()
    {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   DecimalFormat nf = new DecimalFormat("0.00");
   EditText input_value = (EditText)findViewById(R.id.input_value);
   
   double input = Double.parseDouble(input_value.getText().toString());
   double MFT=input*0.3048;
   
   TextView result=(TextView)findViewById(R.id.result);
   result.setText(input+"英呎="+nf.format(MFT)+"公尺");
   
   MenuChange = false;
   
  }
     
    };
    
    

    /*@Override 
    public boolean 
    onCreateOptionsMenu(Menu menu) 
    {
        MenuInflater inflater = getMenuInflater();
        
        return true;
    }*/
    
    public boolean 
    onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId()){
        case R.id.menu_reset:
         ResetValue(); //呼叫ResetValue()
         break;        
        }
        return true;
    }
    
    private void
    ResetValue() //重置
    {
     EditText input_value = (EditText)findViewById(R.id.input_value);
     TextView result = (TextView)findViewById(R.id.result);
     
     input_value.setText("");
     result.setText("");
     
     MenuChange=true;
    }
    
    public boolean onPrepareOptionsMenu(Menu menu)
    {
  if(MenuChange)
   menu.clear();
  else
  {
   if(menu.size()<=0)
   {
    MenuInflater inflater = getMenuInflater();//解析定義menu目錄下的選單佈局文件
    inflater.inflate(R.menu.menu, menu);
    
   }
  }
   
     
     return super.onPrepareOptionsMenu(menu);
     
    }

  
         

}

2012年3月21日 星期三

android伺服器建置以microsoft visual web developer 2010 express為例

本篇主要是說明,如何以microsoft visual web developer 2010 express進行android呼叫本機server,進行值的呼叫和運作的前置作業與運作流程。

新增網站,以asp.net,c#為開發環境

新增網站,命名為website,如下圖

從「加入新項目」中加入,webform

測試asp頁面,並開啟瀏覽器輸入http://localhost/website/default.aspx,
測試是否可以正常看到本機網頁內容

從「加入新項目」中加入「web服務」


此時我們可以看到,hellowrold(),這是可以利用手機呼叫的方法

測試頁面,並開啟瀏覽器輸入http://localhost/website/webservice.asmx,可看出目前可以呼叫的方法有哪些。

2012年3月14日 星期三

Android連線service設定方式

(一)layout版面配置
主要內容是webservice()設定,其他部分則是測試內容


    
        
    
    
    



(二)main.java

package nfu.edu;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
 private EditText input;
 private TextView output;
 private Button btn;
 private static Handler handler;//執行緒的處理事件
 private static Runnable right;
 private static Runnable error;
 private static String result;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        input=(EditText)findViewById(R.id.editText);//讀取值的位置
        output=(TextView)findViewById(R.id.textView1);
        btn=(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new Button.OnClickListener(){
            //按下按鈕的事件
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //進度視窗的提示(執行緒)
    final ProgressDialog mydialog=new ProgressDialog(TestActivity.this);//檔案名稱
    mydialog.setTitle("請稍後");//視窗標題
    mydialog.setMessage("執行中");//顯示訊息
    mydialog.show();//指令顯示
    System.out.println("1");//執行緒的流程
    new Thread(){//宣告執行緒
     public void run(){
      int num;
      int x = 10;
      int y = 2;//0會進入例外事件
      
      try{//正常處理
       //num = x/y; //模擬伺服器處理結果(但會出現錯誤訊息,故等執行緒完成後再另外處理)
       System.out.println("2: + num");
       result = WebService.Welcome();//呼叫107行webservice.welcome()
       sleep(3000);//延遲時間
       handler.post(right);//標題檔.處理區塊
       //output.setText(String.valueOf(num));//數值轉字串
      }catch(Exception a){//例外處理
       System.out.println("3");
       handler.post(error);
       a.printStackTrace();
      }finally{//指令移除
       System.out.println("4");
       mydialog.dismiss();
      }
     }
    }.start();
    //處理事件
    handler = new Handler();
             right = new Runnable(){

     public void run() {
      // TODO Auto-generated method stub
      System.out.println("6");
      output.setText(String.valueOf(result));
     }
              
             };
             
             handler = new Handler();
             error = new Runnable(){

     public void run() {
      // TODO Auto-generated method stub
      System.out.println("7");
      output.setText("執行錯誤");
     }
              
             };
    System.out.println("5");
    
    
    //output.setText("hey");
    //output.setText(input.getText().toString());//將輸入值轉為字串輸出
    
    
   }
         
        });
    }
    //-----webservice start
    public static class WebService {
     //service連線設定
     public static final String NAMESPACE = "http://tempuri.org/"; //以asp環境
     public static final String URL_PATH = "http://10.0.2.2/MSG/"; //asp專案命名/MSG
     public static final String SERVICE_NAME = "WebService.asmx"; 
     
     public static String Welcome(){
      
      String method = "HelloWorld";//呼叫server的HelloWorld()
      String output = "";

      try
      {
       
       SoapObject request = new SoapObject(NAMESPACE, method);
       //request.addProperty("whoName", personal);
       
             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
             envelope.dotNet = true;
             envelope.setOutputSoapObject(request);  

             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_PATH + SERVICE_NAME); 
             androidHttpTransport.call(NAMESPACE + method, envelope);

             Object result = envelope.getResponse();
             output = result.toString();
             
      }catch(Exception e){}
      
      System.gc();
   
      return output;  
     }
    }
    
    
    //-----webservice end
}



(三)AndroidManifest.xml網路連線設定




(四)ASP環境,以c#為例
加入新項目(Ctrl+Shift+A)>WebService>
手機端呼叫的webservice的helloworld()。




(五)匯入kaoap設定

2012年3月13日 星期二

Android-TQC110動態密碼顯示

學習重點:
(1)在layout中,android:hint 標記設置顯示編輯文字的提示字
(2)在layout中,從background改變背景
(3)利用checkbox改變edittext狀態


package COM.TQC.GDD01;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class GDD01 extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    final CheckBox cb = (CheckBox)findViewById(R.id.mCheck);
    final EditText et = (EditText)findViewById(R.id.mPassword);
    
    cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

  @Override
  public void onCheckedChanged(CompoundButton arg0,boolean arg1) {
   // TODO Auto-generated method stub
   if(cb.isChecked())
   {
    et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
   }
   else
   {
    et.setTransformationMethod(PasswordTransformationMethod.getInstance());
   }
  }
     
    });
    
  }
}

2012年3月12日 星期一

Android-TQC108 選擇手機型號

學習重點:
(1)Spinner的OnItemSelectedListener事件處理
(2)Spinner與setDropDownViewResource應用
(3)需要2個layout,1個layout放置spinner,另一個layout則顯示被setDropDownView自訂樣式選單
(4)其畫面如下


package COM.TQC.GDD01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class GDD01 extends Activity {
  private String[] types={"請選擇","HTC HERO","HTC MAGIC","HTC TATTOO",
      "NEXUS ONE","SONY X10","MOTO MILESTONE"};
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  
    
    final TextView tv = (TextView)findViewById(R.id.text1);
    Spinner sp = (Spinner)findViewById(R.id.mySpinner);
       //建立一個ArrayAdapter物件,並放置下拉選單的內容
    ArrayAdapter adapter = new ArrayAdapter
    (GDD01.this,android.R.layout.simple_spinner_item,types);
       //設定下拉選單的樣式simple_spinner_dropdown_item
    adapter.setDropDownViewResource(R.layout.myspinner_layout);
    sp.setAdapter(adapter);
    
    sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

  public void onItemSelected(AdapterView arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   if(!types[arg2].equals("請選擇"))
   {
    tv.setText(getString(R.string.str1)+types[arg2]);
   }else{
    tv.setText(getString(R.string.str1));
   }
  }

  public void onNothingSelected(AdapterView arg0) {
   // TODO Auto-generated method stub
   
  }
     
    });
  }
}

2012年3月9日 星期五

Android-TQC106畫廊展示

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GDD01 extends Activity 
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    Gallery g =(Gallery)findViewById(R.id.mygallery);
    g.setAdapter(new ImageAdapter(this));
    g.setOnItemClickListener(new Gallery.OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   Toast.makeText(arg1.getContext(), getString(R.string.my_gallery_text_pre)
     +arg2+getString(R.string.my_gallery_text_post), 
     Toast.LENGTH_SHORT).show();
   
  }
     
    });
   
  }
   
      

  public class ImageAdapter extends BaseAdapter 
  {
   int BackGround;
   Context m_context;
   Integer[]imageId={ R.drawable.png001,R.drawable.png002,
                R.drawable.png003,R.drawable.png004,
                R.drawable.png005,R.drawable.png006,
                R.drawable.png007,R.drawable.png008,
                R.drawable.png009,R.drawable.png010,
                R.drawable.png011
                      };
   
    
    //建構子只有一個參數,即要儲存的Context
   public ImageAdapter(Context c) 
     {
    super();
    m_context=c;
    TypedArray array=m_context.obtainStyledAttributes(R.styleable.Gallery);
    BackGround=array.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
    
     }
     
    
    //回傳所有已定義的圖片總數量
    public int getCount() 
    {
     return imageId.length;
    }
    
    
    public Object getItem(int position) 
    {
      return position;
    }
    
    //取得圖片編號
    public long getItemId(int position) 
    {
      return position;
    }


 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ImageView i = new ImageView(m_context);
  i.setImageResource(imageId[position]);
  i.setLayoutParams(new Gallery.LayoutParams(236, 188));
  i.setScaleType(ImageView.ScaleType.FIT_XY);
  i.setBackgroundResource(BackGround);
  return i;
 }
    
  
  }
   
    
    //使用android.R.drawable裡的圖片當成圖庫來源
}

Android-TQC104計算BMI值

主要運用到下列觀念
(1)intent運作
(2)bundle運用
(3)2個activity傳值


main.java
package COM.TQC.GDD01;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class GDD01 extends Activity 
{
  private EditText etheight;
  private EditText etweight;
  private RadioButton rb1;
  private RadioButton rb2;
    
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);
    
    
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
      
        //從輸入介面中取出了的身高、體重值,要將身高、體重值傳送給 child_Activity 後作計算
       etheight=(EditText)findViewById(R.id.input_height);
       etweight=(EditText)findViewById(R.id.input_weight);
       
       double height=Double.parseDouble(etheight.getText().toString());
       double weight=Double.parseDouble(etweight.getText().toString());
       
       String Sex="";
       
       rb1=(RadioButton)findViewById(R.id.input_m); //男性按鈕
       rb2=(RadioButton)findViewById(R.id.input_w); //女性按鈕
       
       if(rb1.isChecked())
       {
        Sex="M";
       }
       else
       {
        Sex="W";
       }
       
        //這些附加在 Intent 上的訊息都儲存在 Bundle 物件中
          Intent intent = new Intent();
          intent.setClass(GDD01.this, GDD01_child.class);
          
     //透過「intent.putExtras(bundle)」敘述,將「bundle」 物件附加在 Intent 上,隨著 Intent 送出而送出
          Bundle bundle=new Bundle();
          bundle.putDouble("height", height); //38行的值
          bundle.putDouble("weight", weight); //39行的值
          bundle.putString("Sex", Sex);
          
          intent.putExtras(bundle); //查看GDD01_child.java,26行程式,用extras。
          startActivityForResult(intent,0);
       
       
       
      }
    });
  }
  
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    switch (resultCode)
    { 
      case RESULT_OK:
    
        Bundle bundle = data.getExtras();
        String Sex = bundle.getString("Sex");
        double height = bundle.getDouble("height");
        double weight = bundle.getDouble("weight");
        
        etheight.setText("" + height);
        etweight.setText("" + weight);
        if(Sex.equals("M"))
        {
          rb1.setChecked(true);
        }else
        {
          rb2.setChecked(true);
        }
        break;       
      default: 
        break; 
     } 
   } 
}


second.java
package COM.TQC.GDD01 ;

import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GDD01_child extends Activity 
{
  Bundle bundle;
  Intent intent;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    
    
    setContentView(R.layout.mylayout);
    
    
    intent=this.getIntent();
    bundle = intent.getExtras();
    
   
    String Sex = bundle.getString("Sex");
    double height = bundle.getDouble("height");
    double weight = bundle.getDouble("weight");
      
    
    String BMI_result = this.getBMI(height,weight);
    String BMI_advice = this.getAdvice(Sex,height,weight);
    
    TextView tvBMI=(TextView) findViewById(R.id.tvBMI);
    tvBMI.setText(BMI_result);
    TextView tvAdvice=(TextView) findViewById(R.id.tvAdvice);
    tvAdvice.setText(BMI_advice);
    
  
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {          
      
       GDD01_child.this.setResult(RESULT_OK, intent);
       
      
       GDD01_child.this.finish();
      }
    });
  }
  
 //BMI值格式化
  private String format(double num)
  {
      DecimalFormat nf = new DecimalFormat("0.00");//呈現至小數點第二位
      String s=nf.format(num);
      
      return s;
      
  }
  //取得BMI值
  private String getBMI (double height, double weight)
  {
   double BMI_values=weight/(height*height);
   return getString(R.string.report_result)+format(BMI_values);
   
  }
  //依BMI值取得建議
  private String getAdvice (String Sex, double height, double weight)
  {
  double BMI_MAX;
  double BMI_MIN;
  double BMI=weight/(height*height);

  if(Sex.equals("M"))
  {
   BMI_MAX=25.0;
   BMI_MIN=20.0;
  }
  else
  {
   BMI_MAX=22.0;
   BMI_MIN=18.0;
  }
if (BMI> BMI_MAX)
  {
   return getString(R.string.advice_heavy);//體重過重
  }
  else if (BMI< BMI_MIN)
  {
   return getString(R.string.advice_light);//體重過輕
  }
  else
  {
  return getString(R.string.advice_average);//體重適中
  }

  }

}


二個activity設定 從「AndroidManifest.xml」中application新增

2012年3月5日 星期一

Android-TQC102電費計算機

課程學習筆記

關於spinner運用

程式碼參考如下

package COM.TQC.GDD01;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class GDD01 extends Activity
{
 protected TextView MyOutcome;
 protected int volt;
 protected int inputv = 110;
 
     
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        spinner();
        button();
        
    }
 
 
 private void spinner() {
  // TODO Auto-generated method stub
  
  Spinner Sp=(Spinner)findViewById(R.id.input_v);
  Sp.setOnItemSelectedListener(new
   Spinner.OnItemSelectedListener()
  {

   @Override
   public void onItemSelected(AdapterView arg0, View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    volt=arg0.getSelectedItemPosition();
    
    if(volt ==0) 
    {
     inputv=110;
    }
    else
    {
     inputv=220;
    }
   }

   @Override
   public void onNothingSelected(AdapterView arg0) {
    // TODO Auto-generated method stub
    
   }
   
  });
  
 }

 private void button() {
  // TODO Auto-generated method stub
  Button btn=(Button)findViewById(R.id.submit);
  btn.setOnClickListener(new Button.OnClickListener()
  {

   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    EditText fill_a=(EditText)findViewById(R.id.input_a);
    EditText fill_hr=(EditText)findViewById(R.id.input_hr);
    EditText fill_m=(EditText)findViewById(R.id.input_m);
    
    double inputa=Double.parseDouble(fill_a.getText().toString());
    double inputhr=Double.parseDouble(fill_hr.getText().toString());
    double inputm=Double.parseDouble(fill_m.getText().toString());
    
    double total_w= inputv * inputa;
    
    int total_m=(int) Math.round((total_w/1000)*(inputhr*30)*inputm);
    
    MyOutcome=(TextView)findViewById(R.id.respond);
    MyOutcome.setText("$"+total_m);
   }
   
  });
 }
}


2012年3月3日 星期六

blogger嵌入程式碼以SyntaxHighlighter

當撰寫程式碼要分享的時候,如果單純只是貼在blog上,有可能會造成格式亂碼

或是斷行不乾淨的問題,加上增加容易閱讀性,所以在blog中好的程式碼編排是相當重要的。

因此,耗費一個早上來摸一下,加上對css並不是很熟悉,爬了一下文,做了n次實驗後

把相關的經驗分享給大家。

step1
至「SyntaxHighlighter」網頁,它提供一個

可以將程式碼嵌在blog的技術,樣式也相當美觀

step2
至「SyntaxHighlighter hosting」網頁,提供主要是從官方位址上引用程式碼格式,相關css語法都用好了,省去自已上傳至網路空間的步驟。

step3
複製程式碼如下:

















資料來源:http://king971119.blogspot.com/2010/05/syntaxhighlighter-bloggerjavascript.html

step4
開啟google blogger,「設計」>「修改html」


至(/b:template-skin)和(/head)間貼入step3複製的檔案 or(head)和(/head)之間,我本身是用前者,因為放在前比較容易查,若中間隨便放,反而不容易後續維護

step5
若要寫不同格式的程式碼,則要以不同的「檔案格式」下去寫
參考點我

程式碼寫在這


step6

完成

step7
相關資料如下
http://alexgorbatchev.com/SyntaxHighlighter/
http://sharedderrick.blogspot.com/2007/12/blogger-syntaxhighlighter.html
http://king971119.blogspot.com/2010/05/syntaxhighlighter-bloggerjavascript.html

2012年3月2日 星期五

雲端運算概念

由於本有在研究opensource search engine相關,找到一些關於雲端概念的軟體

可籍由實作方式更加了解,何謂雲端?


相關資料來源:資策會(http://www.iiiedu.org.tw/ites/CBD.htm)