Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

android - How to disable button as soon as its clicked

I have a button which on Click loads another activity, but before it loads another Activity if i tap it continuously, then it launches the activity but the same activity is loaded two times or more. I have tried button.setEnabled(false) soon after button.setOnClickListener...onClick but that is not disabling the button, i am also logging a text as soon as the button is tapped, thus depending on my taps it logs the text 2 or 3 times even if i had tapped it 10 times. What i feel here is before the button can listen to tap events, i am tapping it so fast that it listens to all those events as many times as i tap it.

Thus i need something where the button can just listen one tap and then disable it.

please help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Its known issue.Basically u can set the flag.

int flag=1;

 @Override
        public void onClick(View v) 
           {

                if(flag)
                 {
                   button.setEnabled(false);
                   Log.d("ins", "called");
                 }
                flag=0;
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...