我想制作按钮,当按住3秒钟时我做了这件事。它确实有效,但我想知道它是否正确的做事方式,我的意思是我想让所有按钮设计成相同的方式,整个菜单基于保持 x 秒然后继续我不知道它是否会出现问题,不会让我的应用程序变得迟钝。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onbutton = (Button)findViewById(R.id.onbutton);
onbutton.setOnTouchListener(new View.OnTouchListener() {
private Handler handler;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
onbutton.setBackgroundResource(R.drawable.onbuttonshape);
handler = new Handler();
handler.postDelayed(run,3000);
break;
case MotionEvent.ACTION_UP:
onbutton.setBackgroundResource(R.drawable.buttonshape);
handler.removeCallbacks(run);
break;
}
return true;
}
Runnable run = new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "delayed msg", Toast.LENGTH_SHORT).show();
}
};
});
}
答案 0 :(得分:3)
答案 1 :(得分:1)
我只想使用方法setOnLongClickListener()
:
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Do what you want to do atfer a long click here
return true;
}
});