2014年7月28日 星期一

[Android]多選一的單選鈕 (RadioButton)

  單選按鈕即RadioButton,表示在同一群組內只能選擇一個項目。

RadioButton & RadioGroup元件
  要讓一組 RadioButton每次只能有一個被選,就必須把它們都放置在同一個 RadioGroup中。


如上圖所示,其中就有三個 RadioButton,而三個都被包在同一個 RadioGroup(你看不見)中。


getCheckedRadioButtonId():讀取單選鈕狀態
   由於 RadioButton通常是組合在 RadioGoup之下,因此在程式中要判斷使用者選擇哪一個 RadioButton,可透過 RadioGrop的 getCheckedRadioButtonId()方法,取得被選取的 RadioButton之資源 ID。接著利用判斷式(例: if/else)就可以據以決定程式的走向,其格式如下:

//取得 RadioGroup之物件
RadioGroup Iwant = (RadioGroup) findViewById(...);
    ...
//判斷選擇的項目資源 ID是不是所選擇的該項ID
if(Iwant.getCheckedRadioButtonId() == R.id.takeSword) {
    ...
}
else if(Iwant.getCheckedRadioButtonId() == R.id.cutSoul){
    ...
}
else{
    ...
}

※讀取RadioGroup的選取項目:


MainActivity.java
package tw.com.Mei_Jiang;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }
        
        public void show(View v){
                TextView txv=(TextView)findViewById(R.id.txv);
                RadioGroup ticketType =
                                (RadioGroup) findViewById(R.id.ticketType);
                
                // 依選取項目顯示不同訊息
                switch(ticketType.getCheckedRadioButtonId()){
                case R.id.sword:        // 選 拿勝利寶劍
                        txv.setText("拿勝利寶劍");
                        break;
                case R.id.soul:        // 選 斷開魂節
                        txv.setText("斷開魂節");
                        break;
                case R.id.chain:        // 選 斷開鎖練
                        txv.setText("斷開鎖練");
                        break;
                }
        }
}

備註:別忘了,R.id.XXX都是在 R.java中以 final宣告的常數,其值是固定不變的,因此可用在 switch敘述中用來區別選取的單選鈕。


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txv"
        android:layout_centerHorizontal="true"
        android:onClick="show"
        android:text="確定" />

    <RadioGroup
        android:id="@+id/ticketType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_centerHorizontal="true" >

        <RadioButton
            android:id="@+id/sword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="拿勝利寶劍" />

        <RadioButton
            android:id="@+id/soul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="斷開魂節" />

        <RadioButton
            android:id="@+id/chain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="斷開鎖練" />
    </RadioGroup>

</RelativeLayout>

2014年7月1日 星期二

[Eclipse] 常用快速鍵 及 自動完成( Auto Complete)

一、常用鍵
  • 【Ctrl+Shift+O】:自動匯入所需要的類別
  • 【Alt + /】:程式碼提示,自動補齊 Template
  • 【Ctrl+Shift+F】:程式碼自動排版
  • 【Ctrl + F11】:快速執行程式
  • 【Ctrl+Shift+P 】:轉至匹配的括號 
  • 【Ctrl+/】:將選取的文字多行註解起來
  • 【Ctrl+D】:刪除當前這一行
  • 【Ctrl+F8】:視景切換




二、快速鍵修改處

英文版:Window->Preference->General->Keys
中文版:視窗->喜好設定->一般->按鍵

備註:快捷鍵組合可在Eclipse按下Ctrl+Shift+L來查看。



三、尋找參照

  • 【F3】:找變數宣告處
  • 【Ctrl+T】:顯示繼承樹
  • 【Ctrl+Shift+T】:快速搜尋 Class
  • 【Ctrl+滑鼠左鍵點擊】:查看使用類別的原始碼
  • 【Ctrl+L】:前往某一行




四、Debug

  • 【F11】:以除錯模式啟動 (Debug)
  • 【Ctrl + F11 】:啟動 (Run)
  • 【F5】: 步進,深入一層 (Step Into)
  • 【F6】:步進,往下一行 (Step Over)
  • 【F7】: 步進,回上一層 (Step Return)
  • 【Ctrl+.】:尋找下一個error位置
  • 【Ctrl+,】:尋找前一個error位置



五、JAVA的自動完成( Auto Complete)設定

中文版:視窗 --> 喜好設定 --> Java --> 編輯器 --> 內容輔助 --> Auto Activation




英文版:Window--> Preferences --> Java --> Editor --> Content Assist --> Auto Activation




六、XML的自動完成(Auto Complete)設定

中文版:視窗 --> 喜好設定 --> XML --> XML Files --> 編輯器 --> 內容輔助 --> Auto Activation



英文版:Window --> Preferences --> XML --> XML Files --> Editor --> Content Assist --> Auto Activation



Reference