Android drawable selector state pressed为真按钮必须更改背景颜色
这一定是个愚蠢的问题,因为stackoverflow中有这么多相关的问题,也许答案很简单。但无论如何,它在这里。 我有一个按钮,为它创建了一个可绘制的xml。 在我的按钮下面是代码:Android drawable selector state pressed为真按钮必须更改背景颜色,android,xml,Android,Xml,这一定是个愚蠢的问题,因为stackoverflow中有这么多相关的问题,也许答案很简单。但无论如何,它在这里。 我有一个按钮,为它创建了一个可绘制的xml。 在我的按钮下面是代码: <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent"
<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>
现在我试图做到的是,当我点击那个按钮时,它会将其背景颜色更改为按下的状态,并在点击另一个按钮时返回正常状态(很可能是切换按钮)
究竟是在代码隐藏还是在xml选择器中执行此操作
谢谢
JC在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);
}
}
}
如果他被按下,按钮的背景就会改变。当用户松开手指时,它会再次改变背景。如果只想在按下另一个按钮的情况下更改背景,请不要在另一个按钮的“操作”上写下背景,而在另一个按钮的“操作”下写下背景,请更改其他按钮的背景,请参见以下内容:
@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);
}
}
}
希望有帮助 先生,您的答案需要一个图像我试图暗示的是使用XML您可以使用XML查看这个示例,但功能性是相同的:)