用ButterKnife实现点击事件和多次点击事件

main.java

 @BindView(R.id.showView)
    public TextView showview;
    final static int COUNTS=5;//规定的点击次数
    final static long DURATION=2000;//规定的时间间隔(毫秒)
    long mHits[]=new long[COUNTS];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
        @OnClick({R.id.btn1,R.id.btn2,R.id.btn3})
    public void onViewClick(View view){
            switch (view.getId()){
            case R.id.btn1:
                showview.setText("alrealdy change 1");
                Toast.makeText(this,"button1 has been clicked",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn2:
                showview.setText("alreadly change 2");
                Toast.makeText(this,"button2 has been clicked",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn3:
                showview.setText("alreadly change 3");
                Toast.makeText(this,"button3 has been clicked",Toast.LENGTH_SHORT).show();
                break;
        }
    }
    @OnClick(R.id.showView)
    public void onClick(View view){
        continuousClick(COUNTS,DURATION);
    }
    private void continuousClick(int COUNTS,long DURATION){
        System.arraycopy(mHits,1,mHits,0,mHits.length-1);//每次移动一位,从数组的第一位开始移动数组长度减1次
        mHits[mHits.length-1]= SystemClock.uptimeMillis();//数组尾部为开机到现在的时间
        if(mHits[0]>=(SystemClock.uptimeMillis()-DURATION)){
        //此时意思大概为此时为点击次数的最后那次时,此时数组中的时间(即为第一次点击事件时间)和当前时间与时间间隔差的时间进行比较 
        // 若符合,即为在规定的的时间段内已经执行了要求的点击次数时
            mHits=new long[COUNTS];
            Toast.makeText(this,"welcome to new world!",Toast.LENGTH_SHORT).show();
        }
    }

main.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="this 1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="this 2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="this 3"/>
    </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NoThing..."
            android:id="@+id/showView"/>
    </LinearLayout>

然后就实现的点击Button改变TextView中的文本,多次点击TextView实现多次点击事件

参考:连续点击多次效果实现

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值