答案 0 :(得分:0)
您可以使用选择器:
1)将文件添加到src / main / res / drawable button_accent.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/rect_rounded_accentdark"/>
<item android:drawable="@drawable/rect_rounded_accent" />
</selector>
rect_rounded_accent.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid
android:color="@color/colorAccent"/>
<corners
android:radius="4dp"/>
</shape>
</item>
</selector>
rect_rounded_accentdark.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid
android:color="@color/colorAccentDark"/>
<corners
android:radius="4dp"/>
</shape>
</item>
</selector>
2)使用button_accent.xml作为背景视图 例如:
<TextView
android:id="@+id/enter"
android:text="@string/authorize"
android:textAllCaps="true"
android:textSize="14dp"
android:textColor="@color/white"
android:gravity="center"
android:background="@drawable/button_accent"
android:layout_width="match_parent"
android:layout_height="43dp"/>
答案 1 :(得分:0)
如果您想将按钮的背景颜色更改为红色,例如:
//declaring a button
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
//initializing the button
button=(Button)findViewById(R.id.yourButton);
LongButoonClick();
}
public void LongButoonClick(){
//initializing a long Clicklistener for the button
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//here we will change the button color to red
button.setBackgroundColor(Color.RED);
return false;
}
}
);
}