本文整理汇总了Java中android.support.v7.widget.SwitchCompat.setId方法的典型用法代码示例。如果您正苦于以下问题:Java SwitchCompat.setId方法的具体用法?Java SwitchCompat.setId怎么用?Java SwitchCompat.setId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.support.v7.widget.SwitchCompat
的用法示例。
在下文中一共展示了SwitchCompat.setId方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addSwitch
import android.support.v7.widget.SwitchCompat; //导入方法依赖的package包/类
/**
* Creates a standard toggle switch and adds it to the layout.
*
* -------------------------------------------------
* | ICON | MESSAGE | TOGGLE |
* -------------------------------------------------
* If an icon is not provided, the ImageView that would normally show it is hidden.
*
* @param iconResourceId ID of the drawable to use for the icon, or 0 to hide the ImageView.
* @param iconColorId ID of the tint color for the icon, or 0 for default.
* @param toggleMessage Message to display for the toggle.
* @param toggleId ID to use for the toggle.
* @param isChecked Whether the toggle should start off checked.
*/
public View addSwitch(int iconResourceId, int iconColorId, CharSequence toggleMessage,
int toggleId, boolean isChecked) {
LinearLayout switchLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(
R.layout.infobar_control_toggle, this, false);
addView(switchLayout, new ControlLayoutParams());
ImageView iconView = (ImageView) switchLayout.findViewById(R.id.control_icon);
if (iconResourceId == 0) {
switchLayout.removeView(iconView);
} else {
iconView.setImageResource(iconResourceId);
if (iconColorId != 0) {
iconView.setColorFilter(
ApiCompatibilityUtils.getColor(getResources(), iconColorId));
}
}
TextView messageView = (TextView) switchLayout.findViewById(R.id.control_message);
messageView.setText(toggleMessage);
SwitchCompat switchView =
(SwitchCompat) switchLayout.findViewById(R.id.control_toggle_switch);
switchView.setId(toggleId);
switchView.setChecked(isChecked);
return switchLayout;
}
示例2: init
import android.support.v7.widget.SwitchCompat; //导入方法依赖的package包/类
@Override
public void init() {
super.init();
mSwitch = new SwitchCompat(getContext()) {
boolean isTouched = false;
@Override
public void setChecked(boolean checked) {
if ( isTouched || mForced ) {
super.setChecked(checked);
if ( isTouched && mListener != null ) {
isTouched = false;
mListener.onCheckedChanged(this, checked);
}
mForced = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
isTouched = true;
return super.onTouchEvent(ev);
}
};
mSwitch.setId(R.id.settings_switch);
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mSwitch.setLayoutParams(lp);
lp = (LayoutParams) mTextView.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.addRule(RelativeLayout.LEFT_OF, mSwitch.getId());
mTextView.setLayoutParams(lp);
int p = getResources().getDimensionPixelOffset(R.dimen.eight_dp) / 4;
mSwitch.setPadding(p, p, p, p);
addView(mSwitch);
}