Android - 在onClick操作期间隐藏按钮

时间:2016-07-29 21:36:59

标签: android android-layout onclick visibility

我需要在onClick操作期间隐藏按钮,如下所示:

    public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:

            Button button2 = (Button) findViewById(R.id.button2);
            button2.setVisibility(View.GONE);

            //Some methods
            //...

            button2.setVisibility(View.VISIBLE);

            break;
    }

但是,可见性仅在onClick之后更改,如何在onClick期间隐藏按钮

谢谢

2 个答案:

答案 0 :(得分:1)

当然因为你在同一个线程中执行所有操作,你可能会注意到可见性的变化,试试这个:

public void onClick(View view) {
switch (view.getId()){
    case R.id.button1:

        final button2 = (Button) findViewById(R.id.button2);
        button2.setVisibility(View.GONE);

        setVisibility(GONE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                //your work

                runOnUiThread(new Runnable() { //resetting the visibility of the button
                    @Override
                    public void run() {
                        //manipulating UI components from outside of the UI Thread require a call to runOnUiThread
                        button2.setVisibility(VISIBLE);
                    }
                });
            }
        }).start();

        break;
}
}

答案 1 :(得分:0)

您可以尝试以下方式:

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch(v.getId()){
    case R.id.button1: {
        if(event.getAction() == MotionEvent.ACTION_DOWN){

            Button button2 = (Button) findViewById(R.id.button2);
            button2.setVisibility(View.GONE);

            return true;
        } else if(event.getAction() == MotionEvent.ACTION_UP) {

            // some methods

            Button button2 = (Button) findViewById(R.id.button2);
            button2.setVisibility(View.VISIBLE);

            return true;
        }
    return false;
    }
}