简体   繁体   English

Android禁用按钮看上去很活跃

[英]Buttons Android Disabled looks Active

I'm an Android beginner and I am currently trying to make my first calculator app. 我是Android初学者,目前正在尝试制作我的第一个计算器应用程序。

My biggest inspiration is the Windows 10 built-in calc. 我最大的启发是Windows 10内置的calc。 I want some buttons to be disabled during some actions to get rid of bugs but I don't like the look of disabled-buttons. 我希望在执行某些操作时禁用某些按钮以消除错误,但我不喜欢禁用按钮的外观。

I'd rather have them enabled but make them so they don't perform any action. 我宁愿启用它们,但使它们不执行任何操作。 I tried to use .setClickable(false) but then there is no sound of click as well as no animation of clicking (shadow). 我尝试使用.setClickable(false)但是没有单击声音,也没有单击动画(阴影)。

Can they be fully clickable but have no action? 它们可以完全单击但没有动作吗?

Try this: 尝试这个:

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);

What it does is disable the button and make it not clickable. 它的作用是禁用按钮并使它不可单击。

Use a variable to decide that. 使用变量来决定。 Put it somewhere in your class: 将其放在班级中的某个位置:

private boolean clicked = false;

Then(at the on click listener of the button): 然后(在按钮的单击监听器上):

if (clicked) {
  //your action happens here(it wouldn't happen, until you change clicked to true)
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/disabled_color" android:state_enabled="false" />
    <item android:drawable="@color/enabled_color" android:state_enabled="true"/>
</selector>

You can use the above xml (let's say we name it button_selector.xml) Apply it as background for your Button (in your activity layout) In your code, based on your actions, use 您可以使用上面的xml(假设我们将其命名为button_selector.xml)将其用作Button的背景(在您的活动布局中)在您的代码中,根据您的操作,使用

setEnabled(false);

and that's it. 就是这样。 You can change it back programmatically when you want the button to be active again. 如果您希望再次激活该按钮,可以通过编程方式将其更改回去。 Also, you can add 另外,您可以添加

setFocusable(false);
setFocusableInTouchMode(false);
setClickable(false);

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

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