android中的按钮视图 [英] Button Views in android

查看:43
本文介绍了android中的按钮视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在按钮上指定 onClick 函数时,我有一个疑问,为什么将View用作参数?和当我们不指定该视图时会发生什么

I have a doubt when i am specifying a onClick function on button why View is used as a parameter ? and What happens when we don't specify that view

示例:

public void ara(View button){
}

此处 onClick 按钮的功能: ara

谢谢

推荐答案

View onClick()方法中被用作参数,因为它是附加了 OnClickListener .

View is being used as a parameter in the onClick() method because it's the View that the OnClickListener is attached to.

这在多种方式中非常有用,例如,如果视图是 TextView ,并且您想在单击视图时更新它的文本.

This is very useful in multiple ways, such as if the View is a TextView and you want to update it's text when the view is clicked.

其他常见用途可能是:

  • 设置视图的可见性

  • Setting View visibility

单击时将视图传递给 ObjectAnimator 进行动画处理

Passing the View to an ObjectAnimator for animations when clicking

通过 view.getContext()

总体而言,它只是您用于对 OnClickListener 所属的View进行修改或将 View 用于其他目的的必要参数,或者计算.

Overall, it's just a necessary parameter for you to either perform modifications on the View that the OnClickListener belongs to, or for you to use the View for other purposes or calculations.

此外,如果您未在 onClick()方法中包含View,则您的代码将无法编译.这是因为 OnClickListener 是一个接口,并且默认情况下,接口内部的方法没有方法主体.

Additionally, if you don't include the View in the onClick() method, then your code will fail to compile. This is because OnClickListener is an interface, and by default, methods inside an interface don't have a method body.

相反,由您自己来覆盖方法并为其指定适当的方法主体.但是,由于要覆盖该方法,因此这也意味着必须包含相同的方法名称,相同的方法修饰符和相同的方法参数,才能正确反映要覆盖的方法.

Instead, it's up to you to override the method and give it a proper method body. However, since you're overriding the method, this also means you must include the same method name, same method modifiers, and same method parameters to properly reflect the method you are overriding.

不这样做将意味着所需的接口方法没有被覆盖,因此您的代码将无法编译.

Failure to do so would mean that the required interface method isn't being overridden, so your code will fail to compile.

这篇关于android中的按钮视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆