Need to handle click from NON-Activity( .java ) class

前端 未结 3 565
深忆病人
深忆病人 2021-01-19 18:43

i have one main Activity class which contains huge amount of code/data. So i want to make it short and readable so i want to create one .java file which handle the some func

3条回答
  •  失恋的感觉
    2021-01-19 19:18

    Yes you can do this by implementing OnClickListener in your other java class and call this in you activity class for this do like this

    Button b=(Button)findviewById(R.id.b1);
    b.setOnClickListener(new NonActivityClass(this));
    

    We are passing activity context to NonActivityClass this will make you access your UI component in NonActivityClass but keep in mind this may lead to memory leak;

    And implement OnClickListener like :

    public class NonActivityClass implements OnClickListener 
    {
        void onClick(View oView)
        {
            // Do your stuff here
        }
    }
    

提交回复
热议问题