[基本コントロール]---状態切替コントロールComponentButtonおよびそのサブクラスCheckBox、RadioButton、ToggleButton、switchイベントリスニングおよびシーン使用

12004 ワード

一、事件の傍受
通常のButtonでは、イベントリスニングを行うGoogle公式には、1、各buttonにイベントリスナーbuttonを設定する3つの一般的なリスニング方式が与えられている.setOnClickListener(View.OnclickListener  listener);この方法はbuttonボタンが多い場合、コードが多く、乱れ、簡潔さが足りない.
2、ActivityでインタフェースViewを実現する.OnclickListenerは、void onClick(Viewv)メソッドを書き換え、switch(v.getId()によって異なるButtonを区別する.この方法は比較的簡潔であるが、Viewを実現する必要がある.OnclickListenerインタフェース.3、xmlレイアウトで傍受したいもの
buttonに属性を追加:android:onClick="doClick"属性.Activityに傍受メソッドpublic void doClick(View view){}を追加します.このメソッドは、簡単で明瞭で、追加のインタフェースを実現する必要はありません.この方法を推奨します.Google公式ドキュメントでよく使われる使い方でもあります.
ステータス切り替えコントロールComponentButtonでは、イベントがトリガーするリスニングだけでなく、ステータス切り替えのリスニングも行います.したがって、CompoundButtonでは、イベントトリガ、ステータス切り替えの2つのリスニングが必要です.傍受の方式は通常のButtonの3つの傍受方式と似ている.傍受状態が1つ増えたにすぎない
ただです.余計なことを言ってもくだらないことばかり言っているのか,それとも直接番号をつけているのか.
シーン1:UIインタフェース上の複数のComponentButtonのイベントリスニングを統一的に処理する.
<ToggleButton

        android:id="@+id/togglebutton"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="doClick"

        android:textOff=" "

        android:textOn=" " />



    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >



        <CheckBox

            android:id="@+id/checkbox_meat"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />



        <CheckBox

            android:id="@+id/checkbox_cheese"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />

    </LinearLayout>



    <RadioGroup

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >



        <RadioButton

            android:id="@+id/radiobutton_add"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />



        <RadioButton

            android:id="@+id/radiobutton_delete"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />



        <RadioButton

            android:id="@+id/radiobutton_update"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />



        <RadioButton

            android:id="@+id/radiobutton_seach"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="doClick"

            android:text=" " />

    </RadioGroup>

レイアウトがあり、以下のJavaコードはすべてのComponentButtonコントロールを統一的に傍受します.
/**

     *  。

     * @param view

     */

    public void doClick(View view) {

        //1、 :toogle isChecked==on

        boolean isChecked=((CompoundButton)view).isChecked();////2、 

        switch (view.getId()) {

        case R.id.togglebutton:

            if(isChecked){

                Log.i("MyInfo", " ");

            }else{

                Log.i("MyInfo", " ");

            }

            break;

        case R.id.checkbox_meat:

            if(isChecked){

                Log.i("MyInfo", " ");

            }else{

                Log.i("MyInfo", " ");

            }

            break;

        case R.id.checkbox_cheese:

            

            break;

        case R.id.radiobutton_add:// :RadioButton , 。  if(isChecked)

            if(isChecked)

            break;

        case R.id.radiobutton_delete:

            if(isChecked)

            

            break;

        case R.id.radiobutton_update:

            if(isChecked)

            break;

        case R.id.radiobutton_seach:

            if(isChecked)

            break;

        default:

            break;

        }

    }

doClick()メソッドでは、全体的に2つのステップが実行されます.1が選択されます.---->2がクリックされます.通常、この2つのステップは、クリックされる---->が選択される順番である必要があります.しかし、このようにするには、各サブコントロールブランチに選択されているかどうかの判断を追加する必要があり、コードが重複しているように見えます.
ここでは逆クリック---を使用して選択されます.選択されたステップでアップシフトを使用するのは、すべてのComponentButtonサブクラスのステータスを取得できるようにするためです.ある特定のサブクラスに直接強く移行すると,汎用性がなく,すべてのComponentButtonを判断するのに適応しない.
サブクラスが選択されている状態.
UIインタフェースのステータス切り替えコントロールComponentBuutonと通常のButtonが存在する場合、android:onClick="compoundButtonClick"とandroid:onClick="buttonClick"のような2つのコントロールの使用方法をリスニングすることをお勧めします.
 
二、ComponentoundButton拡張
---未完待機