在我的应用程序中,我有7个按钮,当我点击它时,我正在尝试更改按钮边框颜色。默认情况下,按钮颜色为灰色,点击后我想更改为其他颜色。
gray_button.xml
nolock
green_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="3dp" android:color="#646464" />
<solid android:color="@color/white" />
<size android:width="37dp" android:height="40dp" />
</shape>
</item>
</selector>
CreateAlarm.java
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="3dp" android:color="@color/green" />
<solid android:color="@color/white" />
<size android:width="37dp" android:height="40dp" />
</shape>
</item>
</selector>
更新:
CreateAlarm.java
public class CreateAlarm extends Activity {
private TimePicker mTimePicker;
private EditText mTitle;
private TextView mCancel, mRepeat,mSave;
private String mselectedTime,mAlarmName,mSnoozeEnabled,mFlashLightEnabled,mVibrationEnabled,mVolumeIncreaseEnabled,mSunDay,mMonDay,mTueDay,mWedDay,mThuDay,mFriDay,mSatDay;
private com.suke.widget.SwitchButton mSnooze,mFlashLight,mVibration,mVolumeIncrease;
public String mSelectedDays;
@Override
protected void onCreate(Bundle savedInstanceState) {
mFlashLight.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if(mFlashLight.isChecked()== true){
mFlashLightEnabled = "Enabled";
}
}
});
mVibration.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if(mVibration.isChecked()== true){
mVibrationEnabled = "Enabled";
}
}
});
mVolumeIncrease.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
if(mSelectedDays.contains("Thu")){
mThuDay = "Thu";
}else{
mThuDay = "Null";
}
if(mSelectedDays.contains("Fri")){
mFriDay = "Fri";
}else{
mFriDay = "Null";
}
if(mSelectedDays.contains("Sat")){
mSatDay = "Sat";
}else{
mSatDay = "Null";
}
//SwitchButton isChecked Validation
if (mSnoozeEnabled == null){
mSnoozeEnabled = "Disabled";
}
Intent intent = new Intent(CreateAlarm.this, AlarmActivity.class);
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
public void onPause() {
super.onPause();
mRepeat.setText("");
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onEventMain(AlarmEvent alarmEvent) {
if (alarmEvent.getSelectedEventType() == AlarmEvent.eventTypes.updateSelectedDay) {
mRepeat.setText(alarmEvent.getSelectedDays());
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}
加载此活动后,我收到了之前活动的数据,基于此我想设置启用/禁用按钮,并且还要相应地设置TextView文本(mRepeat)文本。
答案 0 :(得分:1)
您的错误是AppCompatButton
。您应该只展开Button
。
您应该研究EventBus
:https://github.com/greenrobot/EventBus。提供通知活动或碎片任何东西。
请再试一次:
public class CustomBorderButton extends Button {
private static HashSet<Integer> selectedDays = new HashSet<>();
public CustomBorderButton(Context context) {
this(context, null);
}
public CustomBorderButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomBorderButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
boolean isSelected = selectedDays.contains(id);
Drawable bgDrawable;
if (isSelected) {
bgDrawable = getBackgroundDrawable(R.drawable.gray_button);
selectedDays.remove(id);
} else {
bgDrawable = getBackgroundDrawable(R.drawable.green_button);
selectedDays.add(id);
}
setBackground(bgDrawable);
setSelectedDays();
}
});
}
private void setSelectedDays() {
StringBuilder repeatDays = new StringBuilder();
TreeMap<Integer, String> mAlarmDays = new TreeMap<>();
if (selectedDays.size() == 0) {
repeatDays = new StringBuilder("Never");
} else {
for (Integer selectedDayId : selectedDays) {
switch (selectedDayId) {
case R.id.mbtn_Sun:
mAlarmDays.put(0, "Sun");
break;
case R.id.mbtn_Mon:
mAlarmDays.put(1, "Mon");
break;
case R.id.mbtn_Tue:
mAlarmDays.put(2, "Tue");
break;
case R.id.mbtn_Wed:
mAlarmDays.put(3, "Wed");
break;
case R.id.mbtn_Thu:
mAlarmDays.put(4, "Thu");
break;
case R.id.mbtn_Fri:
mAlarmDays.put(5, "Fri");
break;
case R.id.mbtn_Sat:
mAlarmDays.put(6, "Sat");
break;
}
}//for
for (String day : mAlarmDays.values()) {
repeatDays.append(day).append(" ");
}
}
EventBus.getDefault().post(new AlarmEvent().updateSelectedDay(AlarmEvent.eventTypes.updateSelectedDay, repeatDays.toString().trim()));
}
public Drawable getBackgroundDrawable(int resourceId) {
Drawable backgroundDrawable;
if (android.os.Build.VERSION.SDK_INT >= 21) {
backgroundDrawable = getResources().getDrawable(resourceId, getContext().getTheme());
} else {
backgroundDrawable = getResources().getDrawable(resourceId);
}
return backgroundDrawable;
}
}
CreateAlarm.java
public class CreateAlarm extends Activity {
private TimePicker mTimePicker;
private TextView mCancel, mRepeat;
private ImageView mRepeatIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_alarm_layout);
Utils.darkenStatusBar(this, R.color.StatusbarColor);
mTimePicker = (TimePicker) findViewById(R.id.mTimePicker);
mCancel = (TextView) findViewById(R.id.mCancel);
mRepeat = (TextView) findViewById(R.id.mRepeat);
int hours = mTimePicker.getCurrentHour();
mCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Override
public void onPause() {
super.onPause();
mRepeat.setText("");
}
@Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
public void onEventMain(AlarmEvent alarmEvent) {
if (alarmEvent.getSelectedEventType() == AlarmEvent.eventTypes.updateSelectedDay) {
mRepeat.setText(alarmEvent.getSelectedDays());
}
}
}
AlarmEvent:
public class AlarmEvent {
private eventTypes selectedEventType;
private String selectedDays = "";
public String getSelectedDays() {
return selectedDays;
}
public eventTypes getSelectedEventType() {
return selectedEventType;
}
public enum eventTypes {
updateSelectedDay
}
public AlarmEvent updateSelectedDay(eventTypes newEvent, String selectedDays) {
AlarmEvent alarmEvent = new AlarmEvent();
alarmEvent.selectedEventType = newEvent;
alarmEvent.selectedDays = selectedDays;
return alarmEvent;
}
}
CreateAlarm
clickSave(){
Intent intent = new Intent(CreateAlarm.this, AlarmActivity.class);
finish();
startActivity(intent);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
AlarmActivity
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
答案 1 :(得分:0)
您可以在res / drawable中创建一个选择器
<?xml version="1.0" encoding="utf-8"?>
<item android:state_activated="true" android:drawable="@drawable/gray_button"></item>
<item android:state_activated="false" android:drawable="@drawable/green_button"></item>
然后在您的自定义BorderButton中,而不是更改背景,只需设置按钮的激活状态
if (isSelected) {
setActivated(false);
selectedDays.remove(id);
} else {
setActivated(true);
selectedDays.add(id);
}