maq*_*jav 5 android android-4.0-ice-cream-sandwich
我想在Android中为我的应用程序添加一个按钮,并在按下按钮几秒钟后在此按钮(onclick)上捕获事件,因此在第一次触摸时它不会做出反应.这可以在Android上实现吗?
现在我有下一个代码,用于捕获主页按钮上的onclick(在ActionBar中).
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
showSendLogAlert();
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
当用户点击此按钮时,它会通过电子邮件发送一个小报告,我不想意外地启动此事件,这就是为什么我希望用户按下几秒钟以确保他想要这样做操作.
解:
基于以下评论,我得到了这个有效的解决方案:
@Override
protected void onCreate(final Bundle savedInstanceState) {
// stuff
// Set the home button clickable
getActionBar().setHomeButtonEnabled(true);
// Define a long click listener instead of normal one
View homeButton = findViewById(android.R.id.home);
homeButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showSendLogAlert();
return false;
}
});
// more stuff
}
Run Code Online (Sandbox Code Playgroud)
我做了类似的东西,但在我的情况下,如果连续按下按钮3秒钟,我想显示一个新的活动.使用此代码作为参考.
Runnable mRunnable,timeRunnable;
Handler mHandler=new Handler();
btnBackoffice = (Button) findViewById(R.id.btn_backoffice);
btnBackoffice.setOnTouchListener(buttonOnTouchListener);
private OnTouchListener buttonOnTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
mHandler.postDelayed(timeRunnable, 3000);
break;
case MotionEvent.ACTION_UP:
mHandler.removeCallbacks(timeRunnable);
break;
}
return true;
}
};
timeRunnable=new Runnable(){
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, BackofficeActivity.class);
startActivity(intent);
}
};
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
| 归档时间: |
|
| 查看次数: |
1396 次 |
| 最近记录: |