创建函数以处理 onClick 还是创建监听器更好?

huangapple go评论74阅读模式
英文:

Which is better, creating functions for onClick or creating Listeners?

问题

I have watched different tutorials on android development from different channels.

One of them makes functions for onClick events and then adds them on the attribute of the View.

Example:

public void change(View v){
    ((TextView) findViewbyId(R.id.example)).setText("Changed");
}

And then adds this to the attribute of the button.

But the other one implements the View.OnClickListener interface, and it becomes like this:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView output;
    private EditText edit;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.change: {
                Toast.makeText(this, "Change button was clicked", Toast.LENGTH_SHORT).show();
            } break;
            case R.id.input:{
                Toast.makeText(this, "Typing..", Toast.LENGTH_SHORT).show();
            } break;
            default: break;
        } 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.change);
        btn.setOnClickListener(this);
        ((Button) findViewById(R.id.nothing)).setOnClickListener(this);
        edit = findViewById(R.id.input);
        edit.setOnClickListener(this);
        output = findViewById(R.id.output);
    }
}

I'm new to android development, so I was wondering which is better? Which is more used professionally? and why? Thank you!

<details>
<summary>英文:</summary>

I have watched different tutorials on android development from different channels. 

One of them makes functions for onClick events and then adds them on the attribute of the View.

Example:

    public void change(View v){
        ((TextView) findViewbyId(R.id.example)).setText(&quot;Changed&quot;);
    }

And then adds this to the attribute of the button.

But the other one implements the View.OnClickListener interface, and it becomes like this:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private TextView output;
        private EditText edit;
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.change: {
                    Toast.makeText(this, &quot;Change button was clicked&quot;, Toast.LENGTH_SHORT).show();
                } break;
                case R.id.input:{
                    Toast.makeText(this, &quot;Typing..&quot;, Toast.LENGTH_SHORT).show();
                } break;
                default: break;
            } 
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn = findViewById(R.id.change);
            btn.setOnClickListener(this);
            ((Button) findViewById(R.id.nothing)).setOnClickListener(this);
            edit = findViewById(R.id.input);
            edit.setOnClickListener(this);
            output = findViewById(R.id.output);
        }

Im new to android development, so I was wondering which is better? Which is more used professionally? and why? Thank you!

</details>


# 答案1
**得分**: 1

android:onClick=&quot;change&quot;

两种方式都是正确的你可以选择更合适的方式如果你只有一个可点击的 `View`,那么定义一个指向某个方法的 XML 属性会生成更少的代码但如果你有多个可点击的 `View`,那么通过Java代码来管理它们可能更容易例如在运行时移除/禁用点击处理),并在一个 `onClick` 方法中处理所有点击事件

请注意如果你的 `Activity` 中实现了 `View.OnClickListener`,那么你还可以写成

android:onClick=&quot;onClick&quot;

然后你就不必在代码中调用 `setOnClickListener(this)`通常这样做甚至不需要 `findViewById` 和引用到 `View`。

<details>
<summary>英文:</summary>

assuming you are using XML attribute

    android:onClick=&quot;change&quot;

both ways are proper and you may pick more appropriate. If you have one and only clickable `View` then definining XML attr pointing on some method will produce less code, but if you have more of them then it may be easier for manage all of them (e.g. removing/disabling click handling during runtime) by Java code and handle all clicks in one `onClick` method

note you if you have `implements View.OnClickListener` in your `Activity` then you may also write

    android:onClick=&quot;onClick&quot;

and then you don&#39;t have to call `setOnClickListener(this)` in code. oftenly this makes that you don&#39;t even have to `findViewById` and reference to `View`

</details>



huangapple
  • 本文由 发表于 2020年8月3日 13:51:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224346.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定