阻止用户一次按两个按钮?

时间:2013-05-16 09:50:31

标签: android user-interface multi-touch

有没有办法只注册一个按钮?在我的活动中,我有两个按钮,每个按钮开始另一个活动 - 此时如果我同时按下它们,两个活动都开始,一个在另一个上面。我想防止这种情况,并且在任何给定时间只运行一个或另一个。我想过要禁用多点触控,但这对我来说有点不好看。

编辑:我试过这样做:

public class MainActivity extends Activity {

    Button btn_send;
    Button btn_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_send = (Button) findViewById(R.id.btn_send);
        btn_receive = (Button) findViewById(R.id.btn_receive);
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (v.equals(btn_send)) {
                    if (isWifiConnected()) {
                        btn_receive.setClickable(false);
                        startActivity(new Intent(MainActivity.this,
                                SendActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Wifi connection required", Toast.LENGTH_SHORT)
                                .show();
                    }
                } else if (v.equals(btn_receive)) {
                    if (isWifiConnected()) {
                        btn_send.setClickable(false);
                        startActivity(new Intent(MainActivity.this,
                                ReceiveActivity.class));
                    } else {
                        Toast.makeText(MainActivity.this,
                                "Wifi connection required", Toast.LENGTH_SHORT)
                                .show();
                    }
                }

            }

        };
        btn_send.setOnClickListener(listener);
        btn_receive.setOnClickListener(listener);
    }

    @Override
    public void onPause() {
        btn_send.setClickable(true);
        btn_receive.setClickable(true);
        super.onPause();
    }
}

我仍然可以在Galaxy S2 GT-i9100上同时按下两个按钮。

7 个答案:

答案 0 :(得分:8)

对于按钮持有者

android:splitMotionEvents =“false”

答案 1 :(得分:1)

为这两个按钮注册相同的onClickListener,并在点击前者时为后者调用setEnabled ( false )

答案 2 :(得分:1)

首先,创建一个类来处理触摸事件,如下所示:

public class SingleClickTouchListener implements OnTouchListener {
    private boolean mOneViewClicked = false;

    /**
     * Holds the clicked view. Only allow click in other views when this view is no longer clicked.
     */
    private Object mClickedView = null;

    @Override
    public boolean onTouch(View view, MotionEvent motionEvt) {
        boolean consumed = false;

        switch (motionEvt.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (!mOneViewClicked) {
                mClickedView = view;
                mOneViewClicked = true;
            } else {
                consumed = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mClickedView == view) {
                mOneViewClicked = false;
                consumed = false;
            } else {
                consumed = true;
            }
            break;
        default:
            // No action required.
            break;
        }

        return consumed;
    }
}

然后,创建此类的实例:

private SingleClickTouchListener mSingleClickTouchListener = new SingleClickTouchListener();

现在将同一个实例传递给您不希望同时按下的所有视图(或按钮)。

view1.setOnTouchListener(mSingleClickTouchListener);
view2.setOnTouchListener(mSingleClickTouchListener);
view3.setOnTouchListener(mSingleClickTouchListener);

答案 3 :(得分:1)

这对我有用

boolean hasRunOnce = false;
ImageButton clickExample1, clickExample2;

    clickExample1 = (ImageButton) findViewById(R.id.btnExample1);
    clickExample1.setOnClickListener(this);

    clickExample2 = (ImageButton) findViewById(R.id.btnExample2);
    clickExample2.setOnClickListener(this);


}

@Override
public void onClick(View v) {


    if (hasRunOnce == false) {
        hasRunOnce = true;  //Stop users clicking buttons simultaneously

        switch (v.getId()) {
        case R.id.btnExample1:
            //do stuff
            break;
        case R.id.btnExample2:
            //do stuff
            break;

        }
    }
}



@Override
protected void onResume() {
    hasRunOnce = false;

    super.onResume();
}

}

答案 4 :(得分:0)

使用相同的点击列表器。 即,为类实现onClickListener,为两个按钮实现button.setOnClickListener(this)。

答案 5 :(得分:0)

在常见的onclick-listener

if(oneClick){
   oneClick = false;
   //launch the activity
}

,并在onPause或适当的地方重置标志。

答案 6 :(得分:0)

试试这个

button1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                button2.setClickable(false);

                //launch first activity
            }
        });

button2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                button1.setClickable(false);

                //launch second activity
            }
        });