Androidカスタムカウントダウンビュー

1ビューを継承

システムビューを継承する場合でも、ビューを直接継承する場合でも、コンストラクターを書き直す必要があります。コンストラクターは複数あり、そのうちの少なくとも1つを書き直す必要があります。たとえば、AppCompatTextViewを継承する新しいCountDownTextViewを作成します

public class CountDownTextView extends AppCompatTextView

2つのカスタム属性

androidで始まるAndroidシステムコントロールは、システムの属性です。カスタムビュープロパティの構成を容易にするために、プロパティ値をカスタマイズすることもできます。
Androidのカスタム属性は、次の手順に分けることができます。

  1. ビューをカスタマイズする
  2. スタイリング可能やアイテムなどのラベル要素が書き込まれるvalues / attrs.xmlを記述します
  3. ビューはレイアウトファイルでカスタム属性を使用します(名前空間に注意してください)
  4. Viewの構築メソッドでTypedArrayによって取得されます
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="test">
        <attr name="counttime" format="integer" />
    </declare-styleable>
</resources>

<com.layout.CountDownTextView
        app:counttime="180"
       ></com.layout.CountDownTextView>

 

public CountDownTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);
        long test_counttime = ta.getInteger(R.styleable.test_counttime, 120);
        TOTAL_TIME=test_counttime*1000;
        ta.recycle();
        Log.e(TAG, "test_counttime = " + TOTAL_TIME);
        
    }

3カウントダウンを定義します

 public void startCountTime() {
        countDownTimer = new CountDownTimer(TOTAL_TIME, ONCE_TIME) {
            @Override
            public void onTick(long millisUntilFinished) {
              
               String value = String.valueOf((int) (millisUntilFinished / 1000));
               setText("倒计时"+value +"秒后,返回首页");
            }

            @Override
            public void onFinish() {
             };
        
        countDownTimer.start();
    }


    public void stopCountTime() {
    if(countDownTimer!=null){
        countDownTimer.cancel();
        countDownTimer=null;
        setText("倒计时120秒后,返回首页");
    }

 

おすすめ

転載: blog.csdn.net/guodashen007/article/details/105101380