簡體   English   中英

Android:如何在布局中為單擊的按鈕着色並將未單擊的樣式設置為其他樣式?

[英]Android: How to colorize clicked button in layout and set unclicked style to others?

我有以下布局:

<!-- GROUPED BUTTONS -->
        <LinearLayout
            android:id="@+id/group_button_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:layout_centerHorizontal="true">

            <Button
                android:id="@+id/group_button_week"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:background="@color/app_blue"
                android:textSize="12sp"
                android:text="Week"/>

            <Button
                android:id="@+id/group_button_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:textSize="12sp"
                android:padding="0dp"
                android:text="Month"/>

            <Button
                android:id="@+id/group_button_quarter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:background="@color/white"
                android:textSize="12sp"
                android:text="Quarter"/>

            <Button
                android:id="@+id/group_button_half_year"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:background="@color/white"
                android:textSize="12sp"
                android:text="Half Year"/>
        </LinearLayout>
        <!-- //GROUPED BUTTONS -->

我想將樣式(背景和文本顏色)更改為單擊的按鈕,所有其他按鈕(單擊的除外)設置默認(未單擊的)樣式。

我怎樣才能以正確的方式做到這一點?

非常感謝您的任何建議。

點擊監聽器:

       final Button b1= (Button) findViewById(R.id.group_button_week);
       final Button b2= (Button) findViewById(R.id.group_button_month);
       final Button b3= (Button) findViewById(R.id.group_button_quarter);
       final Button b4= (Button) findViewById(R.id.group_button_half_year);

        View.OnClickListener listener= new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Button[] buttons=new Button[]{b1,b2,b3,b4};

                for (Button b:
                  buttons   ) {

                    if(b.equals(v)) // the clicked one
                    {
                        b.setBackgroundResource(android.R.color.darker_gray);
                        b.setTextColor(Color.WHITE);
                    }
                    else  // the others
                    {
                        b.setBackgroundResource(android.R.color.white);
                        b.setTextColor(Color.RED);
                    }
                }
            }
        };
        b1.setOnClickListener(listener);
        b2.setOnClickListener(listener);
        b3.setOnClickListener(listener);
        b4.setOnClickListener(listener);

在按鈕上使用setOnTouchListener並檢查ACTION_DOWNACTION_CANCELACTION_UP

button1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // when you touch button for click 
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                 button.setBackgroundResource(R.color.onClickColor);   
                 text.setBackgroundResource(R.color.onClickColor);
                 // set background of button and text       
            }
            // when you remove touch from button 
            if(event.getAction() == MotionEvent.ACTION_CANCEL){
                button1.setBackgroundResource(R.color.onClickColor);  
                text.setBackgroundResource(R.color.onClickColor);
                // set background of button and text    
            }
            // when you release touch from button 
            if(event.getAction() == MotionEvent.ACTION_UP){
                button1.setBackgroundResource(R.color.onClickColor);  
                text.setBackgroundResource(R.color.onClickColor);
                // set background of button and text    
            }
            return true;
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM