单击事件按钮后颜色应保持不变
after click event button color should remain same
我有两个按钮..按钮 A 和按钮 B..我将两者的背景都设置为白色..我想要的是当用户单击按钮 A 时,背景颜色应变为黑色并且应保持黑色直到用户单击在按钮 B..
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient
android:angle="-90"
android:startColor="#a11005"
android:endColor="#d62608" />
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<solid android:color="#e0341e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient
android:angle="-90"
android:startColor="#ff6c52"
android:endColor="#e0341e" />
</shape>
</item>
</selector>
我会更改您 activity 文件中按钮的背景颜色。
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setBackgroundColor(000000);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setBackgroundColor(FFFFFF);
}
});
只需在您的按钮上添加 2 个 onclick 侦听器,如果您单击 button1,则更改 button1 的背景颜色,如果您单击 button2,则将其更改回来。
以下是我使用的一些示例 XML 按钮(您可能需要稍微更改一下):
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bg"
android:layout_alignParentBottom="true"
android:text="@string/action_settings" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bg"
android:layout_alignParentBottom="true"
android:text="@string/action_settings" />
您只需使用以下代码即可做到这一点:
button1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
button1.setPressed(true);
button2.setPressed(false);
return true;
}
});
我会向可绘制对象的 xml 添加激活状态 (android:state_activated="true"
),并在单击按钮时更改另一个按钮的 setActivated(true/false)
。
一种方法是您应该为按钮的单击事件创建两个 xml 文件。
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
final Button b1 = new Button (MyClass.this);
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever1));
这里 whatever,whatever1 是你创建的 xml 文件。
另一种方法是,
b.setOnClickListener(new View.onClick Listener){
@Override
public void onClick(View v) {
button.setBackgroundColor(000000);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b1.setBackgroundColor(FFFFFF);
}
});
我有两个按钮..按钮 A 和按钮 B..我将两者的背景都设置为白色..我想要的是当用户单击按钮 A 时,背景颜色应变为黑色并且应保持黑色直到用户单击在按钮 B..
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient
android:angle="-90"
android:startColor="#a11005"
android:endColor="#d62608" />
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<solid android:color="#e0341e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<stroke android:width="1dip" android:color="#c53e2b" />
<gradient
android:angle="-90"
android:startColor="#ff6c52"
android:endColor="#e0341e" />
</shape>
</item>
</selector>
我会更改您 activity 文件中按钮的背景颜色。
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setBackgroundColor(000000);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button1.setBackgroundColor(FFFFFF);
}
});
只需在您的按钮上添加 2 个 onclick 侦听器,如果您单击 button1,则更改 button1 的背景颜色,如果您单击 button2,则将其更改回来。
以下是我使用的一些示例 XML 按钮(您可能需要稍微更改一下):
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bg"
android:layout_alignParentBottom="true"
android:text="@string/action_settings" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bg"
android:layout_alignParentBottom="true"
android:text="@string/action_settings" />
您只需使用以下代码即可做到这一点:
button1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
button1.setPressed(true);
button2.setPressed(false);
return true;
}
});
我会向可绘制对象的 xml 添加激活状态 (android:state_activated="true"
),并在单击按钮时更改另一个按钮的 setActivated(true/false)
。
一种方法是您应该为按钮的单击事件创建两个 xml 文件。
final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
final Button b1 = new Button (MyClass.this);
b1.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever1));
这里 whatever,whatever1 是你创建的 xml 文件。
另一种方法是,
b.setOnClickListener(new View.onClick Listener){
@Override
public void onClick(View v) {
button.setBackgroundColor(000000);
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b1.setBackgroundColor(FFFFFF);
}
});