Connection handling. The button cannot be clicked multiple times in a short period of time.

Source: Internet
Author: User

Connection handling. The button cannot be clicked multiple times in a short period of time.

Reprinted please indicate the source: http://www.cnblogs.com/cnwutianhao/p/6694072.html

 

When you click a button during App creation, if you do not perform the link processing, the button will be clicked multiple times in a short time.

To avoid this situation, we need to customize an abstract class CustomClickListener and follow View. OnClickListener

Abstract class CustomClickListener implements View. onClickListener {private static final int MIN_CLICK_DELAY_TIME = 1000; // you can only click private long lastClickTime = 0 once in 1 second; private int id =-1; @ Override public void onClick (View v) {long currentTime = Calendar. getInstance (). getTimeInMillis (); int mId = v. getId (); if (id! = MId) {id = mId; lastClickTime = currentTime; onNoDoubleClick (v); return;} if (currentTime-lastClickTime> MIN_CLICK_DELAY_TIME) {lastClickTime = currentTime; onNoDoubleClick (v );}} protected abstract void onNoDoubleClick (View v );}

 

This document uses the Data-Binding framework. So is added under build. gradle (Module: app ).

android {    ...    dataBinding {        enabled = true    }}

 

Write the connection processing into the class to be implemented

Public class MainActivity extends AppCompatActivity {private ActivityMainBinding mBinding; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mBinding = DataBindingUtil. setContentView (this, R. layout. activity_main); mBinding. btn. setOnClickListener (new CustomClickListener () {@ Override protected void onNoDoubleClick (View v) {// TODO: the operation you want to perform }});}}

 

Layout File

<?xml version="1.0" encoding="utf-8"?><layout xmlns:android="http://schemas.android.com/apk/res/android">    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context="com.tnnowu.android.perfectclick.MainActivity">        <Button            android:id="@+id/btn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button" />    </RelativeLayout></layout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.