亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

android自定義倒計時控件示例

2020-04-11 11:55:35
字體:
來源:轉載
供稿:網友


自定義TextView控件TimeTextView代碼:

復制代碼 代碼如下:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;

import com.new0315.R;
/**
 * 自定義倒計時文本控件
 * @author Administrator
 *
 */
public class TimeTextView extends TextView implements Runnable{

    Paint mPaint; //畫筆,包含了畫幾何圖形、文本等的樣式和顏色信息

    private long[] times;

    private long mday, mhour, mmin, msecond;//天,小時,分鐘,秒

    private boolean run=false; //是否啟動了

    public TimeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint=new Paint();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

        array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響
    }

    public TimeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint=new Paint();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);

        array.recycle(); //一定要調用,否則這次的設定會對下次的使用造成影響
    }

    public TimeTextView(Context context) {
        super(context);
    }

    public long[] getTimes() {
        return times;
    }

    public void setTimes(long[] times) {
        this.times = times;
        mday = times[0];
        mhour = times[1];
        mmin = times[2];
        msecond = times[3];

    }

    /**
     * 倒計時計算
     */
    private void ComputeTime() {
        msecond--;
        if (msecond < 0) {
            mmin--;
            msecond = 59;
            if (mmin < 0) {
                mmin = 59;
                mhour--;
                if (mhour < 0) {
                    // 倒計時結束
                    mhour = 59;
                    mday--;

                }
            }

        }

    }

    public boolean isRun() {
        return run;
    }

    public void setRun(boolean run) {
        this.run = run;
    }

    @Override
    public void run() {
        //標示已經啟動
        run=true;

        ComputeTime();

        String strTime="還剩</pre>
<span style="color: red;">"+mday+"</span>
<pre>"+"天</pre>
<span style="color: red;">"+mhour+"</span>
<pre>小時</pre>
<span style="color: red;">"+
 mmin+"</span>
<pre>分鐘</pre>
<span style="color: red;">"+msecond+"</span>
<pre>秒";
        this.setText(Html.fromHtml(strTime));

        postDelayed(this, 1000);

    }

}

屬性atts.xml

復制代碼 代碼如下:

<declare-styleable name="TimeTextView">
</declare-styleable>

Adapter調用代碼:

復制代碼 代碼如下:

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.new0315.R;
import com.new0315.entity.SpecialGoods;
import com.new0315.utils.CorrectSpecialDataFormHttp;
import com.new0315.utils.DateTools;
import com.new0315.widgets.TimeTextView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SpecialGoodsAdapter extends BaseAdapter {

    private Context context;
    private List list;
    private long sumTime;

    public SpecialGoodsAdapter(Context context) {

        this.context = context;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        //開始計時,性能測試用nanoTime會更精確,因為它是納秒級的
        long startTime = System.nanoTime();
        Log.d("position","getView " + arg0 + " " + convertView);
        ViewHolder viewHolder;
        if(convertView == null)
        {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.item_temai_list, null);
            viewHolder = new ViewHolder();
            viewHolder.goodName = (TextView) convertView
                    .findViewById(R.id.temai_Name);
            viewHolder.price = (TextView) convertView
                    .findViewById(R.id.temai_yuanjia_text);

            viewHolder.specialPrice = (TextView) convertView
                    .findViewById(R.id.temai_xiajia_text);
            //特賣倒計時控件
            viewHolder.mTimeText = (TimeTextView) convertView
                    .findViewById(R.id.temai_timeTextView);

            viewHolder.showDate = (TextView) convertView
                    .findViewById(R.id.index_temai_day);
            viewHolder.showDate_l = (LinearLayout) convertView
                    .findViewById(R.id.temai_weikaishi);
            viewHolder.showTime = (LinearLayout) convertView
                    .findViewById(R.id.temai_yikaishi);
            viewHolder.koukou = (TextView) convertView
                    .findViewById(R.id.temai_zhekou_text);
            viewHolder.image = (ImageView) convertView
                    .findViewById(R.id.index_temai_image);
            Log.d("GoogleIO","new position:"+viewHolder.goodName.getText());

            convertView.setTag(viewHolder);

        }else {
            viewHolder = (ViewHolder) convertView.getTag();
            resetViewHolder(viewHolder);
        }
        //setData
        String off = getOff(list.get(arg0).getGoods_Price(), list.get(arg0)
                .getGoods_SpecialPrice());
        viewHolder.goodName.setText(list.get(arg0).getGoods_Name());
        viewHolder.price.setText(list.get(arg0).getGoods_Price());
        viewHolder.price.getPaint().setFlags(
                Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        viewHolder.specialPrice.setText(list.get(arg0).getGoods_SpecialPrice());
        viewHolder.koukou.setText(off + "折");

        if (DateTools.isStart(list.get(arg0).getSpecialFrom())) {
            //特賣倒計時開始
            viewHolder.mTimeText.setTimes(DateTools.getDate(CorrectSpecialDataFormHttp
                    .correctData((list.get(arg0).getSpecialEnd()))));
            //已經在倒計時的時候不再開啟計時
            if(!viewHolder.mTimeText.isRun())
            {
                viewHolder.mTimeText.run();
            }
            viewHolder.showDate_l.setVisibility(View.GONE);
            viewHolder.showTime.setVisibility(View.VISIBLE);
        } else {
            viewHolder.showTime.setVisibility(View.GONE);
            viewHolder.showDate_l.setVisibility(View.VISIBLE);
            viewHolder.showDate.setText(DateTools.getDay(list.get(arg0).getSpecialFrom())
                    + "");
        }

        ImageLoader.getInstance().displayImage(list.get(arg0).getGoods_Pic(),viewHolder.image);

        //停止計時
        long endTime = System.nanoTime();
        //耗時
        long spendTime = (endTime - startTime);

        sumTime += spendTime;
//        Log.d("GoogleIO", "position at:"+arg0+"--sumTime:"+String.valueOf(sumTime));
        return convertView;
    }

    public String getOff(String price, String specialPrice) {

        double off = Double.parseDouble(specialPrice)
                / Double.parseDouble(price) * 10;

        DecimalFormat df = new DecimalFormat("0.0");
        String off_String = df.format(off);

        if (off_String.equals("NaN") || off_String.equals("1")) {
            off_String = "10";
        }
        return off_String;
    }

    static class ViewHolder {
        ImageView image;
        TextView goodName;
        TextView price;
        TextView specialPrice;
        TextView koukou;
        TimeTextView mTimeText;
        TextView showDate;
        LinearLayout showDate_l;
        LinearLayout showTime;

    }

    protected void resetViewHolder(ViewHolder viewHolder) {
        viewHolder.image.setImageBitmap(null);
        viewHolder.goodName.setText("");
        viewHolder.price.setText("");
        viewHolder.specialPrice.setText("");
        viewHolder.koukou.setText("");
        viewHolder.mTimeText.setText("");
        viewHolder.showDate.setText("");

    }
}

layout使用代碼

復制代碼 代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_panicbuying_background"
android:orientation="vertical" >

<!-- 免單 -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >

<FrameLayout
android:id="@+id/index_temai_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="5dp" >

<ImageView
android:id="@+id/index_temai_image"
android:layout_width="80dp"
android:layout_height="80dp" />

<ImageView
android:id="@+id/index_temai_discount_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="@drawable/app_limit_buy_sale"
android:src="@drawable/app_limit_buy_begin" />
</FrameLayout>

<LinearLayout
android:id="@+id/temai_date_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/index_temai_image_layout"
android:orientation="vertical" >

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

<LinearLayout
android:id="@+id/temai_weikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="距離開始還有"
android:textColor="@color/black"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />

<TextView
android:id="@+id/index_temai_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="99"
android:textColor="@color/red"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天"
android:textColor="@color/black"
android:textSize="@dimen/small_text_size"
android:textStyle="bold" />
</LinearLayout>

<LinearLayout
android:id="@+id/temai_yikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal" >

<com.new0315.widgets.TimeTextView
android:id="@+id/temai_timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="@dimen/small_text_size"
/>

</LinearLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/temai_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:text="大眾甲殼蟲,豪華款,曾全套汽車配件,十年加油卡,車庫補貼,十年車險,五年以舊換新服務,比提供五年免費待架服務"
android:textColor="@color/black"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/index_raw_price"
android:textColor="@color/darkgray"
android:textSize="@dimen/small_text_size" />

<TextView
android:id="@+id/temai_yuanjia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="@color/darkgray"
android:textSize="@dimen/small_text_size" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:background="@drawable/app_limit_buy_sale_bg"
android:gravity="center_vertical" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="3dp"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="特賣價:"
android:textColor="#919263"
android:textSize="13sp" />

<TextView
android:id="@+id/temai_xiajia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5sp"
android:text="¥400"
android:textColor="@color/red"
android:textSize="13sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="折扣:"
android:textColor="#919263"
android:textSize="13sp" />

<TextView
android:id="@+id/temai_zhekou_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5sp"
android:text="5.0折"
android:textColor="@color/green"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
www.日本久久久久com.| 久久999免费视频| 亚洲免费电影一区| 精品中文字幕在线| 国产成人av在线| 亚洲精品一区中文字幕乱码| 欧美激情二区三区| 国产精品美女主播在线观看纯欲| 亚洲精品狠狠操| 国产精品一区二区久久久久| 国产亚洲免费的视频看| 亚洲天堂网在线观看| 亚洲高清一二三区| 最近2019年好看中文字幕视频| 亚洲xxxx做受欧美| 亚洲第一区第一页| 久久97精品久久久久久久不卡| 国产有码在线一区二区视频| 国产精品青草久久久久福利99| 清纯唯美日韩制服另类| 亚洲激情视频网站| 午夜精品久久久久久久久久久久久| 亚洲福利在线视频| 国产久一一精品| 亚洲一区二区三区乱码aⅴ| 97超视频免费观看| 国产亚洲欧美日韩美女| 精品视频一区在线视频| 日韩在线免费视频观看| 最近2019中文字幕mv免费看| 在线成人中文字幕| 青草青草久热精品视频在线观看| 亚洲天堂网在线观看| 91丝袜美腿美女视频网站| 日韩亚洲欧美中文高清在线| 国产视频久久网| www.欧美精品一二三区| 久久久黄色av| 在线观看日韩专区| 亚洲一二三在线| 国产精品视频xxxx| 日韩美女福利视频| 久久天堂av综合合色| 久久亚洲春色中文字幕| 久久精品国产一区二区三区| 国产精品一区二区三区毛片淫片| 国产精品免费电影| 亚洲女人天堂网| 国产精品ⅴa在线观看h| 91精品国产色综合| 亚洲午夜精品久久久久久久久久久久| 成人精品网站在线观看| 中文字幕日韩在线视频| 国外成人在线直播| 色老头一区二区三区在线观看| 亚洲欧洲av一区二区| 亚洲欧美综合另类中字| 国产精品69精品一区二区三区| 久久99亚洲热视| 中文字幕国内精品| 亚洲美女自拍视频| 日韩有码在线观看| 影音先锋日韩有码| 亚洲人精选亚洲人成在线| 在线观看久久av| 国内精品久久久久影院优| 国产欧美日韩专区发布| www.久久久久| 亚洲激情成人网| 狠狠躁夜夜躁人人爽天天天天97| 最近2019年中文视频免费在线观看| 中文字幕日韩精品有码视频| 日韩视频亚洲视频| 亚洲国产成人精品电影| wwwwwwww亚洲| 日产日韩在线亚洲欧美| 久久成人一区二区| 欧美日韩中文字幕在线| 国产亚洲成精品久久| 精品国产鲁一鲁一区二区张丽| 黄色一区二区三区| 日韩精品极品视频| 亚洲专区在线视频| 91精品视频在线播放| 久久久久亚洲精品| 欧美激情a∨在线视频播放| 91久久久在线| 久久精品人人做人人爽| 久久久精品国产| 91九色视频导航| 国产婷婷成人久久av免费高清| 欧美尤物巨大精品爽| 日韩在线播放av| 中文字幕一精品亚洲无线一区| 欧美中文在线观看国产| 精品国产91久久久久久老师| 亚洲国产日韩欧美综合久久| 国产亚洲一区二区精品| 中文字幕一区日韩电影| 国产欧美日韩专区发布| 亚洲成在人线av| 欧美日韩精品在线| 亚洲日本中文字幕免费在线不卡| 77777少妇光屁股久久一区| 亚洲精品一区在线观看香蕉| 日韩在线观看免费| 国产一区二区三区在线观看视频| 456国产精品| 麻豆国产va免费精品高清在线| 中文亚洲视频在线| 亚洲精品日韩av| 亚洲伊人久久大香线蕉av| 久久久久久国产精品久久| 久久久精品在线| 成人h猎奇视频网站| 精品视频—区二区三区免费| 久久中文字幕国产| 亚洲国产精品va在线观看黑人| 欧美午夜宅男影院在线观看| 国产日韩欧美在线| 日日噜噜噜夜夜爽亚洲精品| 91欧美激情另类亚洲| 91视频国产高清| 午夜精品福利电影| 热门国产精品亚洲第一区在线| 亚洲欧美国产精品va在线观看| 日本精品免费观看| 亚洲a在线观看| 韩日欧美一区二区| 亚洲欧美一区二区三区在线| 精品国产欧美一区二区五十路| 日本精品久久中文字幕佐佐木| 亚洲色图激情小说| 日韩亚洲精品视频| 欧美久久精品午夜青青大伊人| 日韩欧美高清在线视频| 欧美精品在线免费播放| 成人字幕网zmw| 成人444kkkk在线观看| 在线播放日韩专区| 久久精品久久久久久国产 免费| 国产美女精品视频免费观看| 91亚洲精华国产精华| 日韩欧美国产视频| 日韩精品欧美国产精品忘忧草| 欧美性生活大片免费观看网址| 一区二区av在线| 欧美性生活大片免费观看网址| 欧美日韩中国免费专区在线看| 国产91网红主播在线观看| 成人h片在线播放免费网站| 福利一区福利二区微拍刺激| 福利精品视频在线| 欧美理论片在线观看| 米奇精品一区二区三区在线观看| 91亚洲精华国产精华| 久久九九国产精品怡红院| 亚洲欧美日韩视频一区| 日韩av在线天堂网| 亚洲国语精品自产拍在线观看| 日本久久久久久| 国产精品揄拍一区二区| 91老司机精品视频| 欧美国产日韩视频|