下面列出了android.widget.Button#setId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void init() {
setTopTitle("工具库");
setTopSubtitle("这个是我的工具类库入口");
setTopIcon(0);
getTopBar().setEndBtnListener("测试", v -> {
VMToast.make(mActivity, "测试自定义 VMTopBar 右侧按钮样式").show();
});
getTopBar().setEndBtnTextColor(VMColor.byRes(R.color.app_accent));
//getTopBar().setEndBtnBackground(R.drawable.rectangle_red_shape_bg);
String[] btnArray = {
"工具", "按钮样式", "描点控件", "详情控件", "自定义控件", "录制屏幕", "声音播放", "悬浮菜单", "Web 功能", "指示器", "识别验证码", "图片选择器", "单权限申请", "多权限申请", "线程测试",
"Scheme"
};
for (int i = 0; i < btnArray.length; i++) {
Button btn = new Button(new ContextThemeWrapper(mActivity, R.style.VMBtn_Flat));
btn.setText(btnArray[i]);
btn.setId(CLICK_START_ID + i);
btn.setOnClickListener(viewListener);
viewGroup.addView(btn);
}
}
/**
* Creates a standardized Button that can be used for DualControlLayouts showing buttons.
*
* @param isPrimary Whether or not the button is meant to act as a "Confirm" button.
* @param text Text to display on the button.
* @param listener Listener to alert when the button has been clicked.
* @return Button that can be used in the view.
*/
public static Button createButtonForLayout(
Context context, boolean isPrimary, String text, OnClickListener listener) {
int lightActiveColor =
ApiCompatibilityUtils.getColor(context.getResources(), R.color.light_active_color);
if (isPrimary) {
ButtonCompat primaryButton = new ButtonCompat(context, lightActiveColor, false);
primaryButton.setId(R.id.button_primary);
primaryButton.setOnClickListener(listener);
primaryButton.setText(text);
primaryButton.setTextColor(Color.WHITE);
return primaryButton;
} else {
Button secondaryButton = ButtonCompat.createBorderlessButton(context);
secondaryButton.setId(R.id.button_secondary);
secondaryButton.setOnClickListener(listener);
secondaryButton.setText(text);
secondaryButton.setTextColor(lightActiveColor);
return secondaryButton;
}
}
/**
* Creates a standardized Button that can be used for DualControlLayouts showing buttons.
*
* @param isPrimary Whether or not the button is meant to act as a "Confirm" button.
* @param text Text to display on the button.
* @param listener Listener to alert when the button has been clicked.
* @return Button that can be used in the view.
*/
public static Button createButtonForLayout(
Context context, boolean isPrimary, String text, OnClickListener listener) {
int lightActiveColor =
ApiCompatibilityUtils.getColor(context.getResources(), R.color.light_active_color);
if (isPrimary) {
ButtonCompat primaryButton = new ButtonCompat(context, lightActiveColor, false);
primaryButton.setId(R.id.button_primary);
primaryButton.setOnClickListener(listener);
primaryButton.setText(text);
primaryButton.setTextColor(Color.WHITE);
return primaryButton;
} else {
Button secondaryButton = ButtonCompat.createBorderlessButton(context);
secondaryButton.setId(R.id.button_secondary);
secondaryButton.setOnClickListener(listener);
secondaryButton.setText(text);
secondaryButton.setTextColor(lightActiveColor);
return secondaryButton;
}
}
@Test public void verifyContentViewBinding() {
FrameLayout frameLayout = new FrameLayout(context);
Button button1 = new Button(context);
button1.setId(android.R.id.button1);
frameLayout.addView(button1);
Button button2 = new Button(context);
button2.setId(android.R.id.button2);
frameLayout.addView(button2);
Button button3 = new Button(context);
button3.setId(android.R.id.button3);
frameLayout.addView(button3);
View content = new View(context);
content.setId(android.R.id.content);
frameLayout.addView(content);
H h = new H(frameLayout);
Unbinder unbinder = ButterKnife.bind(h, frameLayout);
verifyHBound(h);
unbinder.unbind();
verifyHUnbound(h);
}
@Override
protected void onInitData() {
mTitle.setText("Http test");
String[] bttxt = getResources().getStringArray(R.array.http_list);
if (bttxt != null) {
for (int i = 0; i < bttxt.length; i++) {
Button bt = new Button(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
lp.setMargins(margin, margin, margin, margin);
bt.setId(i);
bt.setText(bttxt[i]);
bt.setOnClickListener(this);
bt.setLayoutParams(lp);
mContainer.addView(bt);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_list_btn);
TAG = this.getClass().getSimpleName();
Log.setTag(TAG);
container = (LinearLayout) findViewById(R.id.container);
scroll = (ScrollView) container.getParent();
TextView tv = (TextView) container.findViewById(R.id.title);
tv.setText(getMainTitle());
mTvSubTitle = (TextView) container.findViewById(R.id.sub_title);
String[] bttxt = getButtonTexts();
if (bttxt != null) {
for (int i = 0; i < bttxt.length; i++) {
Button bt = new Button(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.common_marin);
lp.setMargins(margin, margin, margin, margin);
bt.setId(i);
bt.setText(bttxt[i]);
bt.setOnClickListener(this);
bt.setLayoutParams(lp);
container.addView(bt);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_list_btn);
TAG = this.getClass().getSimpleName();
OrmLog.setTag(TAG);
container = (LinearLayout) findViewById(R.id.container);
scroll = (ScrollView) container.getParent();
TextView tv = (TextView) container.findViewById(R.id.title);
tv.setText(getMainTitle());
mTvSubTitle = (TextView) container.findViewById(R.id.sub_title);
String[] bttxt = getButtonTexts();
if (bttxt != null) {
for (int i = 0; i < bttxt.length; i++) {
Button bt = new Button(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.common_marin);
lp.setMargins(margin, margin, margin, margin);
bt.setId(i);
bt.setText(bttxt[i]);
bt.setOnClickListener(this);
bt.setLayoutParams(lp);
container.addView(bt);
}
}
}
private void prepareButtons() {
mDoneButton = (Button) mLayout.findViewById(R.id.button_primary);
mDoneButton.setId(R.id.payments_edit_done_button);
mDoneButton.setOnClickListener(this);
Button cancelButton = (Button) mLayout.findViewById(R.id.button_secondary);
cancelButton.setId(R.id.payments_edit_cancel_button);
cancelButton.setOnClickListener(this);
DualControlLayout buttonBar = (DualControlLayout) mLayout.findViewById(R.id.button_bar);
buttonBar.setAlignment(DualControlLayout.ALIGN_END);
}
private void givenClickableView() {
view = new Button(activityTestRule.getActivity());
view.setId(viewId);
addViewToLayout(view, BaseActivity.rootLayout);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messageView.setText(VIEW_WAS_CLICKED_MESSAGE);
}
});
}
@Before
public void setup() {
Button button = new Button(activityTestRule.getActivity());
button.setId(buttonId);
button.setText(buttonText);
addViewToLayout(button, BaseActivity.rootLayout);
}
private void givenButtonToStartNextPage() {
Button button = new Button(activityTestRule.getActivity());
button.setText(nextPageButtonText);
button.setId(nextPageButtonId);
addViewToLayout(button, BaseActivity.rootLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activityTestRule.getActivity(), LandscapeFixedActivity.class);
activityTestRule.getActivity().startActivity(intent);
}
});
}
@Override
protected void onInitData() {
mTitle.setText("老哦 test");
String[] bttxt = getResources().getStringArray(R.array.test_list);
if (bttxt != null) {
for (int i = 0; i < bttxt.length; i++) {
Button bt = new Button(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
lp.setMargins(margin, margin, margin, margin);
bt.setId(i);
bt.setText(bttxt[i]);
bt.setOnClickListener(this);
//RxBinding示例Demo
// RxView.clicks(bt).throttleFirst(500, TimeUnit.MILLISECONDS)
// .subscribe(new Action1<Void>() {
// @Override
// public void call(Void aVoid) {
//
// }
// });
bt.setLayoutParams(lp);
mContainer.addView(bt);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
setContentView(layout);
mOpenCvCameraView = new CameraView(this,mCameraIndex); //mCameraIndex = 0 indicates rear camera
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
layout.addView(mOpenCvCameraView); //add CameraView to frame layout
gs = new GraphicSurface(this); // create graphic surface
gs.getHolder().setFormat( PixelFormat.TRANSPARENT); //set graphic surface to transparent
gs.setZOrderOnTop(true); //graphic surface as top layer
gs.setLayoutParams(new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
layout.addView(gs); //add graphic surface to frame layout
bt = new Button(this); // create button
bt.setText("Play");
bt.setId(12345);
bt.getBackground().setAlpha(64); //button to transparent
bt.setOnClickListener(this);
bt.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
layout.addView(bt); //add button to frame layout
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometerSensor = Sensor.TYPE_ACCELEROMETER; //accelerometer
magneticSensor = Sensor.TYPE_MAGNETIC_FIELD; //magentic sensor
if(Build.VERSION.SDK_INT >= 23)
askForPermission(Manifest.permission.CAMERA,CAMERA); //ask for camera permission
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Select value");
final LinearLayout view = new LinearLayout(getContext());
final ScrollView sview = new ScrollView(getContext());
view.setOrientation(LinearLayout.VERTICAL);
sview.setVerticalScrollBarEnabled(true);
for (int i = 0; i < values.size(); i++) {
final Button button = new Button(getContext());
String s = values.get(i);
button.setText(s);
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedValue = (Integer) button.getId();
Selector.this.dismiss();
}
});
view.addView(button);
}
sview.addView(view);
setContentView(sview);
}
private Button createAndAddEditButton(ViewGroup parent) {
Resources resources = parent.getResources();
Button view = DualControlLayout.createButtonForLayout(
parent.getContext(), true, resources.getString(R.string.choose), this);
view.setId(R.id.payments_section);
LayoutParams params =
new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ApiCompatibilityUtils.setMarginStart(params, mLargeSpacing);
parent.addView(view, params);
return view;
}
private void prepareButtons() {
mDoneButton = (Button) mLayout.findViewById(R.id.button_primary);
mDoneButton.setId(R.id.payments_edit_done_button);
mDoneButton.setOnClickListener(this);
Button cancelButton = (Button) mLayout.findViewById(R.id.button_secondary);
cancelButton.setId(R.id.payments_edit_cancel_button);
cancelButton.setOnClickListener(this);
}
public void initialize(Context context) {
this.setBackgroundColor(Color.WHITE);
final int width = 90;
final int height = 90;
final int margin = 13;
final int length = PointPath.mPathColors.length;
int left = 10;
for (int i = 0; i < length; ++i) {
int color = PointPath.mPathColors[i];
ImageView imgBtn = new ImageView(context);
if (CURRENT_TYPE == STROCK_TYPE) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(PointPath.mPenStrock[i]);
canvas.drawLine(0, height, width, 0, paint);
imgBtn.setImageBitmap(bitmap);
imgBtn.setBackgroundColor(Color.WHITE);
} else {
imgBtn.setBackgroundColor(color);
}
imgBtn.setOnClickListener(m_clickListener);
imgBtn.setTag(i);
LayoutParams params = new LayoutParams(width, height);
params.setMargins(left, 30, 0, 0);
params.addRule(Gravity.CENTER_VERTICAL);
left += (margin + width);
this.addView(imgBtn, params);
}
// Cancel button.
Button btnCancel = new Button(context);
btnCancel.setText("cancel");
btnCancel.setId(CANCEL_BUTTON_ID);
btnCancel.setTextSize(8);
btnCancel.setOnClickListener(m_clickListener);
LayoutParams btnCancelparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
btnCancelparams.addRule(Gravity.CENTER);
left += 10;
btnCancelparams.setMargins(left, 30, 0, 0);
this.addView(btnCancel, btnCancelparams);
}
public void initialize(Context context) {
this.setBackgroundColor(Color.WHITE);
final int width = 90;
final int height = 90;
final int margin = 13;
final int length = PointPath.mPathColors.length;
int left = 10;
for (int i = 0; i < length; ++i) {
int color = PointPath.mPathColors[i];
ImageView imgBtn = new ImageView(context);
if (CURRENT_TYPE == STROCK_TYPE) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(PointPath.mPenStrock[i]);
canvas.drawLine(0, height, width, 0, paint);
imgBtn.setImageBitmap(bitmap);
imgBtn.setBackgroundColor(Color.WHITE);
} else {
imgBtn.setBackgroundColor(color);
}
imgBtn.setOnClickListener(m_clickListener);
imgBtn.setTag(i);
LayoutParams params = new LayoutParams(width, height);
params.setMargins(left, 30, 0, 0);
params.addRule(Gravity.CENTER_VERTICAL);
left += (margin + width);
this.addView(imgBtn, params);
}
// Cancel button.
Button btnCancel = new Button(context);
btnCancel.setText("cancel");
btnCancel.setId(CANCEL_BUTTON_ID);
btnCancel.setTextSize(8);
btnCancel.setOnClickListener(m_clickListener);
LayoutParams btnCancelparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
btnCancelparams.addRule(Gravity.CENTER);
left += 10;
btnCancelparams.setMargins(left, 30, 0, 0);
this.addView(btnCancel, btnCancelparams);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
this.id = position;
LinearLayout ll = new LinearLayout(mcontext);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(mcontext);
tv.setText(Html.fromHtml("<h2>"+List[position].text+"</h2>"+List[position].source+"<br>"+"path:"+List[position].getPath()+"<br>"+"source:"+List[position].getSource()));
LinearLayout ll2 = new LinearLayout(mcontext);
Button b = new Button(mcontext);
b.setText("change path");
b.setId(1);
b.setOnClickListener(List[position]);
if(position==0)b.setEnabled(false);
Button b2 = new Button(mcontext);
b2.setText("change source");
b2.setId(2);
b2.setOnClickListener(List[position]);
ll2.addView(b);
ll2.addView(b2);
ll.addView(tv);
ll.addView(ll2);
Button b1 = new Button(mcontext);
b1.setText((List[position].installed)?"uninstall":"install");
b1.setId(0);
b1.setOnClickListener(List[position]);
if(Update.update[position]&&List[position].installed){
TextView tv2 = new TextView(mcontext);
tv2.setText(Update.updatetext[position]);
LinearLayout ll3 = new LinearLayout(mcontext);
Button b3 = new Button(mcontext);
b3.setText("update");
b3.setId(3);
b3.setOnClickListener(List[position]);
ll3.addView(b1);
ll3.addView(b3);
ll.addView(tv2);
ll.addView(ll3);
}
else{
ll.addView(b1);
}
ma.setContentView(ll);
ma.state = 2;
}
private void refresh() {
this.removeAllViews();
TableLayout table = new TableLayout(this.getContext());
int days = data.days().length;
int rows = (int)Math.ceil(days / TABLE_LENGTH);
View[] dayViews = new View[days];
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
TableRow[] tRows = new TableRow[rows];
for (int i = 0; i < tRows.length; ++i) {
tRows[i] = new TableRow(this.getContext());
}
Calendar c = Calendar.getInstance();
c.setTime(data.anchor());
c.add(Calendar.DATE, -(data.days().length - 1));
int currentRow = 0;
for (int i = 0; i < days; ++i) {
DotsDay day = data.days()[i];
TableRow.LayoutParams dayParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
dayParams.setMargins(1, 1, 1, 1);
View dayView = getDayView(c, day, i);
tRows[currentRow].addView(dayView);
dayViews[i] = dayView;
if (i % TABLE_LENGTH == TABLE_LENGTH - 1) {
currentRow = currentRow + 1;
}
c.add(Calendar.DATE, 1);
}
for (TableRow row : tRows) {
table.addView(row);
}
Button done = new Button(this.getContext());
done.setId(666);
done.setText("Finished");
done.setOnClickListener(v -> listener.doneWithDOTS());
RelativeLayout topPane = new RelativeLayout(this.getContext());
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ABOVE, done.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
this.addView(topPane, params);
table.setGravity(Gravity.CENTER_VERTICAL);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
topPane.addView(table, params);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
this.addView(done, params);
}