我想检测何时没有点击按钮。例如,在上面的代码中,我想替换?????条件指示仍然单击图像视图,并且一旦不再长时间单击图像视图就退出循环。你有好主意吗?
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
while(?????)
{
int number =(Integer.parseInt(hours.getText().toString())+1)%24;
String text_number= String.valueOf(number);
if(number>-1 && number<10)
{
text_number="0"+text_number;
}
hours.setText(text_number);
}
return true;
}
});
答案 0 :(得分:1)
使用View.OnTouchListener
。
示例:https://stackoverflow.com/a/39588668/4586742
您将通过不同的活动获得onTouch
回调。
MotionEvent.ACTION_DOWN
:当用户开始按视图时。
MotionEvent.ACTION_UP
:当用户停止按下视图时。
答案 1 :(得分:0)
我从您的问题和建议的答案中得到了什么。
`
public class Main2Activity extends AppCompatActivity {
private boolean isImageViewBeingClicked = true;
private boolean isLongPressed = false;
private ImageView imageView;
private TextView hours;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView = (ImageView) findViewById(R.id.imageView);
hours = (TextView) findViewById(R.id.textView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
while (isImageViewBeingClicked) {
int number = (Integer.parseInt(hours.getText().toString()) + 1) % 24;
String text_number = String.valueOf(number);
if (number > -1 && number < 10) {
text_number = "0" + text_number;
}
hours.setText(text_number);
}
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
if (isLongPressed) {
isImageViewBeingClicked = false;
}
}
return false;
}
});
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
isLongPressed = true;
return false;
}
});
}
}
`