Where to set all Listeners?

前端 未结 5 1964
旧巷少年郎
旧巷少年郎 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

    Use onCreate method to set the UI and to get the Widget from UI.

    protected void onCreate(Bundle savedValues) {
        // Here set the UI and get the widgets
        //set the Listeners on the widgets you are getting at the above line
    }
    

    And you can define a clickListener for the widgets and use it in onCreate method

    OnClickListener someListener = new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(v.getContext(), "widget pressed ", Toast.LENGTH_SHORT).show();
    
        }
    };
    

    and you can set the above clickListener to a widget which you have created in onCreate method

提交回复
热议问题