简体   繁体   中英

Android - the button view has never been used in the function

Please pay attention to onClickButton(Button button) function. Here the button view has never been used in the function so why it was placed there and what does this "phenomenon" called in Java world?

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.test_layout);
    super.onCreate(savedInstanceState);
    setUpUI();
}

//BUTTON
private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onClickButton((Button) view);
        }
    });

}

public void onClickButton(Button button){
    Toast.makeText(this,"Button clicked",Toast.LENGTH_SHORT).show();
}
}

There's no need of this onClickButton :

private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onClickButton((Button) view);
        }
    });

}

public void onClickButton(Button button){
    Toast.makeText(this,"Button clicked",Toast.LENGTH_SHORT).show();
}

You are already defining the buttons onClick() , just do this way:

private void setUpUI(){
    Button b=(Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Toast.makeText(getBaseContext(),"Button clicked",Toast.LENGTH_SHORT).show();
        }
    });
}

Edit: Your question: the button view has never been used in the function

The code here you presented doesn't demonstrate the use of View Passed in the onClick(View view) , but if you think, you'll see that a view is passed to function so as to make changes to that specific view, for example, change the backgroundColor of view, hide the view - and much more things can be done here related to view on click of the button.

I guess you are getting, what i am trying to explain.

老实说,我将其称为不好的编程。

The OnClickListener interface defines the onClick() method in a way that it has a View as an parameter. That the programmer in your case creates another method and even passes the parameter along although it's not used later on, lets me suppose that he has either no idea what he's doing or the "Button clicked" toast is just used as an example.

The idea behind passing the View that received the click is that you can create a single OnClickListener and assign it to more than one View (thus saving some memory). In the onClick() method then you will need to find out which of the Views has actually been clicked and that's why the View is passed to the method.

A standard implementation then is something like so:

public void onClick(View view) {
    switch (view.getId()) {
        case R.id.btn_a : /* handle click on button a */ break;
        case R.id.btn_b : /* handle click on button b */ break;
        /* and so on */
    }
}

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