2011年6月17日 星期五

Spinnertest網路範例

這個範例是下拉式選單,
當你在網路填寫問卷或者生日時,
通常都會有這類的下拉式選單,
只要把字面上改一下就能夠有不同的功用唷

範例 Spinnertest


程式碼部分
main

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget29"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="120px"
android:layout_y="112px"
>
</Spinner>
</AbsoluteLayout>

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

主程式部分

package com.demo.android.spinnertest;
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.Toast;

public class Spinnertest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
//建立一個ArrayAdapter物件,並且存放下拉式選單的內容
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,new String[]{"Screen","Keyboard","Mouse"});
//設定Spinner的樣式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//設定adapter 將剛剛的下拉式選單內容 給這個widget
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(spinnerListener);
}
private void findview() {
spinner = (Spinner) findViewById(R.id.spinner);
}
private Spinner spinner;
//監聽下拉式選單 是否被選擇
private Spinner.OnItemSelectedListener spinnerListener= new Spinner.OnItemSelectedListener()
{
//如果被選擇
public void onItemSelected(AdapterView<?>adapterView, View v, int position, long id)
{
//利用Toast來顯示 Toast.LENGTH_LONG則表示 顯示時間長
//Spinnertest.this 則代表指向Spinnertest這個實例 若是寫成this則是指向OnItemSelectedListener這個實例
//最後用show()將Toast 元件顯示在螢幕上
Toast.makeText(Spinnertest.this, "You select"+adapterView.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
}
//若是沒有選擇任何項目
public void onNothingSelected(AdapterView<?>adapterView)
{
Toast.makeText(Spinnertest.this, "You don't select anything", Toast.LENGTH_LONG).show();
}
};

}


執行畫面截圖





這個如果在搭配上其他的物件,
例如說時間或者是其他按鈕時,
就會有不一樣的程式嚕。

ProgressBar網路範例

這次找的範例是ProgressBar
這是我們玩遊戲或者跑程式常看到的讀取條

連結  ProgressBar

程式碼如下

main部分

<?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/hello" 
    /> 
<ProgressBar 
    android:id="@+id/progressbar"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:max="100" 
    /> 
</LinearLayout> 


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

主程式部分

package com.AndroidProgressBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
public class AndroidProgressBar extends Activity {

 ProgressBar myProgressBar;
 int myProgress = 0;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
      
        myProgressBar = (ProgressBar)findViewById(R.id.progressbar);
        myProgressBar.setProgress(myProgress);
       
        new Thread(new Runnable(){
   public void run() {
    // TODO Auto-generated method stub
    while(myProgress<100){
     try{
      myHandle.sendMessage(myHandle.obtainMessage());
      Thread.sleep(1000);
     }catch(Throwable t){
     }
    }
   }
        }).start();
       
    }
   
    Handler myHandle = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   myProgress++;
   myProgressBar.setProgress(myProgress);
  }
    };
}




執行的畫面截圖如下






大家可以多多參考嚕!!

RadioButton範例

想做Android物件必須要學習很多工具
然而在學習必須要有範例
以下是在網路上的RadioButton範例
RadioButton範例


我把程式碼部分列在下方


main 的部分


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="320px"
android:layout_height="430px"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_x="0px"
android:layout_y="2px"
>
<RadioButton
android:id="@+id/maleradio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Man"
>
</RadioButton>
<RadioButton
android:id="@+id/femaleradio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Woman"
>
</RadioButton>
</RadioGroup>
</AbsoluteLayout>

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

程式碼部分

package com.android.demo.radiobutton;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Radiobutton extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlistener();
}
private RadioGroup myradiogroup;
private void findview()
{
myradiogroup=(RadioGroup)findViewById(R.id.radiogroup);
}
private void setlistener()
{
myradiogroup.setOnCheckedChangeListener(changeradio);
}
//監聽radiogroup裡面的radiobutton是否有被使用者圈選
private RadioGroup.OnCheckedChangeListener changeradio=new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
//透過id來辨認不同的radiobutton
switch(checkedId)
{
case R.id.maleradio:
Toast.makeText(Radiobutton.this,"You are a Man",Toast.LENGTH_LONG).show();
break;
case R.id.femaleradio:
Toast.makeText(Radiobutton.this,"You are a Woman",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(Radiobutton.this,"Someting error",Toast.LENGTH_LONG).show();
}
}
};
}




執行出來畫面截圖



這就是執行出來的結果,
然而這在很多東都用的到,
例如製作填寫網路問卷時,
這些簡單的東西累積起來也是很有用的東西唷。




2011年6月16日 星期四

分享自己的小作品 (Android回答問題遊戲)

自己製作的回答問題遊戲

用很簡單的東西來做出一些小遊戲
或者做出一些輔助程式,也是不錯的唷

以下使用了 CheckBox、Button、TextView、RatingBar

主程式為

package edu.fcu;
import org.w3c.dom.Text;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.widget.Button;
import java.awt.*;
import android.widget.RatingBar;
public class A123456Activity extends Activity {
    /** Called when the activity is first created. */
 protected String a,b,c;
 protected float d=0;
 protected TextView text1,text2;
 protected CheckBox box1,box2,box3,box4;
 protected Button but1;
 protected RatingBar rat1;
 protected int i=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        text1 = (TextView)A123456Activity.this.findViewById(R.id.textView1);
        text2 = (TextView)A123456Activity.this.findViewById(R.id.textView2);
        box1=(CheckBox)A123456Activity.this.findViewById(R.id.checkBox1);
        box2=(CheckBox)A123456Activity.this.findViewById(R.id.checkBox2);
        box3=(CheckBox)A123456Activity.this.findViewById(R.id.checkBox3);
        box4=(CheckBox)A123456Activity.this.findViewById(R.id.checkBox4);
        but1=(Button)A123456Activity.this.findViewById(R.id.button1);
        rat1=(RatingBar)A123456Activity.this.findViewById(R.id.ratingBar1);
       
        but1.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
          d=0;
          a="好厲害!!你全對了~~按下確認答案進行下一題";
          b="你確定?? 好像有問題喔";
   if(i==0){
    text1.setText("1+1=?");
    box1.setText("2");
    box2.setText("Two");
    box3.setText("二");
    box4.setText("11");
          if (box1.isChecked()==true){
           d=d+1;
          }
   
          if (box2.isChecked()==true){
           d=d+1;
          }
         
          if (box3.isChecked()==true){
           d=d+1;
          }
          if (box4.isChecked()==false){
           d=d+2;
          }
          }
  
   if (i==1){
    text1.setText("這是不是一款問答遊戲?");
    box1.setText("不是");
    box2.setText("是");
    box3.setText("一定是");
    box4.setText("根本就是");
          if (box1.isChecked()==false){
           d=d+2;
          }
   
          if (box2.isChecked()==true){
           d=d+1;
          }
         
          if (box3.isChecked()==true){
           d=d+1;
          }
          if (box4.isChecked()==true){
           d=d+1;
          }
   }
   if(i==2){
    text1.setText("這款0元大富翁好玩嗎?");
    box1.setText("好玩");
    box2.setText("難玩");
    box3.setText("超好玩");
    box4.setText("不錯玩");
          if (box1.isChecked()==true){
           d=d+1;
          }
   
          if (box2.isChecked()==false){
           d=d+1;
          }
         
          if (box3.isChecked()==true){
           d=d+2;
          }
          if (box4.isChecked()==true){
           d=d+1;
          }
      }
  
   if(i==3){
    text1.setText("成績會有幾分");
    box1.setText("100分");
    box2.setText("滿分");
    box3.setText("最高分");
    box4.setText("夢醒時分?!");
          if (box1.isChecked()==true){
           d=d+1;
          }
   
          if (box2.isChecked()==true){
           d=d+1;
          }
         
          if (box3.isChecked()==true){
           d=d+1;
          }
          if (box4.isChecked()==false){
           d=d+2;
          }
      }
 
  
  
         if (d<5) {
          text2.setText(b);
         }
         if (d>=5){
          text2.setText(a);
          i=i+1;
          box1.setChecked(false);
          box2.setChecked(false);
          box3.setChecked(false);
          box4.setChecked(false);
            if (i>3)
             i=0;
         }
         rat1.setRating(d); 
   }
        });
       
       
    }
}

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

接下來是main的程式碼
<?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_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="1+1=?"
    android:id="@+id/textView1">
    </TextView>
   
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/checkBox1"
android:text="2" >
</CheckBox>
<CheckBox android:text="二"
android:id="@+id/checkBox2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 </CheckBox>

<CheckBox android:text="Two"
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<CheckBox android:text="じゅういち"
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/button1"
android:text="確認答案">
</Button>
<TextView android:text="評語"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<RatingBar android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RatingBar>
</LinearLayout>


遊戲截圖

新一代手機軟體-eclipse

相信對於手機有留意的朋友們
對Android手機印象深刻吧!!

沒錯,接下來就是要分享
用eclipse來製作Android手機軟體
以及利用SDK Manager來模擬Android手機

首先,介紹一個不錯的JAVA部落格
http://tccnchsu.blogspot.com/
程式設計工藝大師
裡面有介紹著很多JAVA的知識給新鮮人
接下來要使用的軟體也必須要有JAVA的人才能用唷
如果沒有JAVA程式就參考大師的內容唄

首先 點選 eclipse 進入之後右上角會有一個 Download eclipse

點選進去 選擇Eclipse Classic 3.6.2  旁邊有分32或64位元
根據你的電腦下載(win7有些是64位元,XP一般都是32位元),然後安裝

另外要下載 SDK Manager來模擬Android手機
然而在這裡 SDK簡介與下載
就可以完成安裝以及使用SDK
SDK一開始安裝後,要下載很多套件等,
 前面打勾的部分就是等一下會安裝的東西
你可以篩選一下哪些要裝的,如果不知道就直接按
右下角吧(大部分都安裝上去,可是會比較久一點)

安裝完後測試一下 ,
首先點選 左邊Virtual devices

然後點選new後,取名字,如圖(這裡用Text1當範例)


完成後點選Create AVD
然後上一張圖的點選start...  就可以了

接下來開啟eclipse,首次使用他會問你要把以後的資料放在哪裡的workspace資料夾
(一般都是用workspace名稱的資料夾,路徑可以自己設定),完成後按下ok。

接下來的請依照SDK簡介與下載 來做第一個Android吧