Where to set all Listeners?

前端 未结 5 1982
旧巷少年郎
旧巷少年郎 2020-12-18 21:50

Where to set all Listeners for the user interfaces?
Is it good practice to set them in onCreate? This looks so unstructured and strange.
Is

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 22:21

    This might be what you want to avoid a mess

    public class SomeActivity extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState){
            Button button1 = (Button)findViewById(R.id.button1);
            button1.setOnClickListener(new OnClickListener(){
                @Override
                public void onClick(View view){
                    SomeActivity.this.button1_onClick(view);
                }
            });
        }
    
        private void button1_onClick(View view){
            ///do stubs here
        }
    }
    

提交回复
热议问题