我有两个按钮,每个按钮都有各自的功能,但是当用户同时单击这些按钮时,我想添加另一个功能。这是我的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1, button2;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
textView = findViewById(R.id.textView);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
private long mLastClickTime = 0;
@Override
public void onClick(View v) {
if (System.currentTimeMillis() - mLastClickTime < 1000) {
textView.setText("Double Action");
SPLogger.logMassage("Same", (System.currentTimeMillis() - mLastClickTime) + "");
return;
}
mLastClickTime = System.currentTimeMillis();
SPLogger.logMassage("notSame", System.currentTimeMillis() + "");
if (v.getId() == R.id.button1) {
textView.setText("Only First Action");
} else if (v.getId() == R.id.button2) {
textView.setText("Only Second Action");
}
}
}
此代码几乎可以正常工作,但是我有一个小问题。例如,当我快速单击第一个按钮时,它的作用就像同时动作。谁能告诉我我的代码有什么问题吗? 也 在Android中同时检查点击的最佳方法是什么? 谢谢
答案 0 :(得分:0)
您必须检查第二次单击是否来自第一次单击以外的其他按钮,因此必须将最后一次单击的按钮保留在变量中。 例子
private long mLastClickTime = 0;
private int mLastClickedID = -1;
@Override
public void onClick(View v) {
if (System.currentTimeMillis() - mLastClickTime < 1000 && mLastClickedID !=v.getId()) {
textView.setText("Double Action");
SPLogger.logMassage("Same", (System.currentTimeMillis() - mLastClickTime) + "");
return;
}
mLastClickTime = System.currentTimeMillis();
mLastClickedID = v.getId()
SPLogger.logMassage("notSame", System.currentTimeMillis() + "");
if (v.getId() == R.id.button1) {
textView.setText("Only First Action");
} else if (v.getId() == R.id.button2) {
textView.setText("Only Second Action");
}
}
答案 1 :(得分:0)
我刚刚对此进行了测试,并且效果很好:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button button1, button2;
private boolean buttonClicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (!buttonClicked) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
buttonClicked = false;
}
}, 100); //100ms
}
else{
Toast.makeText(this, "Both Clicked !", Toast.LENGTH_SHORT).show();
}
switch (v.getId()) {
case R.id.button1:
buttonClicked=true;
//do whatever functions you want here
break;
case R.id.button2:
buttonClicked=true;
//do whatever functions you want here
break;
}
}
}
希望这对您有帮助!
答案 2 :(得分:0)
这似乎也可行:
@Override
public void onClick(View v) {
String TAG = "myTag";
if(System.currentTimeMillis() - 200 >= lastClick){
if(btn1.isPressed() && btn2.isPressed()){
Log.i(TAG, "onClick: both");
}else if(v.getId() == R.id.button1){
Log.i(TAG, "onClick: button 1");
}else if(v.getId() == R.id.button2){
Log.i(TAG, "onClick: button 2");
}
lastClick = System.currentTimeMillis();
}
}
200毫秒的时间是为了防止两次调用onClick内容