2012年2月10日 星期五

[TQC+ Android] 1-9 AlertDialog 顯示結果





一個簡單的表單 , 內含姓名 , 性別及興趣欄位 , 當使用者填寫完表單內的資料後 , 按下顯示結果按鈕 , 此時程是會收集表單內填寫的資訊 , 已跳出AlertDialg 的方式呈現表單輸入結果 , 這題只是單純的AlertDialog顯示我們選擇的資料 , 以下是程式碼。


package COM.TQC.GDD01;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

public class GDD01 extends Activity

{
  private EditText et;
  private RadioButton rb1;
  private RadioButton rb2;
  private CheckBox cb1;
  private CheckBox cb2;
  private CheckBox cb3;
  private Button button;
  private AlertDialog.Builder AB;
  private String content;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et = (EditText) findViewById(R.id.name);
    rb1 = (RadioButton) findViewById(R.id.rButton1);
    rb2 = (RadioButton) findViewById(R.id.rButton2);
    
    cb1 = (CheckBox) findViewById(R.id.cBox1);
    cb2 = (CheckBox) findViewById(R.id.cBox2);
    cb3 = (CheckBox) findViewById(R.id.cBox3);
    
    button = (Button) findViewById(R.id.button1);
    
    AB = new AlertDialog.Builder(this);
    AB.setTitle("結果");
    AB.setPositiveButton("離開", new OnClickListener()
    {
  @Override
  public void onClick(DialogInterface arg0, int arg1)
  {
   // TODO Auto-generated method stub
   // 作用為關閉AlertDialog  
  }     
    }
    );
        
    button.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View v)
      { 
         if(et.getText().toString().trim().length()==0) content = "姓名:未填\n";
         //防止使用者鍵入空白 , 考試時使用 getText().equals("") 即可
         else content = "姓名:"+et.getText()+"\n";
      
         if(rb1.isChecked()) content+="性別:帥哥\n興趣:";
         else content+="性別:美女\n興趣:";
         
         if(cb1.isChecked()==false&&cb2.isChecked()==false&&cb3.isChecked()==false)
          content+="未選擇";
         else
         {
          if(cb1.isChecked()) content+="吃飯 ";
             if(cb2.isChecked()) content+="睡覺 ";
             if(cb3.isChecked()) content+="上網 ";
         }         
         AB.setMessage(content).show();
      }
    });
  }
}

以下是 main.xml

  
    
      
      
    
    
      
      
        
        
      
    
    
      
      
        
        
        
      
    
  
  






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



2012年2月7日 星期二

[TQC+ Android] 1-8 選擇手機型號 Use Spinner






將Spinner元件預設為"請選擇" , 當選擇了某一手機型號時 , 將此型號接續顯示於"選擇手機型號"的TextView中 , 如選取的選項惟請選擇時(即不選擇任何型號) , TextView後方步顯示任何文字 , 一般來說在設定下拉選單的呈現樣式 , 我們都是使用Android 提供的 , 就像下面那行程式碼 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) , 而這題要求我們使用自製的TextView去替換 , 其實Android所提供的 android.R.layout.simple_spinner_dropdown_item , 裡面放的就是一個TextView , 而自製的TextView題目也做好了存放在res/layout/myspinner_layout , 以下是程式碼。


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.Spinner;
import android.widget.TextView;

public class GDD01 extends Activity
{
  private String[] types={"請選擇","HTC HERO","HTC MAGIC","HTC TATTOO",
      "NEXUS ONE","SONY X10","MOTO MILESTONE"};
  
  Spinner spinner; 
  TextView textView;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    textView = (TextView) this.findViewById(R.id.text1);
    
    ArrayAdapter AB = new ArrayAdapter(this, android.R.layout.simple_spinner_item, types);
    AB.setDropDownViewResource(R.layout.myspinner_layout);
       
    spinner = (Spinner) this.findViewById(R.id.mySpinner);    
    spinner.setAdapter(AB);   
    spinner.setOnItemSelectedListener(SOnItemSelectedListener);    
  }
  
  public Spinner.OnItemSelectedListener SOnItemSelectedListener = new Spinner.OnItemSelectedListener()
  {
 @Override
 public void onItemSelected(AdapterView arg0, View arg1, int arg2,
   long arg3)
 {
  // TODO Auto-generated method stub
  if(arg2 == 0) textView.setText(getText(R.string.str1));
  
  else textView.setText(getText(R.string.str1)+types[arg2]);
 }

 @Override
 public void onNothingSelected(AdapterView arg0)
 {
  // TODO Auto-generated method stub  
 }   
  };  
}


main.xml

  
  


myspinner_layout.xml











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



2012年2月6日 星期一

[TQC+ Android] 1-7 驗證 Activity Use Log





程式內有兩個Activity , 於Activity1按下按鈕"Go To Activity2" 時 , Activity 會被開啟 , 點選Activty2的Back to Activity1按鈕時 , 則會將Activity2關閉 , 同時回傳訊息 , 而題目希望透過Log來驗證Activity的生命週期 , 這題用意在測驗考生清不清楚 Activity Lifecycle , 如果清楚的話這題是相當簡單的 , 再配合Log顯示Activity的當前狀態即可 , 另外兩個Activity之間的資料往返 , 在記得要在AndroidManifest.xml 宣告另一支Activity , 以下是程式碼。


package COM.TQC.GDD01;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GDD01 extends Activity
{
  private static final String TAG = "Android_Log";
  private TextView tv;
  private Button b1;
  private Button b2;
  
  final int ACT1 = 1;
  private Intent intent;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv=(TextView) findViewById(R.id.text1);;
    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    
    intent = new Intent();
    intent.setClass(this, GDD1_2.class);
    
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
       startActivityForResult(intent,ACT1);
      }      
    });
    
    b2.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {
       finish();
      }
    });    
    Log.i(TAG, "onCreate()");    
  }
  
  @Override
  public void onActivityResult (int requestCode, int resultCode, Intent data)
  {
   tv.setText(""+resultCode);
  }  
  
  @Override
  public void onStart()
  {
    super.onStart();
    Log.i(TAG, "onStart()");
  }
  @Override
  public void onResume()
  {
    super.onResume();
    Log.i(TAG, "onResume()");
  }
  @Override
  public void onPause()
  {
    super.onPause();
    Log.i(TAG, "onPause()");
  }
  @Override
  public void onStop()
  {
    super.onStop();
    Log.i(TAG, "onStop()");
  }
  @Override
  public void onRestart()
  {
    super.onRestart();
    Log.i(TAG, "onRestart()");
  }
  @Override
  public void onDestroy()
  {
    super.onDestroy();
    Log.i(TAG, "onDestroy()");
  }
}

package COM.TQC.GDD01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class GDD1_2 extends Activity
{ 
 Button backButton;
 
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.mylayout);
     
     backButton = (Button) findViewById(R.id.button2);
     backButton.setOnClickListener(new Button.OnClickListener()
     {
       public void onClick(View v)
       {
        GDD1_2.this.setResult(99);
        finish();
       }
     });     
 }  
}


接下來是簡單的Layout



    


    




不要忘記AndroidManifest.xml 的宣告

    
        
            
                
                
            
        
        

    
    
 







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

[TQC+ Android] 1-6 畫廊展示 Use Gallery





可藉由游標的左右移動瀏覽照片 , 並顯示照片資料 , 這題使用到的類別 Gallery 和 Toast提示 , 另外這題所提供了10張png檔 , 而程式安裝到模擬器時會發現10張圖檔要上傳很久 , 所以我只使用6張png 以下是程式碼。


package COM.TQC.GDD01;

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.ImageView.ScaleType;
import android.widget.Toast;


public class GDD01 extends Activity 
{
  
  Gallery gallery;
  int[] drawableResID = {R.drawable.png001 , R.drawable.png002 , R.drawable.png003
                   , R.drawable.png004 , R.drawable.png005 , R.drawable.png006};
    
  @Override  
  
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    gallery = (Gallery) this.findViewById(R.id.mygallery);
    
    ImageAdapter imageAadapter = new ImageAdapter(this);
    
    gallery.setAdapter(imageAadapter);
    gallery.setOnItemSelectedListener(OISlistener);
  }
  
  Gallery.OnItemSelectedListener OISlistener = new Gallery.OnItemSelectedListener()
  {
 @Override
 public void onItemSelected(AdapterView arg0, View arg1, int arg2,
   long arg3)
 {
  Toast t = Toast.makeText(GDD01.this, getText(R.string.my_gallery_text_pre).toString() 
    + arg2 + getText(R.string.my_gallery_text_post).toString(), Toast.LENGTH_SHORT);
     t.show();
  
 }

 @Override
 public void onNothingSelected(AdapterView arg0) {
  // TODO Auto-generated method stub
  
 }
    
  };
  
  public class ImageAdapter extends BaseAdapter 
  {   
 int backgroundID;
   
   //建構子只有一個參數,即要儲存的Context
   public ImageAdapter(Context c) 
    {
    TypedArray arr = c.obtainStyledAttributes(R.styleable.Gallery);
    backgroundID = arr.getResourceId(R.styleable.Gallery_android_galleryItemBackground , 0);
    arr.recycle();
 }     
  
   //回傳所有已定義的圖片總數量
   public int getCount() 
   {
    return drawableResID.length;
   }  
  
   public Object getItem(int position) 
   {
    return drawableResID[position];
   }
  
   //取得圖片編號
   public long getItemId(int position) 
   {
    return position;
   }

   @Override
   public View getView(int arg0, View arg1, ViewGroup arg2)
   {
    // TODO Auto-generated method stub
    ImageView imageView = new ImageView(GDD01.this);
    imageView.setImageResource(drawableResID[arg0]);
    imageView.setBackgroundResource(backgroundID);
    imageView.setScaleType(ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(236,188));
    return imageView;
   }
  
  }  
    
    //使用android.R.drawable裡的圖片當成圖庫來源
}


這題的Layout也很單純 , 只要擺一個Gallery就可以了。




由於Gallery元件不支援循環播放 , 所以如果要讓圖片一直循環下去的話 , 我們要自己動手實作 , 以下是程式碼。


package COM.TQC.GDD01;

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.ImageView.ScaleType;
import android.widget.Toast;


public class GDD01 extends Activity 
{
  
  Gallery gallery;
  int[] drawableResID = {R.drawable.png001 , R.drawable.png002 , R.drawable.png003
                   , R.drawable.png004 , R.drawable.png005 , R.drawable.png006};
    
  @Override  
  
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    gallery = (Gallery) this.findViewById(R.id.mygallery);
    
    ImageAdapter imageAadapter = new ImageAdapter(this);
    
    gallery.setAdapter(imageAadapter);
    gallery.setOnItemSelectedListener(OISlistener);
  }
  
  Gallery.OnItemSelectedListener OISlistener = new Gallery.OnItemSelectedListener()
  {
 @Override
 public void onItemSelected(AdapterView arg0, View arg1, int arg2,
   long arg3)
 {
  Toast t = Toast.makeText(GDD01.this, getText(R.string.my_gallery_text_pre).toString() 
    + arg2 % drawableResID.length + getText(R.string.my_gallery_text_post).toString(), Toast.LENGTH_SHORT);
     t.show();
  
 }

 @Override
 public void onNothingSelected(AdapterView arg0) {
  // TODO Auto-generated method stub
  
 }
    
  };
  
  public class ImageAdapter extends BaseAdapter 
  {   
 int backgroundID;
   
   //建構子只有一個參數,即要儲存的Context
   public ImageAdapter(Context c) 
    {
    TypedArray arr = c.obtainStyledAttributes(R.styleable.Gallery);
    backgroundID = arr.getResourceId(R.styleable.Gallery_android_galleryItemBackground , 0);
    arr.recycle();
 }     
  
   //回傳所有已定義的圖片總數量
   public int getCount() 
   {
    return Integer.MAX_VALUE;
    
    //設定圖片數量為系統最大整數
   }  
  
   public Object getItem(int position) 
   {
    return drawableResID[position % drawableResID.length];
   }
  
   //取得圖片編號
   public long getItemId(int position) 
   {
    return position % drawableResID.length;
   }

   @Override
   public View getView(int arg0, View arg1, ViewGroup arg2)
   {
    // TODO Auto-generated method stub
    ImageView imageView = new ImageView(GDD01.this);
    imageView.setImageResource(drawableResID[arg0 % drawableResID.length]);
    imageView.setBackgroundResource(backgroundID);
    imageView.setScaleType(ScaleType.FIT_XY);
    imageView.setLayoutParams(new Gallery.LayoutParams(236,188));
    return imageView;
   }
  
  }  
    
    //使用android.R.drawable裡的圖片當成圖庫來源
}



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

2012年2月5日 星期日

[TQC+ Android] 1-5 MENU 功能選單





使用者按MENU按鈕 , 顯示MENU選單 , 這題也相當簡單 , 只是要考在menu加上items的使用和AlertDialog的設定按鈕及事件 , 以下是程式碼。




package COM.TQC.GDD01;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class GDD01 extends Activity {
 
 
 final int ADD = Menu.FIRST , DELETE = Menu.FIRST+1 , 
           ABOUT = Menu.FIRST+2 , QUIT = Menu.FIRST+3 ;
 // ...
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    //建立menu選單的項目
    public boolean onCreateOptionsMenu(Menu menu)
    {
     super.onCreateOptionsMenu(menu);
     
     //新增menu選單,再利用onOptionsItemSelected擷取選單選擇項目,處理相對應的事件
     menu.add(0, ADD, 0, R.string.menu_add);
     menu.add(0, DELETE, 1, R.string.menu_delete);
     menu.add(0, ABOUT, 2, R.string.menu_about);
     menu.add(0, QUIT, 3, R.string.menu_exit);
          
     return true ; 
    }
    //menu被選擇執行後的事件處理
    public boolean onOptionsItemSelected(MenuItem item)
    {
     //點選menu,出現四個選擇,以switch區分點選何項,設定title並繫結至其功能
     switch(item.getItemId())
     {
        case ADD:
        case DELETE:
         this.setTitle(item.getTitle());
         break;
        case ABOUT:
         openAboutDialog();
         break;
        case QUIT:
         openExitDialog();
         break;      
     }
     return super.onOptionsItemSelected(item) ;
    }
    
    //點選「關於」功能
    public void openAboutDialog()
    {     
     AlertDialog.Builder AB = new AlertDialog.Builder(this);
     AB.setMessage(R.string.menu_about_msg)
     .setTitle(R.string.menu_about)
     .setPositiveButton(R.string.str_ok, 
        new OnClickListener()
     {
   @Override
   public void onClick(DialogInterface dialog, int which)
   {
    // TODO Auto-generated method stub
    // 按下確認後會關閉Dialog即可
   }          
     }     
     )
     .show();
    }
    //點選「離開」功能
    public void openExitDialog()
    {
        //開啟視窗詢問【是否確認離開系統?】,於訊息視窗點選「確認」後,關閉程式
     //於訊息視窗點選「取消」,則關閉此詢問視窗,則回到MENU功能選單畫面
     
     AlertDialog.Builder AB = new AlertDialog.Builder(this);
     AB.setMessage(R.string.menu_exit_msg)
     .setTitle(R.string.menu_exit)
     .setPositiveButton(this.getText(R.string.str_ok), 
        new OnClickListener()
     {
   @Override
   public void onClick(DialogInterface dialog, int which)
   {
    // TODO Auto-generated method stub
    GDD01.this.finish();
   }          
     }     
     )
     .setNegativeButton(this.getText(R.string.str_cancel), 
        new OnClickListener()
     {
   @Override
   public void onClick(DialogInterface dialog, int which)
   {
    // TODO Auto-generated method stub
    // 取消關閉程式
   }          
     }     
     )
     .show();    
    }
}


這題不用額外配置layout , 所以不附上main.xml







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



Google Analytics