2012年2月5日 星期日

[TQC+ Android] 1-2 電費計算機 Use Spinner




在選擇伏特 , 輸入安培及輸入每天使用時數與每度電費後 , 按下寄算電器每月電費按鈕後即可在下方顯示金額 , 並可依模擬器語系變更程式語系 , 這題要測驗的是Android多國語系的設定 , 也就是values資料夾的命名 , values-en 和 values-zh , 將中英文介面中所要顯示的文字分別宣告在兩個資料夾內的string.xml中 , 再來就是 RelativeLayout的配置  , 以下是程式碼。





package COM.TQC.GDD01;


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


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);
        
        MyOutcome = (TextView) this.findViewById(R.id.respond);
        
        input_a = (EditText) this.findViewById(R.id.input_a);
        input_hr = (EditText) this.findViewById(R.id.input_hr);
        input_m = (EditText) this.findViewById(R.id.input_m);
        
        submit = (Button) this.findViewById(R.id.submit);
        submit.setOnClickListener(BOnClickListener);
        
        sp = (Spinner) this.findViewById(R.id.input_v);         
        ArrayAdapter adpAdapter = ArrayAdapter.createFromResource(this, R.array.option_vs , android.R.layout.simple_spinner_item);
        adpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adpAdapter); 
        sp.setOnItemSelectedListener(SOnItemSelectedListener);        
    }
    
    TextView MyOutcome;
    EditText input_a , input_hr , input_m;
    Spinner sp;
    Button submit;
    int volt , sum;
    final int VOLT_110v = 0 , VOLT_220v = 1;    
    
    public Button.OnClickListener BOnClickListener = new Button.OnClickListener()
    {
     
  @Override
  public void onClick(View v)
  {
   // TODO Auto-generated method stub
   int ampere = Integer.parseInt(input_a.getText().toString());
   int hour = Integer.parseInt(input_hr.getText().toString());
   int month = Integer.parseInt(input_m.getText().toString());
      
   sum = (int) (volt * ampere / 1000 * hour * 30 * month);
   MyOutcome.setText("$ "+sum);   
  }     
    };
    
    public Spinner.OnItemSelectedListener SOnItemSelectedListener = new Spinner.OnItemSelectedListener()
    {
  @Override
  public void onItemSelected(AdapterView arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   switch(arg2)
   {
     case VOLT_110v:      
      volt = 110;
      break;
     case VOLT_220v:
      volt = 220;
      break;
   }   
  }

  @Override
  public void onNothingSelected(AdapterView arg0) {

   // TODO Auto-generated method stub   

  }     

    };

}

再來是 main.xml



    

        

        

        

        

        

        

        

        

        

        



P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....



沒有留言:

張貼留言

Google Analytics