简体   繁体   English

Android-该功能从未使用过按钮视图

[英]Android - the button view has never been used in the function

Please pay attention to onClickButton(Button button) function. 请注意onClickButton(Button button)函数。 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? 这里的按钮视图从未在函数中使用过,那么为什么将其放置在函数视图中,以及这种“现象”在Java世界中被称为什么?

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 : 不需要这个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: 您已经在定义按钮onClick() ,方法是:

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. 您在此处提供的代码并未演示onClick(View view)中View Passed的用法,但如果您认为,您会看到一个视图已传递给函数,以便对该特定视图进行更改,例如,更改视图的backgroundColor,隐藏视图-单击此按钮可以在此处完成与视图相关的更多操作。

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. OnClickListener接口以将View作为参数的方式定义onClick()方法。 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). 传递接受点击的View的想法是,您可以创建一个OnClickListener并将其分配给多个View(从而节省一些内存)。 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. 在onClick()方法中,您将需要找出实际上已单击了哪些视图,这就是将视图传递给该方法的原因。

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 */
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM