Where to set all Listeners?

妖精的绣舞 提交于 2020-01-10 03:37:08

问题


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 there a better place to set them?


回答1:


From here: http://developer.android.com/reference/android/app/Activity.html

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

When you initialize your views, they are ready to be listened. onCreate is good callback to set listeners. In other way you can set it in onStart or onResume, but you should understand, that its bad practice, because onStart and onResume calls every time, when user see your activity. onCreate calls only when Activity is initialized. It is reason, why you should use onCreate. Actually, good practice implement method like initListeners() where you can put all you listeners logic.

Good luck!




回答2:


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




回答3:


For listeners onCreate() is good place.

Consider 2 activities A,B.

A -> B, launching 'B' Activity from 'A', if we come back from B -> A then onStart(), onResume() methods will be called again in 'A' activity and that is redundant. So it's better practice to only add listeners in onCreate() only.

And, for button listeners you can set attribute android:onClick="method_name" in xml file only.




回答4:


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
    }
}



回答5:


You can set onClick property for any view in xml .So now u have no need to find and set onClick in onCreate.Now u need to define public method in activity of name u mentioned in xml . This looks constructed.



来源:https://stackoverflow.com/questions/21204188/where-to-set-all-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!