How to catch a click event on a button?

前端 未结 7 937
[愿得一人]
[愿得一人] 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:14

    You can just do it in your Activity's onCreate() method. For example:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Assign the button to a variable
        Button button1 = (Button)findViewById(R.id.button1);
    
        //Assign the ImageView to a final variable, so that it's
        //accessible from an inner class
        ImageView imageView = (ImageView)findViewById(R.id.imageview1);
    
        //Assign it a new OnClickListener
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setVisibility(View.VISIBLE);
            }
        }
    }
    

提交回复
热议问题