如何在不点击的情况下实现按钮上的波纹动画

How to achieve ripple animation on button without even clicking

我需要在 activity 的 onCreate() 中简单地在我的按钮上显示波纹。在我扫描过的所有代码中,涟漪效应仅在按下按钮时可见。请指导我如何在不单击按钮的情况下默认显示波纹。

您仍然需要单击按钮,但以编程方式。在你的 onCreate 方法中使用 yourButton.performClick() 并确保当你这样做时不要 运行 处理按钮点击事件的代码,因为你可以使用一个 boolean 变量检查您是否以编程方式执行此操作或实际操作是 perform

yourbutton clicklistener  {
if(isprogrammatic){
// dont do anything
 isprogrammatic = false
}
else{
// run your code
}
} 

OnCreate

onCreate(Bundle..){ // your on create method

    //yes it is programmatic
    isprogrammatic = true;
    yourbutton.performClick();

    }