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新增