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

前端 未结 3 562
深忆病人
深忆病人 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:08

    Try this is working for me . May help you also !

    NonActivityClass:

    public class NonActivityClass {
    
        Context context;
        View v;
        public NonActivityClass(Context context, View v) {
            this.context = context;
            this.v = v;
        }
    
        public void test() {
            Button btn = (Button) v.findViewById(R.id.btn);
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(context, "Hello I am inside Non Activity Class",
                            1).show();
    
                }
            });
        }
    }
    

    MainActivity:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = getLayoutInflater().inflate(R.layout.activity_main, null);
            setContentView(v);
            NonActivityClass nac = new NonActivityClass(MainActivity.this, v);
            nac.test();
        }
    }
    
    0 讨论(0)
  • 2021-01-19 19:14

    You can just implement your onclick listener in your main class BUT use an interface to externalise the content of the listener in an other Class.

    Exemple :

    Create a class ManageClick.java that contain constructor, several methods and objects.

    An attribute

    public OnClickListenerCustom _onClickListenerCustom;
    

    And an interface

    Interface OnClickListenerCustom{
        public void onclick();
        public void onItemClick();
    }
    

    and another method like setOnClickListenerCustom :

    public void setOnClickListenerCustom(OnClickListenerCustom listener){
        this._onClickListenerCustom = listener;
    }
    

    In your main Class MainActivity.java you implement the onclick() method, in the oncreate you set the listener :

    public ManageClick _clickManager;
    
    _clickManager = new ManageClick();
    _clickManager.setOnClickListener((OnClickListenerCustom) this.MainActivity);
    

    To finish in the onclick method you only need to call

    _clickManager.onClick() or _clickManager.onItemClick()
    
    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题