简体   繁体   中英

Capture click in any button with android

Who to capture the click of any button?

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
            Toast.makeText(WHKConversorActivity.this, "Hello World", Toast.LENGTH_SHORT).show();                    
       }
 });

this is a click on button1 but i need an function for all buttons :-/ . Example in javascript:

$('button').click(function(){
    alert($(this).val());
});

Thanks :)

If you want all your buttons to do exactly the same thing (which is unlikely) you could bind the same click listener to each button. Eg:

final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mGlobalClick);

final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(mGlobalClick);

... ect

OnClickListener mGlobalListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
            //Stuff
            }
};

Or if you want a each button to do something different, but still need some sort of reapeated function for each, you could put that part of the code in a seperate method and reference it in every click listener

public void GlobalStuff(){
     //Stuff
}

OnClickListener mSpecificListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
            GlobalStuff();
            //More Stuff
            }
};

There is no generic way like in javascript. You need to assign the click handler to all buttons individually.

您可以尝试覆盖Button类,并在其构造函数中调用addListener

Assuming all views in your activity are in some kind of ViewGroup, you can use this:

private void applyToAllButtons(ViewGroup viewGroup, OnClickListener listener) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        if (viewGroup.getChildAt(i) instanceof Button) {
            viewGroup.getChildAt(i).setOnClickListener(listener);
        }
    }
}

Then, in your onCreate(), do the following:

@Override
public void onCreate(Bundle savedInstanceState) {
    ... // old code here
    View view = findViewById(R.id.layoutRoot);
    setContentView(view);
    OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            // Do stuff here.
        }
    };
    applyToAllButtons((ViewGroup) view, listener);
}

Thanks Jack... this is more useful :) less variables, less memory. The OnClickListener put before tiggers:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        OnClickListener globalClick = new View.OnClickListener() {
            public void onClick(View v) {
                // some functions...
            }
        };

        /* Tiggers */
        findViewById(R.id.button1).setOnClickListener(globalClick);
        findViewById(R.id.button2).setOnClickListener(globalClick);
        findViewById(R.id.button3).setOnClickListener(globalClick);
        findViewById(R.id.button4).setOnClickListener(globalClick);
        findViewById(R.id.button5).setOnClickListener(globalClick);
}

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