gpt4 book ai didi

android - 如何避免在android中同时单击多个按钮?

转载 作者:IT王子 更新时间:2023-10-29 00:00:27 28 4
gpt4 key购买 nike

我在 View 中使用了两个按钮。同时单击两个按钮时,它将一次进入不同的 Activity 。如何避免这种情况?

我试过这样,但它不起作用请保存....

public class MenuPricipalScreen extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_principal_layout);


findViewById(R.id.imageView2).setOnClickListener(new OnClickListener() {

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

disable(findViewById(R.id.imageView3));

Intent intent = new Intent(MenuPricipalScreen.this,
SelectYourLanguageVideo.class);
startActivity(intent);
}
});
findViewById(R.id.imageView3).setOnClickListener(new OnClickListener() {

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

disable(findViewById(R.id.imageView2));

Intent intent = new Intent(MenuPricipalScreen.this,
CategoryScreen.class);
intent.putExtra("request", "false");
startActivity(intent);
}
});

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
((ImageView) findViewById(R.id.imageView3)).setEnabled(true);
((ImageView) findViewById(R.id.imageView2)).setEnabled(true);
((ImageView) findViewById(R.id.imageView3)).setClickable(true);
((ImageView) findViewById(R.id.imageView2)).setClickable(true);
((ImageView) findViewById(R.id.imageView3)).setFocusable(true);
((ImageView) findViewById(R.id.imageView2)).setFocusable(true);
}

private void disable(View v) {
Log.d("TAG", "TAG" + v.getId());
v.setEnabled(false);
v.setClickable(false);
v.setFocusable(false);
}
}

谢谢,

最佳答案

避免多次点击的标准方法是保存最后一次点击的时间并避免在 1 秒(或任何时间跨度)内点击其他按钮。示例:

// Make your activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
...
}

.
.
.

// variable to track event time
private long mLastClickTime = 0;

// View.OnClickListener.onClick method defination

@Override
public void onClick(View v) {
// Preventing multiple clicks, using threshold of 1 second
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();

// Handle button clicks
if (v == R.id.imageView2) {
// Do your stuff.
} else if (v == R.id.imageView3) {
// Do your stuff.
}
...
}

.
.
.

}

关于android - 如何避免在android中同时单击多个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20971484/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com