Java 如何使按钮不可点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19625569/
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-12 18:54:48  来源:igfitidea点击:

How to make a button unclickable

javaandroidbuttonbuttonclick

提问by Dan

So right now i am having trouble making the next button unclickable when it is on the last page of the activity. As of right now it goes back to the first screen. How do i make it so that it know when to grey out the button or make it unclickable when the user gets to the last screen.

所以现在我在下一个按钮位于活动的最后一页时无法点击它时遇到了麻烦。截至目前,它返回到第一个屏幕。我如何使它知道何时使按钮变灰或在用户到达最后一个屏幕时使其不可点击。

Here is my code:

这是我的代码:

public class ReadingActivity extends Activity implements OnClickListener {

    private ViewFlipper viewFlipper;
    Button btnNext, btnPrev;
    private float lastX;

    /** Called when the activity is first created */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reading);
        viewFlipper=(ViewFlipper)findViewById(R.id.view_flipper);
        btnNext=(Button)findViewById(R.id.btnNext);
        btnPrev=(Button)findViewById(R.id.btnPre);


        btnNext.setOnClickListener(this);
        btnPrev.setOnClickListener(this);

        btnNext.setEnabled(true);
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch(arg0.getId()){
        case R.id.btnNext:
            viewFlipper.setInAnimation(this, R.anim.in_from_right);
            viewFlipper.setOutAnimation(this, R.anim.out_to_left);
            viewFlipper.showNext();
            break;
        case R.id.btnPre:
            viewFlipper.setInAnimation(this, R.anim.in_from_left);
            viewFlipper.setOutAnimation(this, R.anim.out_to_right);
            viewFlipper.showPrevious();
            break;
        }

    }


}

采纳答案by JRowan

you can set the OnClickListener to null like so

您可以像这样将 OnClickListener 设置为 null

btnNext.setOnClickListener(null);

回答by Ryan Hu

I think you just want this method

我想你只是想要这个方法

button.setClickable(false);

回答by Jawad Zeb

To make a button grey and UnClickable

使按钮变灰且不可点击

put android:enabled="false"in button tag

放入android:enabled="false"按钮标签

And using code button.setEnabled(false);

并使用代码 button.setEnabled(false);