我想在 btn_presses、btn_focus 等不同状态下更改按钮文本颜色。
为此,我使用了 colorstatelist.xml,并在 titlebarlayout.xml 的按钮文本中引用了它。但我仍然无法更改按钮的文本颜色。
任何人都知道该怎么做。我在代码的任何地方都会出错。
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
EditText emailEdit, passwordEdit;
Button loginButton;
String email, password;
TitleBarLayout titlebarLayout;
String r;
String rr;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
emailEdit = (EditText) findViewById(R.id.etEmail);
passwordEdit = (EditText) findViewById(R.id.etPassword);
loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(this);
// titlebarLayout=(TitleBarLayout) findViewById(R.id.titlebar);
titlebarLayout = new TitleBarLayout(MainActivity.this);
titlebarLayout.setLeftButtonText("");
titlebarLayout.setRightButtonText("Logout");
titlebarLayout.setTitle("iProtect");
//titlebarLayout.setLeftButtonSize(50,50);
//titlebarLayout.setRightButtonSize(100,50);
titlebarLayout.setLeftButtonBackgroundColor(Color.rgb(34,49,64));
titlebarLayout.setRightButtonBackgroundColor(Color.rgb(34,49,64));
titlebarLayout.setLeftButtonTextColor(Color.rgb(255,255,255));
titlebarLayout.setRightButtonTextColor(Color.rgb(255,255,255));
XmlResourceParser parser =getResources().getXml(R.color.colorstatelist);
ColorStateList colorStateList;
try {
colorStateList = ColorStateList.createFromXml(getResources(), parser);
titlebarLayout.setLeftButtonTextColor(colorStateList);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// ColorStateList colorlist=new ColorStateList( new int[][] { new int[] { android.R.attr.state_focused }, new int[0], }, new int[] { Color.rgb(0, 0, 255), Color.BLACK, } );
// titlebarLayout.setLeftButtonTextColor(colorlist);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.left_button) {
} else if (v.getId() == R.id.right_button) {
}
}
};
titlebarLayout.setLeftButtonOnClickListener(listener);
titlebarLayout.setRightButtonOnClickListener(listener);
}
标题栏布局.java
public class TitleBarLayout {
private Activity activityRef;
private View contentView;
private Button leftButton, rightButton;
TextView titletext;
public TitleBarLayout(Activity a) {
Log.i("TitleBar Layout", "Inside constructor");
activityRef = a;
inflateViewsFromXml();
setListenersOnViews();
setValuesOnViews();
}
private void setValuesOnViews() {
leftButton.setText("");
rightButton.setText("");
}
private void setListenersOnViews() {
leftButton.setOnClickListener(listener);
rightButton.setOnClickListener(listener);
}
private final OnClickListener listener = (new OnClickListener() {
@Override
public void onClick(View v) {
activityRef.finish();
}
});
private void inflateViewsFromXml() {
contentView = activityRef.findViewById(R.id.titlebar);
rightButton = (Button) contentView.findViewById(R.id.right_button);
leftButton = (Button) contentView.findViewById(R.id.left_button);
titletext = (TextView) contentView.findViewById(R.id.title_textview);
}
public void setLeftButtonText(int resID) {
leftButton.setText(resID);
}
public void setLeftButtonText(String text) {
leftButton.setText(text);
}
public void setLeftButtonOnClickListener(View.OnClickListener listener) {
leftButton.setOnClickListener(listener);
}
public void setRightButtonText(int resID) {
rightButton.setText(resID);
}
public void setRightButtonText(String text) {
rightButton.setText(text);
}
public void setRightButtonOnClickListener(View.OnClickListener listener) {
rightButton.setOnClickListener(listener);
}
public void setTitle(int resID) {
titletext.setText("" + resID);
}
public void setTitle(String text) {
titletext.setText(text);
}
public void setLeftButtonSize(int width, int height) {
Log.i("Button" ,"Width"+width);
leftButton.setWidth(width);
leftButton.setHeight(height);
}
public void setRightButtonSize(int width, int height) {
Log.i("Button" ,"Width"+width);
rightButton.setWidth(width);
rightButton.setHeight(height);
}
public void setLeftButtonBackgroundResource(int backgroundResource) {
}
public void setRightButtonBackgroundResource(int backgroundResource) {
}
public void setLeftButtonBackgroundColor(int backgroundColor) {
Log.i("Button" ,"COLOR"+backgroundColor);
leftButton.setBackgroundColor(backgroundColor);
}
public void setRightButtonBackgroundColor(int backgroundColor) {
Log.i("Button" ,"COLOR"+backgroundColor);
rightButton.setBackgroundColor(backgroundColor);
}
public void setLeftButtonTextColor(int textcolor) {
Log.i("Button" ,"COLOR"+textcolor);
leftButton.setTextColor(textcolor);
}
public void setRightButtonTextColor(int textcolor) {
Log.i("Button" ,"COLOR"+textcolor);
rightButton.setTextColor(textcolor);
}
public void setLeftButtonTextColor(ColorStateList colorStateList) {
leftButton.setTextColor(colorStateList);
}
public void setRightButtonTextColor(ColorStateList colorStateList) {
}
}
颜色.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="title_bg_color">#2e4256</color>
<color name="layout_bg_color">#dcdcdc</color>
<color name="state_pressed">#ffffff</color>
<color name="state_selected">#00ff00</color>
<color name="state_focused">#0000ff</color>
</resources>
标题栏布局.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/titlebar_height"
android:background="@color/title_bg_color"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/left_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:text=""
android:clickable="true"
android:textColor="@color/colorstatelist"
android:background="@drawable/button_shape_drawable"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/title_textview"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text=""
android:textColor="@android:color/white"
android:textStyle="bold" />
<Button
android:id="@+id/right_button"
android:layout_width="@dimen/titlebar_button_width"
android:layout_height="@dimen/titlebar_button_height"
android:text=""
android:clickable="true"
android:layout_marginRight="10dp"
android:background="@drawable/button_shape_drawable"
android:textColor="@color/colorstatelist" />
</LinearLayout>
颜色状态列表.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="@color/state_pressed"/>
<item android:state_selected="true"
android:color="@color/state_selected"/>
<item android:state_focused="true"
android:color="@color/state_focused"/>
<item android:color="#808080"/>
</selector>