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 -->
我想将样式(背景和文本颜色)更改为单击按钮,并将所有其他按钮(单击按钮除外)设置为默认(未单击)样式。
请问我怎样才能以正确的方式做到这一点?
非常感谢您的任何建议。
在您的按钮上使用 setOnTouchListener 并检查 ACTION_DOWN
、ACTION_CANCEL
、ACTION_UP
MotionEvent
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;
}
});
OnClickListener:
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);
我有以下布局:
<!-- 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 -->
我想将样式(背景和文本颜色)更改为单击按钮,并将所有其他按钮(单击按钮除外)设置为默认(未单击)样式。
请问我怎样才能以正确的方式做到这一点?
非常感谢您的任何建议。
在您的按钮上使用 setOnTouchListener 并检查 ACTION_DOWN
、ACTION_CANCEL
、ACTION_UP
MotionEvent
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;
}
});
OnClickListener:
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);