Java 通过编程方式在android中单击按钮时执行传递效果?

Java 通过编程方式在android中单击按钮时执行传递效果?,java,android,Java,Android,当用户在应用程序中单击时,我需要在单击“执行”后在按钮上显示涟漪效应,但我需要以编程方式应用它。您可以尝试以下操作: yourBtn = findViewById(R.id.your_btn); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { TypedValue outValue = new TypedValue(); getTheme().resolveAttribute(and

当用户在应用程序中单击时,我需要在单击“执行”后在按钮上显示涟漪效应,但我需要以编程方式应用它。

您可以尝试以下操作:

    yourBtn = findViewById(R.id.your_btn);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        TypedValue outValue = new TypedValue();
        getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        yourBtn.setForeground(getResources().getDrawable(outValue.resourceId, null));
    }
如果没有为按钮设置特定的背景,则还可以使用以下代码段:

    startSearchBtn = findViewById(R.id.start_search_btn);
    TypedValue outValue = new TypedValue();
    getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    startSearchBtn.setBackgroundResource(outValue.resourceId);

如果你只是问如何产生连锁反应,看看这篇文章吧?