下面列出了android.widget.Button#setBackgroundResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initWidget() {
swipeBackView.setOnSwipeListener(this);
AppCompatImageView icon = findViewById(R.id.activity_login_icon);
ImageHelper.loadImage(this, icon, R.drawable.ic_launcher);
Button loginBtn = findViewById(R.id.activity_login_loginBtn);
Button joinBtn = findViewById(R.id.activity_login_joinBtn);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (ThemeManager.getInstance(this).isLightTheme()) {
loginBtn.setBackgroundResource(R.color.colorPrimaryDark_dark);
joinBtn.setBackgroundResource(R.color.colorPrimaryDark_light);
} else {
loginBtn.setBackgroundResource(R.color.colorPrimaryDark_light);
joinBtn.setBackgroundResource(R.color.colorPrimaryDark_dark);
}
} else {
loginBtn.setBackgroundResource(R.drawable.button_login);
joinBtn.setBackgroundResource(R.drawable.button_join);
}
progressContainer.setVisibility(View.GONE);
}
/**
* Indicate token input error
*
* @param button
*/
private void flashButton(final Button button)
{
if (button == null) return;
button.setBackgroundResource(R.drawable.button_round_error);
handler.postDelayed(() -> {
switch (button.getId())
{
case R.id.primary_button:
button.setBackgroundResource(R.drawable.selector_round_button);
break;
default:
case R.id.secondary_button:
button.setBackgroundResource(R.drawable.selector_round_button_secondary);
break;
}
}, 500);
}
/**
* set positive button
*
* @param text the name of button
*/
public void setPositiveButton(String text, final View.OnClickListener listener)
{
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setTextColor(Color.argb(255, 35, 159, 242));
button.setText(text);
button.setGravity(Gravity.CENTER);
button.setTextSize(14);
button.setPadding(dip2px(12), 0, dip2px(32), dip2px(BUTTON_BOTTOM));
button.setOnClickListener(listener);
mButtonLayout.addView(button);
}
/**
* set negative button
*
* @param text the name of button
*/
public void setNegativeButton(String text, final View.OnClickListener listener) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setText(text);
button.setTextColor(Color.argb(222, 0, 0, 0));
button.setTextSize(14);
button.setGravity(Gravity.CENTER);
button.setPadding(0, 0, 0, dip2px(8));
button.setOnClickListener(listener);
if (mButtonLayout.getChildCount() > 0) {
params.setMargins(20, 0, 10, dip2px(BUTTON_BOTTOM));
button.setLayoutParams(params);
mButtonLayout.addView(button, 1);
} else {
button.setLayoutParams(params);
mButtonLayout.addView(button);
}
}
private void setMenuButton() {
mYMenuButton = new Button(mContext);
//生成ID
mYMenuButton.setId(generateViewId());
//调用抽象方法来确定MenuButton的位置
setMenuPosition(mYMenuButton);
// mSetting.setMenuPosition(mYMenuButton);
//设置打开关闭事件
mYMenuButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!isShowMenu) {
showMenu();
} else {
closeMenu();
}
}
});
mYMenuButton.setBackgroundResource(mMenuButtonBackGroundId);
addView(mYMenuButton);
}
public void speech(View view) {
this.speak = !this.speak;
Button button = (Button) findViewById(R.id.speechButton);
if (this.speak) {
button.setBackgroundResource(R.drawable.voice);
} else {
button.setBackgroundResource(R.drawable.voiceoff);
}
}
private void customizeViewEmpty(ViewGroup viewEmpty, ImageView viewEmptyImage,
TextView viewEmptyMessage, Button viewEmptyTryAgainButton) {
if (viewEmpty != null) {
if (mPlaceHolderManager.mViewEmptyBackgroundColor != 0) {
viewEmpty.setBackgroundColor(getColor(mPlaceHolderManager.mViewEmptyBackgroundColor));
} else if (mPlaceHolderManager.mViewEmptyBackgroundResource > 0) {
viewEmpty.setBackgroundResource(mPlaceHolderManager.mViewEmptyBackgroundResource);
}
if (mPlaceHolderManager.mViewEmptyText > 0) {
viewEmptyMessage.setText(mPlaceHolderManager.mViewEmptyText);
}
if (mPlaceHolderManager.mViewEmptyTextSize > 0) {
setTextSize(viewEmptyMessage, mPlaceHolderManager.mViewEmptyTextSize);
}
if (mPlaceHolderManager.mViewEmptyTextColor != 0) {
viewEmptyMessage.setTextColor(getColor(mPlaceHolderManager.mViewEmptyTextColor));
}
if (mPlaceHolderManager.mViewEmptyTryAgainButtonText > 0) {
viewEmptyTryAgainButton.setText(mPlaceHolderManager.mViewEmptyTryAgainButtonText);
}
if (mPlaceHolderManager.mViewEmptyTryAgainButtonTextColor > 0) {
viewEmptyTryAgainButton.setTextColor(mPlaceHolderManager.mViewEmptyTryAgainButtonTextColor);
}
if (mPlaceHolderManager.mViewEmptyTryAgainButtonBackgroundResource > 0) {
int backgroundRes = mPlaceHolderManager.mViewEmptyTryAgainButtonBackgroundResource;
viewEmptyTryAgainButton.setBackgroundResource(backgroundRes);
}
if (mPlaceHolderManager.mViewEmptyImage > 0) {
viewEmptyImage.setImageDrawable(getDrawable(mPlaceHolderManager.mViewEmptyImage));
}
}
}
public void chime(View view) {
this.chime = !this.chime;
Button button = (Button) findViewById(R.id.chimeButton);
if (this.chime) {
button.setBackgroundResource(R.drawable.sound);
} else {
button.setBackgroundResource(R.drawable.mute);
}
}
@Override
protected void onResume () {
super.onResume();
isRunning = true;
//TextView status = (TextView) findViewById(R.id.status);
Button enableButton = (Button) findViewById(R.id.notification_open);
if (
( Build.VERSION.SDK_INT >= 18 && isNotificationListenerEnabled(this) )
|| isAccessibilityEnabled(getApplicationContext())
) {
//status.setVisibility(View.VISIBLE);
enableButton.setBackgroundResource(R.drawable.button_enable_on);
checkEnabled();
if (( Build.VERSION.SDK_INT >= 18 && isNotificationListenerEnabled(this) )
&& isAccessibilityEnabled(getApplicationContext()) ) {
final View bothEnabled = findViewById(R.id.bothEnabled);
bothEnabled.setVisibility(View.VISIBLE);
bothEnabled.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoAccessibility(v.getContext());
}
});
} else {
findViewById(R.id.bothEnabled).setVisibility(View.GONE);
}
} else {
enableButton.setBackgroundResource(R.drawable.button_enable);
//status.setVisibility(View.GONE);
}
}
public void show() {
super.show();
int i = 0;
Button btn = null;
Iterator<Button> iterator = mButtons.iterator();
while (iterator.hasNext()) {
Button button = iterator.next();
if(button.getVisibility() != View.VISIBLE) {
continue;
}
++i;
btn = button;
}
if (i == 1) {
btn.setBackgroundResource(R.drawable.btn_dialog_single);
}
if (i == 2) {
btn.setSelected(true);
((ViewGroup.MarginLayoutParams)(this.mButtons.get(0)).getLayoutParams()).rightMargin = 1;
}
if (i == 3) {
btn.setSelected(true);
((ViewGroup.MarginLayoutParams)(this.mButtons.get(2)).getLayoutParams()).leftMargin = 1;
((ViewGroup.MarginLayoutParams)(this.mButtons.get(0)).getLayoutParams()).rightMargin = 1;
}
}
public void setBackgroundResource(int resId, int... ids) {
for (int id : ids) {
Button button = getButton(id);
if (button != null) {
button.setBackgroundResource(resId);
}
}
}
private void setButtonStatus(Button button, boolean canClick) {
Logger.d(TAG, " setButtonStatus canClick : " + canClick);
if (canClick) {
button.setClickable(true);
button.setBackgroundResource(R.drawable.btn_blue_selecter);
button.setTextAppearance(this.mContext, R.style.letv_text_13_blue_white);
return;
}
button.setClickable(false);
button.setBackgroundResource(R.drawable.btn_grey);
button.setTextColor(Util.getContext().getResources().getColor(R.color.letv_color_ffa1a1a1));
}
/**
* set positive button
*
* @param text the name of button
*/
public void setPositiveButton(String text, final View.OnClickListener listener) {
Button button = new Button(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.material_card);
button.setTextColor(Color.argb(255, 35, 159, 242));
button.setText(text);
button.setGravity(Gravity.CENTER);
button.setTextSize(14);
button.setPadding(dip2px(12), 0, dip2px(32), dip2px(BUTTON_BOTTOM));
button.setOnClickListener(listener);
mButtonLayout.addView(button);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_uvc_device, null);
}
final DeviceContainer device = getItem(position);
String name = device.getName();
TextView nameView = convertView.findViewById(R.id.device_name);
nameView.setText(name);
Button btn = convertView.findViewById(R.id.btn_connect_device);
if (device.isRegisterFlag()) {
btn.setBackgroundResource(R.drawable.button_red);
btn.setText(R.string.uvc_settings_disconnect);
} else {
btn.setBackgroundResource(R.drawable.button_blue);
btn.setText(R.string.uvc_settings_connect);
}
btn.setOnClickListener((v) -> {
if (device.isRegisterFlag()) {
disconnectDevice(device);
} else {
connectDevice(device);
}
});
return convertView;
}
public void chime(View view) {
this.chime = !this.chime;
Button button = (Button) findViewById(R.id.chimeButton);
if (this.chime) {
button.setBackgroundResource(R.drawable.sound);
} else {
button.setBackgroundResource(R.drawable.mute);
}
}
private void initPageView() {
flPage = new FrameLayout(getContext());
flPage.setOnClickListener(this);
// 宫格列表的容器,为了“下对齐”,在外部包含了一个FrameLayout
LinearLayout llPage = new LinearLayout(getContext()) {
public boolean onTouchEvent(MotionEvent event) {
return true;
}
};
llPage.setOrientation(LinearLayout.VERTICAL);
int resId = getBitmapRes(getContext(), "share_vp_back");
if (resId > 0) {
llPage.setBackgroundResource(resId);
}
FrameLayout.LayoutParams lpLl = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lpLl.gravity = Gravity.BOTTOM;
llPage.setLayoutParams(lpLl);
flPage.addView(llPage);
// 宫格列表
grid = new PlatformGridView(getContext());
grid.setEditPageBackground(bgView);
LinearLayout.LayoutParams lpWg = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
grid.setLayoutParams(lpWg);
llPage.addView(grid);
// 取消按钮
btnCancel = new Button(getContext());
btnCancel.setTextColor(0xffffffff);
btnCancel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
resId = getStringRes(getContext(), "cancel");
if (resId > 0) {
btnCancel.setText(resId);
}
btnCancel.setPadding(0, 0, 0, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 5));
resId = getBitmapRes(getContext(), "btn_cancel_back");
if (resId > 0) {
btnCancel.setBackgroundResource(resId);
}
LinearLayout.LayoutParams lpBtn = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, cn.sharesdk.framework.utils.R.dipToPx(getContext(), 45));
int dp_10 = cn.sharesdk.framework.utils.R.dipToPx(getContext(), 10);
lpBtn.setMargins(dp_10, dp_10, dp_10, dp_10);
btnCancel.setLayoutParams(lpBtn);
llPage.addView(btnCancel);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_heart_rate_device, null);
}
final DeviceContainer device = getItem(position);
String name = device.getName();
if (device.isRegisterFlag()) {
if (getManager() != null && getManager().containConnectedHeartRateDevice(device.getAddress())) {
name += " " + getResources().getString(R.string.heart_rate_setting_online);
} else {
name += " " + getResources().getString(R.string.heart_rate_setting_offline);
}
}
TextView nameView = convertView.findViewById(R.id.device_name);
nameView.setText(name);
TextView addressView = convertView.findViewById(R.id.device_address);
addressView.setText(device.getAddress());
Button btn = convertView.findViewById(R.id.btn_connect_device);
if (device.isRegisterFlag()) {
btn.setBackgroundResource(R.drawable.button_red);
btn.setText(R.string.heart_rate_setting_disconnect);
} else {
btn.setBackgroundResource(R.drawable.button_blue);
btn.setText(R.string.heart_rate_setting_connect);
}
btn.setOnClickListener((v) -> {
if (device.isRegisterFlag()) {
disconnectDevice(device);
} else {
connectDevice(device);
}
});
return convertView;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View cv = convertView;
if (cv == null) {
cv = mInflater.inflate(R.layout.item_irkit_profile_list, parent, false);
} else {
cv = convertView;
}
final VirtualProfileContainer device = getItem(position);
String name = device.getLabel();
TextView nameView = (TextView) cv.findViewById(R.id.profile_name);
nameView.setText(name);
Button registerBtn = (Button) cv.findViewById(R.id.btn_register_ir);
registerBtn.setTag(position);
if (device.isRegister()) {
registerBtn.setBackgroundResource(R.drawable.button_orange);
registerBtn.setText(getString(R.string.virtual_device_update));
} else {
registerBtn.setBackgroundResource(R.drawable.button_blue);
registerBtn.setText(getString(R.string.virtual_device_register));
}
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = ((Integer) view.getTag());
VirtualProfileData profile = mProfiles.get(pos);
IRKitVirtualDeviceListActivity activity = (IRKitVirtualDeviceListActivity) getActivity();
activity.startRegisterPageApp(profile);
IRKitApplication application = activity.getIRKitApplication();
int yOffset = mListView.getChildAt(0).getTop();
application.setListViewPosition(
IRKitVirtualDeviceListActivity.MANAGE_VIRTUAL_PROFILE_PAGE,
pos,
yOffset);
}
});
return cv;
}
private void init() {
// TODO Auto-generated method stub
iv_avatar = (SmartImageView) findViewById(R.id.iv_avatar);
tv_username = (TextView) findViewById(R.id.tv_username);
lv_topics = (LinearLayout) findViewById(R.id.lv_topics);
lv_topics.setOnClickListener(this);
tv_topic = (TextView) findViewById(R.id.tv_topic);
lv_ifocus_person = (LinearLayout) findViewById(R.id.lv_ifocus_person);
lv_ifocus_person.setOnClickListener(this);
tv_ifocus_person = (TextView) findViewById(R.id.tv_ifocus_person);
lv_focusi_person = (LinearLayout) findViewById(R.id.lv_focusi_person);
lv_focusi_person.setOnClickListener(this);
tv_focusi_person = (TextView) findViewById(R.id.tv_focusi_person);
tv_thanks = (TextView) findViewById(R.id.tv_thanks);
tv_votes = (TextView) findViewById(R.id.tv_votes);
tv_collect = (TextView) findViewById(R.id.tv_collect);
lv_replys = (LinearLayout) findViewById(R.id.lv_replys);
lv_replys.setOnClickListener(this);
tv_replys = (TextView) findViewById(R.id.tv_replys);
lv_asks = (LinearLayout) findViewById(R.id.lv_asks);
lv_asks.setOnClickListener(this);
tv_asks = (TextView) findViewById(R.id.tv_asks);
lv_articles = (LinearLayout) findViewById(R.id.lv_articles);
lv_articles.setOnClickListener(this);
lv_news = (LinearLayout) findViewById(R.id.lv_news);
lv_news.setOnClickListener(this);
lv_search_friens = (LinearLayout) findViewById(R.id.lv_search_friens);
lv_search_friens.setOnClickListener(this);
bt_focus = (Button) findViewById(R.id.bt_focus);
bt_focus.setOnClickListener(this);
tv_focusi_person_comment = (TextView) findViewById(R.id.tv_focusi_person_comment);
tv_ifocus_person_comment = (TextView) findViewById(R.id.tv_ifocus_person_comment);
tv_topic_comment = (TextView) findViewById(R.id.tv_topic_comment);
tvSignature = (TextView) findViewById(R.id.tvSignature);
pb_change_follow = (ProgressBar) findViewById(R.id.pb_change_follow);
ll_logout = (LinearLayout) findViewById(R.id.ll_logout);
ll_logout.setOnClickListener(this);
// �жϱ������ѵ�¼�û�������ǿ��Ա༭�����ع�ע��ť���������ر༭��ť��ʾ��ע��ť��
if (status == GlobalVariables.AVAILABLE_EDIT) {
bt_focus.setVisibility(View.INVISIBLE);
ll_logout.setVisibility(View.VISIBLE);
} else {
ll_logout.setVisibility(View.GONE);
tv_focusi_person_comment.setText("��ע������");
tv_ifocus_person_comment.setText("����ע����");
tv_topic_comment.setText("����ע�Ļ���");
}
if (haveFrocus == YES) {
bt_focus.setBackgroundResource(R.drawable.btn_silver_normal);
bt_focus.setTextColor(android.graphics.Color.BLACK);
bt_focus.setText("ȡ����ע");
}
}
/**
* 改变bt颜色gray设置不可点击
*
* @param bt
*/
private void setButtonGray(Button bt) {
bt.setClickable(false);
bt.setBackgroundResource(R.color.gray);
}