【问题标题】:Android drawable selector state pressed is true the button must change background colorAndroid可绘制选择器状态按下为真按钮必须更改背景颜色
【发布时间】:2012-08-13 08:09:12
【问题描述】:

这一定是一个愚蠢的问题,因为 stackoverflow 中有很多相关的问题,也许答案很简单。但无论如何,它就在这里。 我有一个按钮并为它创建一个可绘制的 xml。 所以在我的按钮下面是代码:

<Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="30dip"           
            android:background="@drawable/button2" 
            />

现在,在我的 drawable/button2 下,我创建了一个选择器,在该选择器中我没有使用图像。 这是我的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#FF9E35" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
 </item>
 <item>
    <shape>
        <gradient
            android:startColor="#FF9E35"
            android:endColor="#992f2f"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
    </item>
   </selector>

现在我要完成的是,当我单击该按钮时,它会将其背景颜色更改为 state_pressed 所暗示的内容,如果单击另一个按钮(很可能是切换按钮)则恢复正常 无论如何要在代码后面或 xml 选择器中执行此操作吗?

谢谢,

JC

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您实际上可以在 Java 代码中执行的操作是:

    public class LaunchActivity extends Activity implements OnTouchListener{
    
        private Button yourButton;
    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.main);      
    
            yourButton= (Button)findViewById(R.id.yourButton);
            yourButton.setOnTouchListener(this); 
    
        }
    
        @Override
        public boolean onTouch(final View view, MotionEvent event) {
    
        final int action = event.getAction();
    
            if(view.getId()==R.id.yourButton){
                if(action == MotionEvent.ACTION_DOWN)
                      yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
                if(action == MotionEvent.ACTION_UP)
                      yourButton.setBackgroundResource(R.drawable.ic_button_normal);
            }
    
     }
    }
    

    如果他被按下,这将改变按钮的背景。当用户松开手指时,它会再次改变背景。如果您只想在按下另一个按钮时更改背景,请不要写 ACTION_UP 情况,而在另一个按钮的 ACTION_DOWN 情况下,更改其他按钮的背景,请参见下文:

     @Override
            public boolean onTouch(final View view, MotionEvent event) {
    
        final int action = event.getAction();
    
            if(view.getId()==R.id.yourButton){
                if(action == MotionEvent.ACTION_DOWN){
                      secondButton.setBackgroundResource(R.drawable.ic_button_normal);
                      yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
                }
            }
                if(view.getId()==R.id.secondButton){
                if(action == MotionEvent.ACTION_DOWN){
                      yourButton.setBackgroundResource(R.drawable.ic_button_normal);
                      secondButton.setBackgroundResource(R.drawable.ic_button_pressed);
                }
            }
    
     }
    

    希望对你有帮助!

    【讨论】:

    猜你喜欢
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2014-11-30
    • 2021-07-05
    • 2018-10-01
    相关资源
    最近更新 更多