Home  >  Article  >  Web Front-end  >  Button has several Click response methods

Button has several Click response methods

php中世界最好的语言
php中世界最好的语言Original
2018-02-01 09:43:472794browse

This time I will bring you several Click response methods for Button. What are the precautions for using Button's Click response method? The following is a practical case, let's take a look.

Button is used a lot. I have sorted out its

event handling methods here and found that there are many implementation methods. I prefer the second one. What about you, which one is most commonly used? One?

Implementation one:

Button bt_Demo = (Button)findViewById(R.id.bt_Demo);
   bt_Demo.setOnClickListener(new OnClickListener()
   {
       @Override
       public void onClick(View v)
       {
           //响应Clicked事件
//......
       }
   });

Implementation two:

Button bt_Demo = (Button)findViewById(R.id.bt_Demo);
   bt_Demo.setOnClickListener(listener);
   private OnClickListener listener = new OnClickListener(){ 
       @Override
       public void onClick(View arg0) {
           // TODO Auto-generated method stub        
            switch(arg0.getId()){ 
                case R.id.bt_Demo:
                   //响应Clicked事件
//......
break;
                default:
                break;
                }
       }
   }

Implementation three:

Button bt_Demo = (Button)findViewById(R.id.bt_Demo);
   bt_Demo.setOnClickListener(new ButtonListener());
   private class ButtonListener implements OnClickListener{
       @Override
       public void onClick(View arg0) {
               //响应Clicked事件
//......
       }        
   }

I believe you have mastered the method after reading these cases, For more exciting content, please pay attention to other related articles on the php Chinese website!

Related reading:

How to make the html drop-down menu retain the selected value and not return to the default value after submission

How to html Example of adding attributes using style

#How to set focus on an html element

The above is the detailed content of Button has several Click response methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn