2015年2月4日

【Android】CheckBox元件應用

各位Android安卓開發者大家好!

今天小黑人要與大家分享的文章內容是"CheckBox元件",原因是小黑人最近蠻密切接觸CheckBox元件的功能機制,其中包含判斷CheckBox的狀態與其他元件控制切換等規則處理,想說藉由這一篇文章與大家分享CheckBox會常用到的判斷與狀態切換等功能,當然小黑人會直接以範例實作的方式來說明CheckBox元件的使用方法,就讓我們繼續看下去吧!

1. 首先小黑人先簡短說明此範例會運用到的功能,就是針對CheckBox來進行狀態的控制與判斷,其中在控制方面包含CheckBox本身點選控制與其他Button元件來控制勾選與取消,而在判斷方面則是有勾選的狀態下才能顯示下一步,否則隱藏下一步的功能畫面。

2.
版面的部分就簡單配置Layout畫面,其中包含CheckBox元件與Button元件 :

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold|italic"
        android:text="CheckBox
元件應用"
        />
   
        <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img"
        />

        <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="
小黑人的Android教室"
        />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
       
                <Button
                android:id="@+id/choose"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="
按鈕勾選"
                android:layout_weight="1"
                />
           
                <Button
                android:id="@+id/cancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="
取消選取"
                android:layout_weight="1"
                />
       
        </LinearLayout>

        <TextView
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="
下一步 >>"
        android:textStyle="bold|italic"
        android:visibility="gone"
        />

</LinearLayout>


3. 接下來在Activity(.java)裡才是讓CheckBox進行判斷與控制動作的部分 :

public class MainActivity extends Activity
{
        private CheckBox mCheckBox;
        private TextView mNextText;
       
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
               
                //
獲取Layout裡的CheckBoxTextView元件
                mCheckBox = (CheckBox) findViewById(R.id.check);
                mNextText = (TextView) findViewById(R.id.next);
               
                //
按下勾選按鈕動作
                ((Button) findViewById(R.id.choose)).setOnClickListener(new OnClickListener()
                {
                        @Override
                        public void onClick(View v)
                        {
                                //
設定CheckBox勾選狀態
                                mCheckBox.setChecked(true);
                        }
                });
               
                //
按下取消按鈕動作
                ((Button) findViewById(R.id.cancel)).setOnClickListener(new OnClickListener()
                {
                        @Override
                        public void onClick(View v)
                        {
                                //
設定CheckBox未勾選狀態
                                mCheckBox.setChecked(false);
                        }
                });

                //CheckBox
狀態改變觸發動作
                mCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
                {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
                        {
                                //
判斷CheckBox是否有勾選,同mCheckBox.isChecked()
                                if(isChecked)
                                {
                                        //CheckBox
狀態 : 已勾選,顯示TextView
                                        mNextText.setVisibility(View.VISIBLE);
                                }
                                else
                                {
                                        //CheckBox
狀態 : 未勾選,隱藏TextView
                                        mNextText.setVisibility(View.GONE);
                                }
                        }
                });
        }
}

以上是針對CheckBox常用的判斷處理方法,其實CheckBox可以運用在非常多的地方,當然也可以結合其他元件互相搭配運用,在不同的需求下可以有不同的處理方式,大家可以試試看喔!


小黑人範例畫面如下 :




謝謝大家,如有任何問題都可以和小黑人一起交流討論!

☆小黑人☆


沒有留言:

張貼留言

謝謝大家支持,有任何問題都可以和小黑人一起討論!