示例#1
0
  /**
   * Instantiates a new ab slider button.
   *
   * @param context the context
   * @param attrs the attrs
   */
  public AbSlidingButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    btnWidth = 40;
    btnHeight = 40;
    // 移动的距离
    moveWidth = 45;
    // 每次移动的距离
    movePDis = 5;
    // 覆盖按钮的宽
    WidthOffset = 5;

    WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wManager.getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

    /*
     2 px = 3 dip if dpi == 80(ldpi), 320x240 screen
     1 px = 1 dip if dpi == 160(mdpi), 480x320 screen
     3 px = 2 dip if dpi == 240(hdpi), 840x480 screen
    */

    if (width < 320) {
      btnWidth = btnWidth - 10;
      btnHeight = btnHeight - 10;
      moveWidth = moveWidth - 10;
    } else if (width > 320 && width < 450) {
      btnWidth = btnWidth + 10;
      btnHeight = btnHeight + 10;
      moveWidth = moveWidth + 10;
    } else if (width >= 450) {
      btnWidth = btnWidth + 20;
      btnHeight = btnHeight + 20;
      moveWidth = moveWidth + 20;
    }

    detector = new GestureDetector(this);

    mRelativeLayout = new RelativeLayout(context);

    ImageButton btnOn = new ImageButton(context);
    ImageButton btnOff = new ImageButton(context);
    btnLeft = new ImageButton(context);
    btnRight = new ImageButton(context);

    btnOn.setFocusable(false);
    btnOff.setFocusable(false);
    btnLeft.setFocusable(false);
    btnRight.setFocusable(false);

    Bitmap onImage = AbFileUtil.getBitmapFormSrc("image/button_on_bg.png");
    Bitmap offImage = AbFileUtil.getBitmapFormSrc("image/button_off_bg.png");
    Bitmap blockImage = AbFileUtil.getBitmapFormSrc("image/button_block.png");

    btnOn.setId(ID_BTN1);
    // btnOn.setImageBitmap(onImage);
    btnOn.setMinimumWidth(btnWidth);
    btnOn.setMinimumHeight(btnHeight);
    btnOn.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(onImage));

    btnOff.setId(ID_BTN2);
    btnOff.setMinimumWidth(btnWidth);
    btnOff.setMinimumHeight(btnHeight);
    // btnOff.setImageBitmap(offImage);
    btnOff.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(offImage));

    btnLeft.setId(ID_BTN3);
    btnLeft.setMinimumWidth(btnWidth + WidthOffset);
    btnLeft.setMinimumHeight(btnHeight);
    // btnLeft.setImageBitmap(blockImage);
    btnLeft.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(blockImage));

    btnRight.setId(ID_BTN4);
    btnRight.setMinimumWidth(btnWidth + WidthOffset);
    btnRight.setMinimumHeight(btnHeight);
    // btnRight.setImageBitmap(blockImage);
    btnRight.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(blockImage));

    mRelativeLayout.addView(btnOn);
    mRelativeLayout.setFocusable(false);

    RelativeLayout.LayoutParams lp2 =
        new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp2.addRule(RelativeLayout.RIGHT_OF, ID_BTN1);
    mRelativeLayout.addView(btnOff, lp2);

    RelativeLayout.LayoutParams lp3 =
        new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp3.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    mRelativeLayout.addView(btnLeft, lp3);

    RelativeLayout.LayoutParams lp4 =
        new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lp4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    mRelativeLayout.addView(btnRight, lp4);

    addView(mRelativeLayout);

    mSwitcherChangeListener =
        new AbOnChangeListener() {

          @Override
          public void onChange(int position) {}
        };
  }
  @Override
  public void createControl(TitaniumModuleManager tmm) {
    tv = new EditText(tmm.getAppContext());
    tv.isFocusable();
    tv.setId(100);

    tv.addTextChangedListener(this);
    tv.setOnEditorActionListener(this);
    tv.setText(value);
    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
    tv.setPadding(4, 2, 4, 2);
    tv.setSingleLine();
    TitaniumUIHelper.styleText(tv, fontSize, fontWeight);

    if (color != null) {
      tv.setTextColor(TitaniumColorHelper.parseColor(color));
    }
    if (backgroundColor != null) {
      tv.setBackgroundColor(TitaniumColorHelper.parseColor(backgroundColor));
    }

    cancelBtn = new ImageButton(tmm.getAppContext());
    cancelBtn.isFocusable();
    cancelBtn.setId(101);
    cancelBtn.setPadding(0, 0, 0, 0);
    Drawable d = new BitmapDrawable(this.getClass().getResourceAsStream("cancel.png"));
    cancelBtn.setImageDrawable(d);
    cancelBtn.setMinimumWidth(48);

    cancelBtn.setVisibility(showCancel ? View.VISIBLE : View.GONE);
    cancelBtn.setOnClickListener(
        new OnClickListener() {

          public void onClick(View view) {
            handler.sendEmptyMessage(MSG_CANCEL);
          }
        });

    RelativeLayout layout = new RelativeLayout(tmm.getAppContext());
    control = layout;

    layout.setGravity(Gravity.NO_GRAVITY);
    layout.setPadding(0, 0, 0, 0);
    if (barColor != null) {
      layout.setBackgroundColor(TitaniumColorHelper.parseColor(barColor));
    }

    RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    params.addRule(RelativeLayout.LEFT_OF, 101);
    params.setMargins(4, 4, 0, 4);
    layout.addView(tv, params);

    params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_VERTICAL);
    params.setMargins(0, 4, 4, 4);
    layout.addView(cancelBtn, params);
  }