Activity

5885 단어 Activity
res/layout
<?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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/height"
	/>
<EditText android:id="@+id/height"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numeric="integer"
    android:text=""
	/>
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/weight"
	/>
<EditText android:id="@+id/weight"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numeric="integer"
    android:text=""
	/>
<Button android:id="@+id/submit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/bmi_btn"
	/>
<TextView android:id="@+id/result" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
	/>
<TextView  android:id="@+id/suggest"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
	/>
</LinearLayout>

res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">  , Bmi!</string>
    <string name="app_name">BMI_App</string>
    <string name="height">  (cm)</string>
    <string name="weight">  (Kg)</string>
    <string name="bmi_btn">  BMI</string>
    <string name="bmi_result">BMI_App</string>
     <string name="about_title">  Android BMI</string>
    <string name="about_msg">Android BMI Calc
xxx

gasolin+android [at] gmail.com</string> <string name="ok"> </string> </resources>

src
package com.demo.bmi;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
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 Bmi extends Activity {
	private Button button;
	private EditText fieldHeight;
	private EditText fieldWeight;
	private TextView result;
	private TextView suggest;
	
	   //new    
//  private OnClickListener clickListener = new OnClickListener()
	private Button.OnClickListener clickListener  = new Button.OnClickListener(){
		@Override
		public void onClick(View v) {			
			try {
				double height = Double.parseDouble(fieldHeight.getText().toString())/100;
				double weight = Double.parseDouble(fieldWeight.getText().toString());
				double bmi = weight/(height*height);
				
				DecimalFormat df = new DecimalFormat("0.00");
				
				result.setText("  BMI " + df.format(bmi));
				
				if(bmi>25){
					suggest.setText(R.string.advice_heavy);
				}else if(bmi<20){
					suggest.setText(R.string.advice_light);
				}else{
					suggest.setText(R.string.advice_avg);
				}
			} catch (NumberFormatException e) {
				Toast.makeText(Bmi.this, "     ", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
			}	
			//    
			openOptionsDialog();
		}  	
  };
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        fineView();
        setLintener();
    }
    
	
	//       
	private void fineView(){
		 button = (Button) findViewById(R.id.submit);
		 fieldHeight = (EditText) findViewById(R.id.height);
		 fieldWeight = (EditText) findViewById(R.id.weight);
		 result = (TextView) findViewById(R.id.result);
		 suggest = (TextView) findViewById(R.id.suggest);
	}
	
	//    
	private void setLintener(){
		button.setOnClickListener(clickListener);
	}	
	
	//    
	private void openOptionsDialog(){
		/*
		new AlertDialog.Builder(Bmi.this)
			.setTitle(R.string.about_title)
			.setMessage(R.string.about_msg)
			.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
            	 public void onClick(
                 	DialogInterface dialoginterface, int i){
                 }
        })
			.show();
		*/
		
		Toast.makeText(Bmi.this, "BMI    ", Toast.LENGTH_SHORT).show();
	}
    
 
    
    
    
}

좋은 웹페이지 즐겨찾기