简体   繁体   中英

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

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 functionality as per the requirement. Like

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

and Show a Toast from non-activity class. So my question is How to handle/initialize button in non-activity? Can we get id's from Passing Context from Activity to non-activity class?

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();
    }
}

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM