2011年6月10日 星期五

期末作業

建立選單(Menu)

 在這個網站找到建立選單的方法

按下選單後
按下說明

出現說明


-----------------------------------------------------------------

匯率換算器加強版

加入可選擇幣值

選擇會出現提示

支援8種不同國家幣值


-------------------------------------------------------------------------------------------------------------
NewOne.java
-----------------------
package edu.fcu.wang;

import java.text.NumberFormat;

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

public class NewOne extends Activity {
 private EditText ed1, ed2;
 private Button btn1;
 private TextView tv1;
 private Spinner sp1;
 public static final String MY_PREFS = "mSharedPreferences01";
    /** Called when the activity is first created. */
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sp1 = (Spinner)NewOne.this.findViewById(R.id.spinner1);
        ed1 = (EditText)NewOne.this.findViewById(R.id.editText1);  // 匯率
        ed2 = (EditText)NewOne.this.findViewById(R.id.editText2);  // 台幣
        btn1 = (Button)NewOne.this.findViewById(R.id.button1);
        tv1 = (TextView)NewOne.this.findViewById(R.id.textView4);
        ed1.setText("28.4");
        ed2.setText("10000");
        
        btn1.setOnClickListener(new Button.OnClickListener()
        {
          @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 按鈕事件,處理數學換算的語法
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits( 2 );
            
            double d = Double.parseDouble(ed2.getText().toString()) / Double.parseDouble(ed1.getText().toString());
            
            tv1.setText( NewOne.this.getResources().getString(R.string.str4) + ":" + nf.format(d));
}
        });
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,new String[]{"美金","英鎊","日幣","港幣","韓元","泰銖","印尼盾","歐元"});
      //設定下拉選單的樣式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter(adapter);
      //設定項目被選取之後的動作
        sp1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            public void onItemSelected(AdapterView adapterView, View view, int position, long id){
            Toast.makeText(NewOne.this, "您選擇"+adapterView.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
           
            int pos = sp1.getSelectedItemPosition();
                try
            {
                    if (pos==0)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("28.58");
                    }
                  
                    if (pos==1)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("45.51");
                    }    
                    if (pos==2)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("0.35110");
                    }    
                    if (pos==3)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("3.57600");
                    }    
                    if (pos==4)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("0.02487");
                    }    
                    if (pos==5)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("0.83610");
                    }    
                    if (pos==6)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("0.00296");
                    }    
                    if (pos==7)
                    {
                      // TODO Auto-generated method stub
                      ed1.setText("40.23000");
                    }    
                  }
                  catch(Exception err)
                      {
                        
                      
                      }
                  
            }
            public void onNothingSelected(AdapterView arg0) {
                Toast.makeText(NewOne.this, "您沒有選擇任何項目", Toast.LENGTH_LONG).show();
            }
  
        });
      }/*End: onCreate()*/

      @Override
      protected void onResume()
      {
        // TODO Auto-generated method stub
        
        SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
        // 第二個參數為 假若 "rate" 不存在,程式為第一次執行的話,要以什麼值取代
        String previousInputRate = mySharedPreferences.getString("rate", "");
        ed1.setText(previousInputRate);
        super.onResume();
      }

      public void onItemSelected()
      {
      
          }
                  
        
       
      
      protected void onStop()
      {
        // TODO Auto-generated method stub
        SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
        SharedPreferences.Editor ed = mySharedPreferences.edit();
        ed.putString("rate", ed1.getText().toString() );
        ed.commit();
        
        super.onStop();
    }
}


-------------------------------------------------------------------------------------------------------------
main.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/str1"/>

<EditText android:id="@+id/editText1" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:text="">
</EditText>

<TextView android:id="@+id/textView2" 
android:layout_height="wrap_content" 
android:text="@string/str2" 
android:layout_width="match_parent">
</TextView>

<EditText android:id="@+id/editText2" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:text="">
</EditText>
<TextView android:text="@string/str3" android:id="@+id/textView3" android:layout_height="wrap_content" android:layout_width="match_parent"></TextView>
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="match_parent"></Spinner>

<Button android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:id="@+id/button1" 
android:text="@string/str_btn1">
</Button>
<TextView android:id="@+id/textView4" android:text="@string/str4" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>