Best way to implement View.OnClickListener in android

后端 未结 14 2291
盖世英雄少女心
盖世英雄少女心 2020-11-27 05:25

Suppose we have an Activity with a lot of views on which OnClickListener is to be registered.

The most common way to implement this is to let the Activi

相关标签:
14条回答
  • 2020-11-27 05:57

    simply you using like not implements subclass or not handle a click event just do like this way .

    android.view.View.OnClickListener method_name = new android.view.View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // put your code .
            }
        };
    

    and handle click event into button ya any type of click event like

    button_name.setOnClickListener(method_name);
    

    its work very simply Thanks

    0 讨论(0)
  • 2020-11-27 06:01

    I have found using Butterknife makes for clean code. And because it uses code generation (not reflections) it has little performance overhead.

    public class ActivityMain extends Activity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ButterKnife.inject(this);
      }
    
      @OnClick(R.id.button_foo)
      void onFoodClicked() {
        // Do some foo
      }
    
      @OnClick(R.id.button_bar)
      void onBarClicked() {
        // do some bar
      }
    }
    
    0 讨论(0)
  • 2020-11-27 06:02
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Chronometer chronometer;
        private Button startButton;
        private Button stopButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            chronometer = findViewById(R.id.chronometer);
            startButton =findViewById(R.id.startBtn);
            stopButton = findViewById(R.id.stopBtn);
    
            startButton.setOnClickListener(this);
            stopButton.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.startBtn:
                    chronometer.start();
                    break;
                case R.id.stopBtn:`
                    chronometer.stop();
                    break;
    
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 06:05

    Your ClickListener is an inner non-static class the coupling of this 'has-a' is no different than if your class Activity implemented View.OnClickListener. This is because your inner ClickListener requires an instance of ActivityMain and really can't be reused. I would argue that you're over engineering and aren't actually gaining anything.

    EDIT: To answer your question I like to have anonymous View.OnClickListener for each widget. I think this creates the best separation of logic. I also have methods like setupHelloWorldTextView(TextView helloWorldTextView); where I put all my logic related to that widget.

    0 讨论(0)
  • 2020-11-27 06:05
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button north,south,east,west;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        north.setOnClickListener(this);
        south.setOnClickListener(this);
        east.setOnClickListener(this);
        west.setOnClickListener(this);
    }
    
    private void init(){
        north = findViewById(R.id.north);
        south = findViewById(R.id.south);
        east = findViewById(R.id.east);
        west = findViewById(R.id.west);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.north:
                Toast.makeText(MainActivity.this,"NORTH",Toast.LENGTH_SHORT).show();
                break;
            case R.id.south:
                Toast.makeText(MainActivity.this,"SOUTH",Toast.LENGTH_SHORT).show();
                break;
            case R.id.east:
                Toast.makeText(MainActivity.this,"EAST",Toast.LENGTH_SHORT).show();
                break;
            case R.id.west:
                Toast.makeText(MainActivity.this,"WEST",Toast.LENGTH_SHORT).show();
                break;
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-27 06:06

    Here you can create a btnClickListner object and after that you will call that btnCLickLisner object when ever you want to perform the onCLieck actions for buttons..

    Let us assume, in my activity i have a 5 to 10 buttons and writing each button separate onclick listner is bad idea. So to over come this,we can use like below..

    register your buttons

    Button button1 = (Button)findViewById(R.id.button1);
    Button button2 = (Button)findViewById(R.id.button2);
    Button button3 = (Button)findViewById(R.id.button3);
    Button button4 = (Button)findViewById(R.id.button4);
    Button button5 = (Button)findViewById(R.id.button5);
    

    Here i am setting the onclick listner to my buttons after click

    button1.setOnClickListener(btnClickListner);
    button2.setOnClickListener(btnClickListner);
    button3.setOnClickListener(btnClickListner);
    button4.setOnClickListener(btnClickListner);
    button5.setOnClickListener(btnClickListner);
    

    Here is the btnClick Listner implementation

    View.OnClickListener btnClickListner = new OnClickListener()
        {
      @Override
            public void onClick( View v )
            {
                // TODO Auto-generated method stub
                if( button1.getId() == v.getId() )
                {
                    //Do Button1 click operations here
    
                }
                else if( button2.getId() == v.getId() )
                {
    
                   // Do Button2 click operations here
    
                }
                else if( button3.getId() == v.getId() )
                {
                     // Do Button3 click operations here
    
                }
                else if( button4.getId() == v.getId() )
                {
                    // Do Button4 click operations here
    
                }
                else if( button5.getId() == v.getId() )
                {
                    // Do Button5 click operations here
                }
    
            }
    
         }
    
    0 讨论(0)
提交回复
热议问题