【问题标题】:Buttons strange behavior when pressing按下时按钮奇怪的行为
【发布时间】:2013-12-23 22:39:14
【问题描述】:

我有一个 Android 活动,它的底部有一些按钮,这些按钮默认按下一个,释放一个三个,

当它们中的任何一个被按下时,它的背景应该变成按下按钮的背景,而其他背景应该变成释放按钮的背景,但是当点击任何按钮时,我得到了这个结果:

这是我的代码:

xml:

<LinearLayout
            android:id="@+id/GrandThree"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal"
            android:background="@android:color/transparent">

            <Button
                android:id="@+id/BedRoom_bottom"
                android:layout_width="200dp"
                android:layout_height="fill_parent"
                android:background="@drawable/light_bottom_buttons_pressed"
                android:text="Bedroom"
                android:textSize="20dp"
                android:textColor="#FFFFFF"
                android:layout_gravity="center"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="pressed"
                android:clickable="true" />

            <Button
                android:id="@+id/livingroom"
                android:layout_width="200dp"
                android:layout_height="fill_parent"
                android:background="@drawable/light_bottombuttons"
                android:text="Living Room"
                android:layout_gravity="center"
                android:textSize="20dp"
                android:padding="20px"
                android:textColor="#FFFFFF"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="3dp"
                android:layout_marginBottom="3dp"
                android:layout_marginRight="3dp"
                android:tag="released"
                android:clickable="true" />
            <!.. then two buttons the same way ..!>
</LinearLayout>

JAVA:

        // the onClickListener method for the Navigation buttons
            public OnClickListener onNavigationClick = new OnClickListener() {

             @Override
                public void onClick(View v) {


                  if(v == bedRoom){

                String tag = bedRoom.getTag().toString().trim();
                    if (tag.equals("released")) {

                       bedRoom.setBackgroundColor(R.drawable.light_bottom_buttons_pressed);
                       bedRoom.setTag("pressed");
                       livingRoom.setBackgroundColor(R.drawable.light_bottombuttons);
                       livingRoom.setTag("released");
                       masterBedroom.setBackgroundColor(R.drawable.light_bottombuttons);
                       masterBedroom.setTag("released");
                       kitchen.setBackgroundColor(R.drawable.light_bottombuttons);
                       kitchen.setTag("released");

      }
   }
       //then another 3 blocks the same way for the other buttons
}
};

提示: light_bottombuttons 和 light_bottom_buttons_pressed 是渐变颜色的形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="30dp"/>

    <gradient 
        android:startColor="#353535"
        android:centerColor="#212121"
        android:endColor="#121212"  <!.. the values of the other is just upside down these values ..!>
        android:angle="270"/>

    </shape>

【问题讨论】:

    标签: android button background


    【解决方案1】:

    在您的可绘制文件夹中创建一个名为 button-drawable.xml 的文件,其中包含以下内容:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_selected="true" 
        android:@drawable/light_bottom_buttons_pressed" />
    <item 
        android:drawable="@drawable/light_bottom_buttons_pressed" />
    </selector>
    

    为布局文件中的所有按钮添加标签。

    android:background="@drawable/button_drawable"
    

    现在,在按钮的单击侦听器中,将按钮的选中状态设置为 true,任何时候选择新按钮。

    示例代码:

    public OnClickListener onNavigationClick = new OnClickListener() {
    
             @Override
                public void onClick(View v) {
    
    
                  if(v == bedRoom){
    
                String tag = bedRoom.getTag().toString().trim();
                    if (tag.equals("released")) {
    
                       bedRoom.setTag("pressed");
                       bedRoom.setSelected(true);
                       livingRoom.setTag("released");
                       livingRoom.setSelected(false);
                       masterBedroom.setTag("released");
                       masterBedroom.setSelected(false);
                       kitchen.setTag("released");
                       kitchen.setSelected(false);
    
      }
    }
        //then another 3 blocks the same way for the other buttons
    } 
    };
    

    【讨论】:

      【解决方案2】:

      为此使用单选组和单选按钮。 您的单选组将包含多个按钮,并且一次只能选择一个。

      【讨论】:

        【解决方案3】:

        当你得到你的drawable时,你应该像下面那样做

                bedRoom.setBackgroundColor(MyActivity.this.getResources().getDrawable(R.drawable.light_bottom_buttons_pressed));
        

        MyActivity 将更改为您的活动名称

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-14
          • 2014-09-20
          • 2021-05-05
          相关资源
          最近更新 更多