:::

3-6 src\com\demo\android\bmi\Main.java

package com.demo.android.bmi;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    protected static final int MENU_ABOUT = Menu.FIRST;
    protected static final int MENU_QUIT = Menu.FIRST+1;
	private Button button_calc;
	private EditText field_height;
	private EditText field_weight;
	private TextView view_result;
	private TextView view_good_weight;
	private TextView  view_suggest;


	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findviews();
        setListensers();
        
    }

	private void findviews() {
		button_calc = (Button)findViewById(R.id.submit);
		field_height=(EditText)findViewById(R.id.height);
		field_weight=(EditText)findViewById(R.id.weight);
		view_result=(TextView)findViewById(R.id.result);
		view_suggest=(TextView)findViewById(R.id.suggest);
		view_good_weight=(TextView)findViewById(R.id.goodWeight);
		
	}
	
    private void setListensers() {
    	button_calc.setOnClickListener(calcBMI);

	}


	private  OnClickListener calcBMI = new OnClickListener(){
    	public void onClick(View v){
    		DecimalFormat nf = new DecimalFormat("0.0");
    		try{
        		
        		double height = Double.parseDouble( field_height.getText().toString() ) / 100;
        		double weight = Double.parseDouble( field_weight.getText().toString() );
        		double BMI=weight / ( height * height );
        		
        		view_result.setText( getText( R.string.bmi_result  ) + nf.format(BMI) );

        		double min_weight=20 * ( height * height );
        		double max_weight=25 * ( height * height );
        		view_good_weight.setText( getText( R.string.goodWeight ) + nf.format(min_weight) + "~" + nf.format(max_weight) );
        		
        		//建議    		
        		if(BMI > 25){
        			 view_suggest.setText(R.string.advice_heavy);
        		}else if(BMI < 20){
        			view_suggest.setText(R.string.advice_light);
        		}else{
        			view_suggest.setText(R.string.advice_average);
        		}
    			
    		}catch(Exception obj){
    			
    			//隱現視窗,LENGTH_SHORT短訊息
    			Toast.makeText(Main.this, R.string.errMsg1, Toast.LENGTH_SHORT).show();
    			
    		}
    	}
  	
    };


	private void openOptionsDialog() {
		//建立具名實體,會佔記憶體到關閉程式為止 Main.this 是指建立在目前畫面之上
		/*
		AlertDialog.Builder dialog = new  AlertDialog.Builder(Main.this);
		dialog.setTitle("關於BMI App");
		dialog.setMessage("這只是我練習的一個小App啦~");
		dialog.show();
		*/
		
		//建立對話框匿名實體,結束就釋放記憶體,適合短暫秀出
		
		new  AlertDialog.Builder(Main.this)
		.setTitle(R.string.aboutTitle)
		.setMessage(R.string.aboutContent)
		.setPositiveButton(R.string.label_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
					}
				})
		.setNegativeButton(R.string.label_homepage,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						//連到某個網址,地圖可用 geo: 經度 , 緯度,電話tel:2130669
						// Uri.parse不支援資源識別符號,所以用getString()取得實際值
						Uri uri = Uri.parse( getString( R.string.label_homepage_url ) );
						//"意圖"實體,ACTION_VIEW 根據不同檔案類型,自動執行關聯軟體
						//ACTION_DIAL可撥電話
						Intent intent = new Intent(Intent.ACTION_VIEW , uri);
						//執行意圖
						startActivity(intent);
					}
				})		
		.show();
		
	}  

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		//menu.add(0 , MENU_ABOUT , 0 , R.string.menu_about).setIcon(R.drawable.about);
		//menu.add(0 , MENU_QUIT , 0 , R.string.menu_quit).setIcon(R.drawable.close);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		//case MENU_ABOUT:
		case R.id.about:
			openOptionsDialog();
			break;
			
		//case MENU_QUIT:
		case R.id.finish:
			finish();
			break;		
		}
		return super.onOptionsItemSelected(item);
	}
    
    
}

:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D593%26tbsn%3D21

書籍目錄

展開 | 闔起

線上使用者

59人線上 (9人在瀏覽線上書籍)

會員: 0

訪客: 59

更多…