【发布时间】:2014-05-02 04:32:56
【问题描述】:
点击后我需要在按钮上显示不同的颜色,因为用户需要知道按钮是
点击。
我不明白该怎么做?
给我建议。 button click shoud be programatically,无需为此创建XML
//XML file saved at res/drawable/button_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
在 JAVA 中
我是这样创作的
Button Settings_Button = new Button(this) ;
Settings_Button.setClickable(true);
//Settings_Button.setBackgroundResource(R.drawable.selector);
Settings_Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent newIntent = new Intent(activity.getBaseContext(), ProfileSettingActivity.class);
// activity.startActivity(newIntent);
}
});
但是这不起作用
EDIT :如何以编程方式在单击按钮时更改背景颜色。
【问题讨论】:
-
先告诉我你要把
Color或Image设置成你的Button什么? -
取消注释设置按钮选择器资源的行会发生什么?
-
使用该活动时强制关闭@TheDude
-
你为什么不做 setBackgroundResource(R.drawable.button_bg);