Android 将按钮可点击属性设置为 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11592014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:11:22  来源:igfitidea点击:

Set button clickable property to false

androidonclicklistener

提问by Joyson

I need to disable the click-eventfor a button in Android. Just as a sample, I have tried doing the following. I have taken a TextViewnamed it (entered a text) as Name. The condition checks if, TextViewis empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable eventfor button as true.

我需要禁用click-eventAndroid 中的按钮。作为示例,我尝试执行以下操作。我已将其TextView命名(输入文本)作为Name。条件检查是否TextView为空按钮,并且可点击应设置为 false。但是,打印 Toast 时不会发生这种情况。谁能告诉我原因。此外,如果文本字段不为空,我想将clickable eventfor 按钮重置为 true。

Java File:

Java文件:

public class ButtonclickabkeActivity extends Activity {
    TextView tv;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Name");
        btn = (Button) findViewById(R.id.button1);

        if (tv.getText().toString().length() != 0) {
            btn.setClickable(false);
            Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
        } else {
            btn.setClickable(true);
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
            }
        });
    }
}

XML file:

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"/>
    <TextView 
        android:text="TextView" 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <Button 
        android:text="Button" 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
</LinearLayout>

回答by user370305

Use

btn.setEnable(false);

instead of

代替

btn.setClickable(false);

回答by Steven

User 370305 is correct. .setEnableis what your looking for. Or you could use android:clickablein the layout XML file.

用户 370305 是正确的。.setEnable是您要找的。或者您可以android:clickable在布局 XML 文件中使用。

回答by Tim Kruichkov

In my case

就我而言

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
         view.setEnabled(false);
    }
 });