setOnclickListener VS OnClickListener VS View.OnClickListenerVS、setOnclickListener、View、OnClickListe

2023-09-06 07:03:49 作者:猛虎总是独行

当我创建一个按钮对象,侦听点击我要我的理解是: 1.创建按钮对象 2.使用OnClickListner使其收听到用户的点击 3.使用的onClick用户后执行的操作点击按钮

现在在哪里setOnClickListener融入上述的逻辑?哪一个真正监听按钮点击? setOnclickListener? ONclickListener? View.OnClickListener?那些是什么3之间的区别?

下面是我发现在Android开发人员:

 下面的例子说明如何注册的点击监听器的按钮。

//创建一个匿名实现OnClickListener的
私人OnClickListener mCorkyListener =新OnClickListener(){
    公共无效的onClick(视图v){
      //做一些按钮被按下时,
    }
};

保护无效的onCreate(包savedValues​​){
    ...
    //捕捉我们从布局按钮
    Button按钮=(按钮)findViewById(R.id.corky);
    //注册onClick的监听器与执行上述
    button.setOnClickListener(mCorkyListener);
    ...
}
 

您可能还会发现它更方便地实现OnClickListener作为活动的一部分。这将避免额外的类加载和对象分配。例如:

 公共类为ExampleActivity扩展活动实现OnClickListener {
    保护无效的onCreate(包savedValues​​){
        ...
         Button按钮=(按钮)findViewById(R.id.corky);
        button.setOnClickListener(本);
    }

    //实施OnClickListener回调
    公共无效的onClick(视图v){
      //做一些按钮被按下时,
    }
}
 

解决方案

假设我们有3个按键,例如

 公共类MainActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_main);


        //捕捉我们从布局按钮
        Button按钮=(按钮)findViewById(R.id.corky);
        按钮按钮2 =(按钮)findViewById(R.id.corky2);
        按钮按钮3 =(按钮)findViewById(R.id.corky3);
        //注册onClick的监听器与执行上述
        button.setOnClickListener(mCorkyListener);
        button2.setOnClickListener(mCorkyListener);
        button3.setOnClickListener(mCorkyListener);

    }

    //创建一个匿名实现OnClickListener的
    私人View.OnClickListener mCorkyListener =新View.OnClickListener(){
        公共无效的onClick(视图v){
            //做一些按钮被按下时,
            //是的,我们将处理点击这里,但其中单击按钮???我们不知道

        }
    };

}
 

所以我们会做什么?

 公共类MainActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_main);


        //捕捉我们从布局按钮
        Button按钮=(按钮)findViewById(R.id.corky);
        按钮按钮2 =(按钮)findViewById(R.id.corky2);
        按钮按钮3 =(按钮)findViewById(R.id.corky3);
        //注册onClick的监听器与执行上述
        button.setOnClickListener(mCorkyListener);
        button2.setOnClickListener(mCorkyListener);
        button3.setOnClickListener(mCorkyListener);

    }

    //创建一个匿名实现OnClickListener的
    私人View.OnClickListener mCorkyListener =新View.OnClickListener(){
        公共无效的onClick(视图v){
            //做一些按钮被按下时,
            //是的,我们将处理点击这里,但其中单击按钮???我们不知道

            //所以我们将
            开关(v.getId()/ *为获得点击查看身份证** /){
                案例R.id.corky:

                    //做一些事情的木栓质被点击时

                    打破;
                案例R.id.corky2:

                    //做一些事情的corky2被点击时

                    打破;
                案例R.id.corky3:

                    //做一些事情的corky3被点击时

                    打破;
                默认:
                    打破;
            }
        }
    };

}
 

或者,我们可以这样做:

 公共类MainActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_main);


        //捕捉我们从布局按钮
        Button按钮=(按钮)findViewById(R.id.corky);
        按钮按钮2 =(按钮)findViewById(R.id.corky2);
        按钮按钮3 =(按钮)findViewById(R.id.corky3);
        //注册onClick的监听器与执行上述
        button.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //做一些事情的木栓质被点击时
            }
        });
        button2.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //做一些事情的corky2被点击时
            }
        });
        button3.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //做一些事情的corky3被点击时
            }
        });

    }

}
 

或者,我们可以实现View.OnClickListener,我认为这是最好的方式:

 公共类MainActivity扩展ActionBarActivity实现View.OnClickListener {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.activity_main);


        //捕捉我们从布局按钮
        Button按钮=(按钮)findViewById(R.id.corky);
        按钮按钮2 =(按钮)findViewById(R.id.corky2);
        按钮按钮3 =(按钮)findViewById(R.id.corky3);
        //注册onClick的监听器与执行上述
        button.setOnClickListener(本);
        button2.setOnClickListener(本);
        button3.setOnClickListener(本);

    }

    @覆盖
    公共无效的onClick(视图v){
        //做一些按钮被按下时,
        //是的,我们将处理点击这里,但其中单击按钮???我们不知道

        //所以我们将
        开关(v.getId()/ *为获得点击查看身份证** /){
            案例R.id.corky:

                //做一些事情的木栓质被点击时

                打破;
            案例R.id.corky2:

                //做一些事情的corky2被点击时

                打破;
            案例R.id.corky3:

                //做一些事情的corky3被点击时

                打破;
            默认:
                打破;
        }
    }
}
 

最后有没有真正的差异在这里只是的方式比其他的更好

My understanding is when I'm creating a button object that listens for a click I have to: 1. create the button object 2. use OnClickListner to make it listen to the user's click 3. use onClick to execute actions after the user clicks the button

now where do setOnClickListener fit into the above logic? Which one actually listens to the button click? setOnclickListener? ONclickListener ? View.OnClickListener? what are the differences between those 3?

Here is what I found in Android Dev:

The example below shows how to register an on-click listener for a Button.

// 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);
    ...
}

You may also find it more convenient to implement OnClickListener as a part of your Activity. This will avoid the extra class load and object allocation. For example:

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
         Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
}

解决方案

Imagine that we have 3 buttons for example

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corky);
        Button button2 = (Button)findViewById(R.id.corky2);
        Button button3 = (Button)findViewById(R.id.corky3);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(mCorkyListener);
        button2.setOnClickListener(mCorkyListener);
        button3.setOnClickListener(mCorkyListener);

    }

    // Create an anonymous implementation of OnClickListener
    private View.OnClickListener mCorkyListener = new View.OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked 
            // Yes we will handle click here but which button clicked??? We don't know

        }
    };

}

So what we will do?

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corky);
        Button button2 = (Button)findViewById(R.id.corky2);
        Button button3 = (Button)findViewById(R.id.corky3);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(mCorkyListener);
        button2.setOnClickListener(mCorkyListener);
        button3.setOnClickListener(mCorkyListener);

    }

    // Create an anonymous implementation of OnClickListener
    private View.OnClickListener mCorkyListener = new View.OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
            // Yes we will handle click here but which button clicked??? We don't know

            // So we will make
            switch (v.getId() /*to get clicked view id**/) {
                case R.id.corky:

                    // do something when the corky is clicked

                    break;
                case R.id.corky2:

                    // do something when the corky2 is clicked

                    break;
                case R.id.corky3:

                    // do something when the corky3 is clicked

                    break;
                default:
                    break;
            }
        }
    };

}

Or we can do this:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corky);
        Button button2 = (Button)findViewById(R.id.corky2);
        Button button3 = (Button)findViewById(R.id.corky3);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something when the corky is clicked
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something when the corky2 is clicked
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something when the corky3 is clicked
            }
        });

    }

}

Or we can implement View.OnClickListener and i think it's the best way:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        // Capture our button from layout
        Button button = (Button)findViewById(R.id.corky);
        Button button2 = (Button)findViewById(R.id.corky2);
        Button button3 = (Button)findViewById(R.id.corky3);
        // Register the onClick listener with the implementation above
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // do something when the button is clicked
        // Yes we will handle click here but which button clicked??? We don't know

        // So we will make
        switch (v.getId() /*to get clicked view id**/) {
            case R.id.corky:

                // do something when the corky is clicked

                break;
            case R.id.corky2:

                // do something when the corky2 is clicked

                break;
            case R.id.corky3:

                // do something when the corky3 is clicked

                break;
            default:
                break;
        }
    }
}

Finally there is no real differences here Just "Way better than the other"