简体   繁体   English

Android小计数器旁边有一个按钮

[英]Android small counter next to a button

I would like to add a small counter next to a button to show the remaining quantity for some items, for example, the remaining number of tips remaining unused. 我想在按钮旁边添加一个小计数器,以显示某些项目的剩余数量,例如,剩余未使用的提示数量。 Targeted layout would be as shown: 目标布局如下所示:

在此输入图像描述

Question: 题:

I have researched the web and found that some say to use different pictures for each quantity. 我研究过网络,发现有人说要为每个数量使用不同的图片。 Yet how could it be solved if the quantity can be up to 100? 然而,如果数量可以达到100,怎么能解决? Really necessary to draw such out? 真的有必要画这样的吗?

I am thinking of to stick 2 buttons together in a RelativeLayout such that when the user presses the bottom button, the top button will count up and down and setText itself, but are there some better solutions or imports? 我想在RelativeLayout中将2个按钮粘在一起,这样当用户按下底部按钮时,顶部按钮会向上和向下计数和setText本身,但是有更好的解决方案还是导入?

Edit: 编辑:

Thanks Rupesh for your codes and advice! 感谢Rupesh提供您的代码和建议! I have implemented as follows. 我已经实现如下。 Yet do you know how to move the red circle textview further to the right? 但是你知道如何将红色圆圈textview进一步向右移动吗? and the 20 cannot be properly shown in the red circle too... 并且20也无法在红圈中正确显示......

在此输入图像描述

Code: 码:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp" >

    <Button
        android:id="@+id/button_tip"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/orange_btn"
        android:onClick="button_tip_click"
        android:text="Hello" />

    <TextView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_gravity="top|right"
        android:background="@drawable/red_circle_btn"
        android:gravity="center"
        android:text="20"
        android:textColor="@color/white"
        android:textSize="8sp"
        android:textStyle="bold" />
</FrameLayout>

set Button's background Drawable to a custom Drawable like this one: 设置Button的背景可绘制为自定义Drawable,如下所示:

在此输入图像描述

public class DecoratedTextViewDrawable extends LayerDrawable {
    private int mCnt = 0;
    private Paint mPaint;
    private TextView mParent;
    private ColorStateList mColors;
    private Rect mBounds;

    public DecoratedTextViewDrawable(TextView tv, Drawable[] layers, int cnt) {
        super(layers);
        mParent = tv;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextAlign(Align.CENTER);
        mPaint.setTextSize(tv.getTextSize());
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);
        int[][] states = {
                {android.R.attr.state_pressed}, {android.R.attr.state_focused}, {}
        };
        int[] colors = {
                0xff0000aa, 0xff880000, 0xff00aa00
        };
        mColors = new ColorStateList(states, colors);
        mBounds = new Rect();
        setCnt(cnt);
    }

    public void setCnt(int cnt) {
        mCnt = cnt;
        String s = Integer.toString(cnt);
        mPaint.getTextBounds(s, 0, s.length(), mBounds);
        invalidateSelf();
    }

    @Override
    protected boolean onStateChange(int[] state) {
        invalidateSelf();
        return super.onStateChange(state);
    }

    @Override
    public boolean isStateful() {
        return true;
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        float x = mPaint.getTextSize() * 1.5f;
        float r = mPaint.getTextSize() * 0.9f;
        int base = mParent.getBaseline();
        int[] stateSet = getState();
//        Log.d(TAG, "draw " + StateSet.dump(stateSet));
        int color = mColors.getColorForState(stateSet, 0xff000000);
        mPaint.setColor(color);
        canvas.drawCircle(x, base + mBounds.top + mBounds.height() / 2, r, mPaint);
        mPaint.setColor(0xffeeeeee);
        canvas.drawText(Integer.toString(mCnt), x, base, mPaint);
    }
}

you can use it like this: 你可以像这样使用它:

// Activity.onCreate method
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

int NUM = 5;
final int[] cnt = new int[NUM];
Random r = new Random();
for (int i = 0; i < NUM; i++) {
    cnt[i] = r.nextInt(20);
    Button b = new Button(this);
    b.setText("Click me");
    b.setTextSize(18);
    b.setTag(i);

    Drawable[] layers = {b.getBackground()};
    Drawable d = new DecoratedTextViewDrawable(b, layers, cnt[i]);
    b.setBackgroundDrawable(d);
    OnClickListener l = new OnClickListener() {
        @Override
        public void onClick(View v) {
            DecoratedTextViewDrawable  d = (DecoratedTextViewDrawable) v.getBackground();
            int idx = (Integer) v.getTag();
            d.setCnt(++cnt[idx]);
        }
    };
    b.setOnClickListener(l);
    ll.addView(b);
}
setContentView(ll);

use this - and keep updating the textviews text with the count 使用此功能 - 并继续使用计数更新textviews文本

 <FrameLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">

 <ImageView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/your_phone_call_image"
     android:gravity="center"
     android:scaletype="matrix"/>

  <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="1"
     android:padding="5dp"
     android:gravity="top|right"  <!--this is important-->
     android:background="@drawable/your_counter_red_background"/>
 </FrameLayout>

I suggest you can use a FrameLayout. 我建议你可以使用FrameLayout。 1.- you can use 2 images one for your button, one for the little circle and use a textview for the numbers... 2.- you can use one image for your button, and create a gradient for the little circle and a text view.. 1.-您可以使用2个图像作为按钮,一个用于小圆圈,并使用文本视图作为数字... 2.-您可以使用一个图像作为按钮,并为小圆圈创建渐变文字视图..

For the frameLayout see : http://developer.android.com/reference/android/widget/FrameLayout.html For the gradient see: How to make gradient background in android . 对于frameLayout,请参阅: http//developer.android.com/reference/android/widget/FrameLayout.html对于渐变,请参阅: 如何在android中制作渐变背景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM