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实现多次点击事件
参考:连续点击多次效果实现