How to catch a click event on a button?

前端 未结 7 934
[愿得一人]
[愿得一人] 2020-12-05 04:28

.NET Developer here just getting started with Eclipse and Android.

Can someone show me in the simplest way possible, with the absolute fewest lines of code, how to D

7条回答
  •  臣服心动
    2020-12-05 05:16

    Taken from: http://developer.android.com/guide/topics/ui/ui-events.html

    // Create an anonymous implementation of OnClickListener
    private OnClickListener mCorkyListener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        }
    };
    
    protected void onCreate(Bundle savedValues) {
        ...
        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corky);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(mCorkyListener);
        ...
    }
    

提交回复
热议问题