下面列出了android.widget.Button#setBackgroundColor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Format the button
* @param button The button
* @param active Active
*/
public void setButtonActive(Button button, boolean active) {
int colorText;
int colorBackground;
// Button is active
if (active) {
// Gets the active color
colorText = ContextCompat.getColor(getContext(), R.color.button_navigation_drawer_text_active);
colorBackground = ContextCompat.getColor(getContext(), R.color.button_navigation_drawer_active);
} else {
// Gets the normal color
colorText = ContextCompat.getColor(getContext(), R.color.button_navigation_drawer_text);
colorBackground = ContextCompat.getColor(getContext(), R.color.button_navigation_drawer_normal);
}
// Sets the color
button.setBackgroundColor(colorBackground);
button.setTextColor(colorText);
button.getCompoundDrawables()[0].setColorFilter(colorText, PorterDuff.Mode.MULTIPLY);
}
private void showCorrectOptions() {
for (int i = 0; i < 3; i++) {
Option option = mQuestion.getOptionList().get(i);
Button button = null;
switch (i) {
case 0:
button = mOption1Button;
break;
case 1:
button = mOption2Button;
break;
case 2:
button = mOption3Button;
break;
}
if (button != null) {
if (option.isCorrect()) {
button.setBackgroundColor(Color.GREEN);
} else {
button.setBackgroundColor(Color.RED);
}
}
}
}
private void showCorrectOptions() {
mQuestionCardData.mShowCorrectOptions = true;
for (int i = 0; i < 3; i++) {
Option option = mQuestionCardData.options.get(i);
Button button = null;
switch (i) {
case 0:
button = mOption1Button;
break;
case 1:
button = mOption2Button;
break;
case 2:
button = mOption3Button;
break;
}
if (button != null) {
if (option.isCorrect) {
button.setBackgroundColor(Color.GREEN);
} else {
button.setBackgroundColor(Color.RED);
}
}
}
}
@Override
protected void onRestart() {
super.onRestart();
model = loadFile();
Button game_continue = (Button) findViewById(R.id.game_button_continue);
if (model == null || model.isGame_finished())
{
// no saved game available
game_continuable = false;
game_continue.setClickable(true);
game_continue.setBackgroundColor(ContextCompat.getColor(getBaseContext(),(R.color.middlegrey)));
}
else
{
game_continuable = true;
game_continue.setClickable(true);
game_continue.setBackgroundColor(ContextCompat.getColor(getBaseContext(),R.color.colorPrimary));
}
}
private void addButton(LinearLayout layout, String name, String action) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
param.setMargins(2, 2, 2, 2);
Button btn = new Button(G.context);
btn.setLayoutParams(param);
btn.setTextColor(Color.WHITE);
btn.setBackgroundColor(ContextCompat.getColor(G.context, R.color.backgroundColorCall2));
btn.setText(name);
btn.setAllCaps(false);
btn.setTypeface(G.typeface_IRANSansMobile);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (G.onBotClick != null) {
G.onBotClick.onBotCommandText(action);
}
setLayoutBot(true, false);
}
});
layout.addView(btn);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_error);
//TextView errorDetailsText = (TextView) findViewById(R.id.error_details);
//errorDetailsText.setText(CustomActivityOnCrash.getStackTraceFromIntent(getIntent()));
Button restartButton = (Button) findViewById(R.id.restart_button);
restartButton.setBackgroundColor(Color.parseColor(G.appBarColor));
final Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final CaocConfig config = CustomActivityOnCrash.getConfigFromIntent(getIntent());
restartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomActivityOnCrash.restartApplicationWithIntent(ActivityCustomError.this, i, config);
}
});
}
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA))
tittle = intent.getStringExtra(EXTRA);
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getSize(size);
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
overlayedButton = new Button(this);
overlayedButton.setText(tittle);
overlayedButton.setAlpha(0.7f);
overlayedButton.setBackgroundColor(0xFFFFFFFF);
overlayedButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 26);
WindowManager.LayoutParams params =
new WindowManager.LayoutParams
(WindowManager.LayoutParams.MATCH_PARENT,
size.y - 200,
LAYOUT_FLAG
, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER;
wm.addView(overlayedButton, params);
return START_STICKY;
}
private View buildCustomView() {
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rootLayout.setFocusable(true);
rootLayout.setFocusableInTouchMode(true);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
LinearLayout scrollableView = new LinearLayout(this);
scrollableView.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(scrollableView);
TextView traceView = new TextView(this);
traceView.setPadding(10, 10, 10, 10);
traceView.append(deviceInfo);
traceView.append(stackTrace);
scrollableView.addView(traceView);
rootLayout.addView(scrollView);
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setBackgroundColor(Color.TRANSPARENT);
button.setText("我知道了");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
rootLayout.addView(button);
return rootLayout;
}
private View buildCustomView() {
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
rootLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rootLayout.setFocusable(true);
rootLayout.setFocusableInTouchMode(true);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
LinearLayout scrollableView = new LinearLayout(this);
scrollableView.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(scrollableView);
TextView traceView = new TextView(this);
traceView.setPadding(10, 10, 10, 10);
traceView.append(deviceInfo);
traceView.append(stackTrace);
scrollableView.addView(traceView);
rootLayout.addView(scrollView);
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
button.setBackgroundColor(Color.TRANSPARENT);
button.setText("我知道了");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
rootLayout.addView(button);
return rootLayout;
}
public void withItems(View view) {
final String[] items = {"Apple", "Banana", "Orange", "Grapes"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("List of Items")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which] + " is clicked", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("OK", null);
builder.setNegativeButton("CANCEL", null);
builder.setNeutralButton("NEUTRAL", null);
builder.setPositiveButtonIcon(getResources().getDrawable(android.R.drawable.ic_menu_call, getTheme()));
builder.setIcon(getResources().getDrawable(R.drawable.jd, getTheme()));
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
button.setBackgroundColor(Color.BLACK);
button.setPadding(0, 0, 20, 0);
button.setTextColor(Color.WHITE);
}
@Override
protected void setColorsOnButtons() {
int color = Color.parseColor(BetterColor.getColor());
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int[] alphas = shuffledColors();
for (int i = 0; i < alphas.length; i++) {
Button button = buttonList.get(i);
button.setBackgroundColor(Color.argb(alphas[i], red, green, blue));
}
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
View v = getView();
TextView reportText = null;
if (v != null) {
reportText = v.findViewById(R.id.report_text);
}
TypefaceUtil.setTypefaceBold(getContext(), reportText);
Button helpButton = v.findViewById(R.id.help_button);
TypefaceUtil.setTypefaceLight(getContext(), helpButton);
helpButton.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
// storageChooserButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// }
// });
Button reportButton = v.findViewById(R.id.report_button);
TypefaceUtil.setTypefaceLight(getContext(), reportButton);
reportButton.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white));
//
// storageChooserButtonMemorybar.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
//
// }
// });
}
/**
* Create And Add Layout
*/
private void createAndAddLayout() {
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(1f);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams tvLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
tvLayoutParams.weight = 0.7f;
LinearLayout.LayoutParams showButtonLayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
showButtonLayoutParams.weight = 0.3f;
tvText = new TextView(context);
tvText.setLayoutParams(tvLayoutParams);
tvText.setTextColor(ContextCompat.getColor(context, R.color.top_snackbar_text_color));
tvText.setPadding(24, 0, 0, 0);
linearLayout.addView(tvText);
showButton = new Button(context);
showButton.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
showButton.setTextColor(ContextCompat.getColor(context, R.color.top_snackbar_show_button_text_color));
showButton.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
showButton.setLayoutParams(showButtonLayoutParams);
linearLayout.addView(showButton);
linearLayout.setVisibility(View.GONE);
linearLayout.setBackgroundResource(R.drawable.top_snackbar_bg);
}
@Override
public View makeView(int vertical, int horizontal) {
Button btn = new Button(getActivity());
btn.setText("Vertical " + vertical);
btn.setBackgroundColor(mBgColor);
return btn;
}
private void setButtonStyle(Button button, boolean is_active) {
if (is_active) {
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorBtnTextAccent));
button.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
} else {
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.colorBtnText));
button.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorBtn));
}
}
private void setButtonColor(Button btn) {
int alpha = genInt(0x8D, 0xEF);
btn.setBackgroundColor((alpha<<24) | random.nextInt(0x00FFFFFF));
}
/** Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activateSensors = true;
setContentView(R.layout.main);
tt = new ToolbarTools();
drawingPanel = (FidoEditor) findViewById(R.id.drawingPanel);
// Create the standard directory for the drawings and libs, if needed:
createDirIfNotExists("FidoCadJ/Drawings");
createDirIfNotExists("FidoCadJ/Libs");
StaticStorage.setCurrentEditor(drawingPanel);
tt.activateListeners(this, drawingPanel.eea);
Globals.messages = new AccessResources(this);
activateSensors();
readAllLibraries();
createLibraryDrawer();
// TODO: this is method which works well, but it is discouraged by
// modern Android APIs.
reloadInstanceData(getLastNonConfigurationInstance());
IO.context = this;
// Process the intents. It is useful when a file has to be opened.
Uri data = getIntent().getData();
boolean readPrevious=false;
if(data!=null) {
getIntent().setData(null);
readPrevious=importData(data);
}
// If a file has not been opened, read the content of the previous
// session and try to restore the drawing, as contained in the
// temporary file.
if(!readPrevious)
readTempFile();
// Update the color of the layer button.
Button layerButton= (Button)findViewById(R.id.layer);
Vector<LayerDesc> layers =
drawingPanel.getDrawingModel().getLayers();
layerButton.setBackgroundColor(
((ColorAndroid)layers.get(0).getColor())
.getColorAndroid());
// Zoom to fit only if there is something to show.
if(!drawingPanel.getDrawingModel().isEmpty()) {
drawingPanel.panToFit();
}
}
public CircularButton(Context context, AttributeSet attrs) {
super(context, attrs);
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularButton);
setLayoutTransition(new LayoutTransition());
setRadius(getPx(DEFAULT_RADIUS));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setElevation(getPx(DEFAULT_ELEVATION));
}
mLinearLayout = new LinearLayout(context);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
// set selectable background
final TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
typedValue, true);
mLinearLayout.setBackgroundResource(typedValue.resourceId);
// create button
mButton = new Button(context);
mButton.setBackgroundColor(Color.TRANSPARENT);
mButton.setClickable(false);
mButton.setPadding((int)getPx(15), mButton.getPaddingTop(), (int)getPx(15),
mButton.getPaddingBottom());
final String text = typedArray.getString(R.styleable.CircularButton_text);
mButton.setText(text);
mButton.setTextColor(typedArray.getColor(R.styleable.CircularButton_textColor, Color.BLACK));
// create progressbar
mProgressBar = new ProgressBar(context);
mProgressBar.setVisibility(View.GONE);
// animation transaction
final LayoutTransition layoutTransition = getLayoutTransition();
layoutTransition.setDuration(DEFAULT_DURATION);
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
this.setOnClickListener(view -> {
if (isClickable()) {
startLoading();
}
});
// set background color animations
mBackgroundColor = typedArray.getColorStateList(R.styleable.CardView_cardBackgroundColor)
.getDefaultColor();
mLinearLayout.setBackgroundColor(mBackgroundColor);
final ColorDrawable[] color1 = {new ColorDrawable(mBackgroundColor),
new ColorDrawable(Color.WHITE)};
mTransStartLoading = new TransitionDrawable(color1);
final ColorDrawable[] color2 = {new ColorDrawable(Color.WHITE), new
ColorDrawable(mBackgroundColor)};
mTransStopLoading = new TransitionDrawable(color2);
// set progressbar for API < lollipop
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mProgressBar.setBackgroundColor(Color.WHITE);
mProgressBar.getIndeterminateDrawable().setColorFilter(
mBackgroundColor, PorterDuff.Mode.SRC_IN);
}
typedArray.recycle();
mLinearLayout.addView(mButton);
mLinearLayout.addView(mProgressBar);
addView(mLinearLayout);
}
@Override
protected View createView(LayoutInflater inflater, ViewGroup container) {
// 不显示默认标题
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
mCommonFrameLayout = new CommonFrameLayout(getContext());
mToolbar = mCommonFrameLayout.getTitleView();
mMoreButton = mToolbar.addMoreImageButton();
LinearLayout layout = LayoutUtil.newCommonLayout(getContext());
FrameLayout headLayout = new FrameLayout(getContext());
headLayout.setLayoutParams(LayoutUtil.newFrameLayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
TextView tvTips = new TextView(getContext());
tvTips.setTextColor(0xffafafaf);
tvTips.setTextSize(10f);
tvTips.setText("提示:单击编辑左滑可删除");
mAddCommonButton = new Button(getContext());
mAddCommonButton.setText("添加");
mAddCommonButton.setTextSize(14f);
mAddCommonButton.setTextColor(0xFFF93F25);
mAddCommonButton.setBackgroundColor(0x00000000);
FrameLayout.LayoutParams tipsParams = LayoutUtil.newWrapFrameLayoutParams();
tipsParams.leftMargin = DisplayUtil.dip2px(getContext(), 15f);
tipsParams.gravity = Gravity.CENTER_VERTICAL;
FrameLayout.LayoutParams params = LayoutUtil.newFrameLayoutParams(
DisplayUtil.dip2px(getContext(), 70f), DisplayUtil.dip2px(getContext(), 40f));
params.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
headLayout.addView(tvTips, tipsParams);
headLayout.addView(mAddCommonButton, params);
mSwipeMenuListView = new SwipeMenuListView(getContext());
mSwipeMenuListView.setCacheColorHint(0x00000000);
mSwipeMenuListView.setDividerHeight(0);
mSwipeMenuListView.setMenuCreator(newMenuCreator());
mSwipeMenuListView.setCloseInterpolator(new BounceInterpolator());
mSwipeMenuListView.setLayoutParams(LayoutUtil.newMatchLinearLayoutParams());
layout.addView(headLayout);
layout.addView(mSwipeMenuListView);
mCommonFrameLayout.setContent(layout);
return mCommonFrameLayout;
}
/**
* Method that initializes this custom view's default values and sets listeners
*
* @param context The context used to create this view
* @param attrs The attributes used to initialize fields
*/
private void initAttrs(final Context context, final AttributeSet attrs) {
if (isInEditMode())
return;
final TypedArray typedArray =
context.obtainStyledAttributes(attrs, R.styleable.SlickForm);
try {
mContext = context;
slickButtonForegroundColor = typedArray.getColor(R.styleable.SlickForm_slick_buttonFgColor, context.getResources().getColor(R.color.colorWhite));
slickButtonBackgroundColor = typedArray.getColor(R.styleable.SlickForm_slick_buttonBgColor, context.getResources().getColor(R.color.colorPurple));
isTooltipEnabled = typedArray.getBoolean(R.styleable.SlickForm_slick_tooltipEnabled, true);
LinearLayout mRootView = (LinearLayout) inflate(context, R.layout.library_main_layout, this);
slickFieldContainer = (RelativeLayout) mRootView.findViewById(R.id.slick_form_field_container);
slickFormProgressBar = (ProgressBar) mRootView.findViewById(R.id.slick_form_progress);
slickSVGIcon = (PathView) mRootView.findViewById(R.id.svgIcon);
slickEndAnimationContainer = (RelativeLayout) mRootView.findViewById(R.id.slick_form_end_anim_container);
slickEndAnimationProgressBar = (ProgressBar) mRootView.findViewById(R.id.slick_form_end_progress_bar);
slickFormSubmitButton = (Button) mRootView.findViewById(R.id.slick_form_submit_button);
slickFormSubmitButton.setBackgroundColor(slickButtonBackgroundColor);
slickFormSubmitButton.setTextColor(slickButtonForegroundColor);
slickFormSubmitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(currentFieldPosition >= formFields.size()) {
return;
}
processFormState();
}
});
} finally {
typedArray.recycle();
}
}